Spring整合Hibernate5框架有两种方法
- 带有hibernate.cfg.xml的配置文件。强调:不能加绑定当前线程的配置
- 不带有hibernate.cfg.xml的配置文件
注意在本例使用的是hibernate5 在导入有关hibernate包的时候注意导入5的jar包
-
一、带有hibernate.cfg.xml的配置文件
编写bean类与映射文件并在hibernate.cfg.xml中引入
customer类
package domain;
public class Customer {
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id
+ ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry="
+ cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone="
+ cust_phone + ", cust_mobile=" + cust_mobile + "]";
}
}
映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"/>
<property name="cust_user_id" column="cust_user_id"/>
<property name="cust_create_id" column="cust_create_id"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_linkman" column="cust_linkman"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
在hibernate.cfg.xml中引入映射文件
<mapping resource="domain/Customer.hbm.xml"/>
在dao中继承HibernateDaoSupport类并使用 getHibernateTemplate获得模板操作数据库
daoImpl
package dao;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import domain.Customer;
/**
* 继承HibernateDaoSupport 简化开发操作
* @author Administrator
*
*/
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
public void add(Customer cust) {
System.out.println("dao add");
//从父类获得hibernate模板保存customer对象
this.getHibernateTemplate().save(cust);
}
}
将dao注入到service中
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置客户模块 -->
<!-- action类由struts2创建的方法 action类正常编写,需要导入struts2-spring-plugin-2.3.24.jar包 -->
<bean id="customerService" class="service.CustomerServiceImpl">
<property name="customerDao" ref="customerDao" />
</bean>
<!-- 配置sessionFactory合并hibernate使用 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml" />
</bean>
<!-- customerDao 注入到customerService中 -->
<bean id="customerDao" class="dao.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- action类由spring创建的方法 直接配置action bean 其中注入service 然后在struts.xml中action的class直接填入applicationContext.xml中bean的id值
需要注意:配置action bean 需要填写属性scope="prototype" 表示多例,其他一律单例 -->
<bean id="customerAction" class="web.action.CustomerAction"
scope="prototype">
<property name="customerService" ref="customerService"></property>
</bean>
</beans>
在applicationContext.xml中配置开启事务
在applicationContext.xml中加入
<!-- 开启事务 -->
<!-- 配置HibernatetransactionManager 需要导入 sessionFactory-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
发送前台请求测试
控制台输出结果
查看数据库结果
已经正常加入数据库
二、没有hibernate.cfg.xml的配置文件
实现没有hibernate配置文件需要将原hibernate配置文件中的所有属性都配置到spring中
以下是hibernate配置文件中的属性
-
数据库参数
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day04</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">pass</property>
- C3P0连接池
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- 数据库方言
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
-
其他配置
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
- mapping映射文件
<mapping resource="domain/Customer.hbm.xml"/>
将这些配置到applicationContext.xml中就可以了,具体配置如下:
数据库相关的都配置到C3P0的dataSource bean中
其他设置都配置到sessionFactory bean中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置客户模块 -->
<!-- 配置C3P0数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///ssh"/>
<property name="user" value="root"/>
<property name="password" value="Aa123456"/>
</bean>
<!-- action类由struts2创建的方法 action类正常编写,需要导入struts2-spring-plugin-2.3.24.jar包 -->
<bean id="customerService" class="service.CustomerServiceImpl">
<property name="customerDao" ref="customerDao" />
</bean>
<!-- 配置sessionFactory合并hibernate使用 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 加载连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载数据库方言 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载bean类映射文件 -->
<property name="mappingResources">
<list>
<value>domain/Customer.hbm.xml</value>
</list>
</property>
</bean>
<!-- customerDao 注入到customerService中 -->
<bean id="customerDao" class="dao.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- action类由spring创建的方法 直接配置action bean 其中注入service 然后在struts.xml中action的class直接填入applicationContext.xml中bean的id值
需要注意:配置action bean 需要填写属性scope="prototype" 表示多例,其他一律单例 -->
<bean id="customerAction" class="web.action.CustomerAction"
scope="prototype">
<property name="customerService" ref="customerService"></property>
</bean>
<!-- 开启事务 -->
<!-- 配置HibernatetransactionManager 需要传入datasource-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
其他实现类与测试类都不变直接测试
控制台输出
查看数据库