Spring整合Hibernate操作
1.创建web Project
2.添加jar包
Spring的jar包+Hibernate的jar包+数据库连接jar包
3.书写配置文件
3.1 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd "> <!--1.加载数据库的属性配置文件 --> <context:property-placeholder location="classpath:dbinfo.properties" ></context:property-placeholder> <!-- 2.配置c3p0数据源 --> <bean name="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!-- 3.将Hibernate的sessionFactory对象交给Spring来创建管理维护 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 3.1 注入数据源连接池信息 --> <property name="dataSource" ref="ds"></property> <!-- 3.2 hibernate的额外配置 (可选配置)--> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 隔离级别 --> <prop key="hibernate.connection.isolation">4</prop> <!-- 目的:要使用getCurrentSession()获取当前线程绑定的Session,必须要做如下配置:必须要使用Spring提供的SpringSessionContext--> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> </props> </property> <!-- 3.3 加载映射文件(不能重复加载) --> <property name="mappingLocations" value="classpath:com/xx/domain/*.hbm.xml"></property> </bean> <!-- 2.配置dao层 --> <bean name="userDao" class="com.xx.dao.impl.UserDaoimpl"> <!-- 注入sessionFatory对象 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 3.配置service层 --> <bean name="userService" class="com.xx.service.impl.UserServiceimpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- 4.配置事务管理 --> <bean name="tm" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
<!-- 5.开启事务 (两种方式选一)--> <!-- 方式一:注解开启事务 --> <tx:annotation-driven transaction-manager="tm"/>
<!-- 方式二:XML配置开启事务 --> <!-- 配置通知--> <tx:advice id="txAdvice" transaction-manager="tm"> <tx:attributes> <tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice> <!-- 配置切面 --> <aop:config proxy-target-class="true">
<!-- 配置切点 --> <aop:pointcut expression="execution(* com.itqf.service.impl.*Serviceimpl.*(..))" id="pt"/>
<!-- 织入 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>
3.2 web.xml(重点)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>SSH_CRM</display-name> <!--监听web程序:在程序启动时就加载applicationContext.xml文件--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!--加载applicationContext.xml文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext2.xml</param-value>
</context-param> </web-app>
4.创建持久化类
package com.xx.domain;
public class User{ private Integer id; private String name; private Integer age; //省略set/get方法 }
5.创建持久化类对应的映射文件(持久化类名.hbm.xml)
<?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 package="com.xx.domain"> <class name="User" table="user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="age"></property> </class> </hibernate-mapping>
6.配置service层和dao层
6.1 dao层实现类
//dao层实现类需要继承HibernateDaoSupport 在代码中使用getHibernateTemplate() public class CustomerDaoimpl extends HibernateDaoSupport implements CustomerDao{ @Override public void insertCustomer(User user) { getHibernateTemplate().save(customer); } }
6.2 service层实现类
//方式一:注解配置事务 @Transactional public class UserServiceimpl implements UserService{ private UserDao userDao; public void setUserDao (UserDao userDao) { this.userDao= userDao; } public void save(User user){ userDao.insertUser(user); } } //方式二:xml配置事务 public class UserServiceimpl implements UserService{ private UserDao userDao; public void setUserDao (UserDao userDao) { this.userDao= userDao; } public void save(User user){ userDao.insertUser(user); } }
7.测试
public class Test1{ @Resource(name="userService") private UserService userService; public void test1(){ User user = new User(); user.setId(1); user.setName("哈哈"); user.setAge(123); userService.save(user); } }