Spring 与 hibernate整合
添加Hibernate和Spring所需要的jar包还有Mysql连接的jar包
创建Dao层,Dao层实现,Model层,Service层
DAO层代码:IUserDao
- package org.zhy.demo.dao;
- import java.sql.SQLException;
- import org.zhy.demo.dao.model.UserInfo;
- /**
- * DAO层简单的增删改查方法
- * @author Administrator
- *
- */
- public interface IUserDao {
- public void saveUser(UserInfo user) throws SQLException;
- public void delUser(UserInfo user) throws SQLException;
- public void editUsre(UserInfo user) throws SQLException;
- public UserInfo getUserById(int id) throws SQLException;
- }
IUuserDao层实现(暂时空实现)
- package org.zhy.demo.dao.impl;
- import java.sql.SQLException;
- import org.zhy.demo.dao.IUserDao;
- import org.zhy.demo.dao.model.UserInfo;
- /**
- * IUserDao实现,配置完Spring及Hibernate文件后实现此类
- * @author Administrator
- *
- */
- public class IUserDaoImpl implements IUserDao {
- @Override
- public void saveUser(UserInfo user) throws SQLException {
- }
- @Override
- public void delUser(UserInfo user) throws SQLException {
- }
- @Override
- public void editUsre(UserInfo user) throws SQLException {
- }
- @Override
- public UserInfo getUserById(int id) throws SQLException {
- return null;
- }
- }
Model代码
- package org.zhy.demo.dao.model;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- @Entity
- public class UserInfo {
- private int id;
- private String name;
- private String title;
- @Id
- @GeneratedValue
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- }
下面添加Spring配置文件
在Src目录下添加SpringContext.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: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-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- ">
- <aop:config></aop:config>
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
- <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost/student"></property>
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 数据库连接池保持的最小连接数 -->
- <property name="minIdle" value="5" />
- <!-- 数据库连接池保持的最大连接数 -->
- <property name="maxIdle" value="30" />
- <!--
- 当数据库连接因为某种原因断掉之后,再重新从连接池中拿另外一个连接时实际上这个连接可能
- 已经无效,所以为了确保所拿到的连接全都有效需要在获取连接,返回连接以及连接空闲时进行
- 有效性验证 下面3个设置为ture时进行验证,默认为false
- -->
- <!-- 取得连接时是否进行有效性验证 -->
- <property name="testOnBorrow" value="true" />
- <!-- 返回连接时是否进行有效性验证 -->
- <property name="testOnReturn" value="true" />
- <!-- 连接空闲时是否进行有效性验证 -->
- <property name="testWhileIdle" value="true" />
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="datasource" />
- <!-- 注意:我用的是Annotation的方式配置的Hibernate,这里的property的name是annotatedClasses -->
- <property name="annotatedClasses">
- <list>
- <value>org.zhy.demo.dao.model.UserInfo</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <!-- 设置Hibernate方言 -->
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <!-- 是否打印sql -->
- <prop key="hibernate.show_sql">true</prop>
- <!-- 格式化sql -->
- <prop key="hibernate.format_sql">true</prop>
- <!-- 是否自动更新表 -->
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- <prop key="hibernate.current_session_context_class">thread</prop>
- <!-- 最大抓取深度,如果为0,则关闭默认的外连接抓取。建议值为0-3 -->
- <prop key="hibernate.max_fetch_depth">1</prop>
- <!-- 用于生成有助于调试的注释信息,默认为关闭 -->
- <prop key="hibernate.use_sql_comments">true</prop>
- </props>
- </property>
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <aop:config>
- <aop:pointcut id="txMethod" expression="execution(* org.zhy.demo.dao.impl.*DaoImpl.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="txMethod"/>
- </aop:config>
- <!-- AOP切面声明事务管理 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="save*" propagation="REQUIRED" /> <!-- 支持当前事务,如果执行到save开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
- <tx:method name="update*" propagation="REQUIRED" /><!-- 支持当前事务,如果执行到save开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
- <tx:method name="add*" propagation="REQUIRED" /><!-- 支持当前事务,如果执行到save开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
- <tx:method name="delete*" propagation="REQUIRED" /><!-- 支持当前事务,如果执行到save开头的任何方法时没有事务则开启一个事务 这是最常见的方式-->
- <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <!-- 支持当前事务,如果当前没有事务,就以非事务方式执行。只读 -->
- <tx:method name="get*" propagation="SUPPORTS" read-only="true"/><!-- 支持当前事务,如果当前没有事务,就以非事务方式执行。只读 -->
- <tx:method name="*" />
- </tx:attributes>
- </tx:advice>
- <bean name="userDao" class="org.zhy.demo.dao.impl.IUserDaoImpl" >
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <bean name="userService" class="org.zhy.demo.service.UserService" />
- </beans>
修改DAO实现类
- package org.zhy.demo.dao.impl;
- import java.sql.SQLException;
- import org.hibernate.SessionFactory;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- import org.zhy.demo.dao.IUserDao;
- import org.zhy.demo.dao.model.UserInfo;
- /**
- * IUserDao实现
- *
- * @author Administrator
- *
- */
- public class IUserDaoImpl implements IUserDao {
- private HibernateTemplate hibernateTemplate;
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.hibernateTemplate = new HibernateTemplate(sessionFactory);
- }
- @Override
- public void saveUser(UserInfo user) throws SQLException {
- hibernateTemplate.save(user);
- }
- @Override
- public void delUser(UserInfo user) throws SQLException {
- hibernateTemplate.delete(user);
- }
- @Override
- public void editUsre(UserInfo user) throws SQLException {
- hibernateTemplate.update(user);
- }
- @Override
- public UserInfo getUserById(int id) throws SQLException {
- UserInfo user = (UserInfo) hibernateTemplate.get(UserInfo.class, id);
- return user;
- }
- }
junit测试
- @Test
- public void saveUserTest() throws SQLException{
- ApplicationContext context= new ClassPathXmlApplicationContext("/SpringContext.xml");
- UserService service =(UserService) context.getBean("userService");
- UserInfo user = new UserInfo();
- user.setName("T`");
- user.setTitle("CSDN BLOG");
- service.addUserInfo(user);
- }