SSH整合方式二:将hibernate的配置交给Spring管理

SSH整合方式二:将hibernate的配置交给Spring管理

SSH整合方式二:不带hibernate配置文件

1、hibernate配置文件中有哪些内容:
数据库连接的基本信息
Hibernate的相关的属性的配置
方言
显示SQL
格式化SQL

C3P0连接池
映射文件


2、编写一个数据库基本信息的属性配置文件:jdbc.properties

jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/study_test
jdbc.user=root
jdbc.password=123456

3、其他地方不变,参考SSH整合方式一。

只需把hibernate.cfg.xml文件交给Spring管理

applicationContext.xml文件:


<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	
	<context:property-placeholder location="classpath:jdbc.properties" />

	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.className}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.user}" />
		<property name="password" value="${jdbc.password}" />
	bean>

	
	<bean id="customerAction" class="com.pipi.ssh.web.action.CustomerAction" scope="prototype">
		<property name="customerService" ref="customerService"/>
	bean>

	
	<bean id="customerService" class="com.pipi.ssh.service.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"/>
	bean>

	
	<bean id="customerDao" class="com.pipi.ssh.dao.CustomerDaoImpl">
		<property name="sessionFactory" ref="sessionFactoryBean"/>
	bean>

	
	
	<bean id="sessionFactoryBean" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource" />

		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
				<prop key="hibernate.show_sql">trueprop>
				<prop key="hibernate.format_sql">trueprop>
				<prop key="hibernate.hbm2ddl.auto">updateprop>
			props>
		property>

		
		<property name="mappingResources">
			<list>
				<value>com/pipi/ssh/domain/Customer.hbm.xmlvalue>
			list>
		property>
	bean>


	
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactoryBean"/>
	bean>

	
	<tx:annotation-driven transaction-manager="transactionManager"/>

beans>

4、Hibernate模板的常用的方法

(1)保存操作
save(Object obj);

public void save(Customer customer) {
     
    this.getHibernateTemplate().save(customer);
}

(2)修改操作
update(Object obj);

public void update(Customer customer) {
     
    this.getHibernateTemplate().update(customer);
}

(3)删除操作
delete(Object obj);

public void delete(Customer customer) {
     
    this.getHibernateTemplate().delete(customer);
}

(4)查询操作
查询一个:
get(Class c,Serializable id);
load(Class c,Serializable id);

public Customer findById(Long id) {
     
    //return this.getHibernateTemplate().get(Customer.class, id);
    return this.getHibernateTemplate().load(Customer.class, id);
}

查询多个:
List find(String hql,Object… args);
List findByCriteria(DetachedCriteria dc);
List findByCriteria(DetachedCriteria dc,int firstResult,int maxResults);
List findByNamedQuery(String name,Object… args);

public List<Customer> findAllByHQL() {
     
    List<Customer> list = (List<Customer>) this.getHibernateTemplate().find("from Customer");
    return list;
}
public List<Customer> findAllByQBC() {
     
    DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
    List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
    return list;
}
public List<Customer> findAllByNamedQuery() {
     
    return (List<Customer>) this.getHibernateTemplate().findByNamedQuery("queryAll");
}

5、Spring提供了延迟加载的解决方案

在SSH整合开发中哪些地方会出现延迟加载:
使用load方法查询某一个对象的时候(不常用)
查询到某个对象以后,显示其关联对象。

只需在web.xml中配置一个过滤器即可,在核心过滤器之前配置:

web.xml中配置:


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>

    
    <filter>
        <filter-name>OpenSessionInViewFilterfilter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilterfilter-name>
        <url-pattern>*.actionurl-pattern>
    filter-mapping>

    
    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

web-app>

你可能感兴趣的:(Spring,spring,ssh整合)