首页 > 代码库 > sh_Spring整合Hibernate
sh_Spring整合Hibernate
分别介绍了Sping和Hibernate,以下是将它们整合到一块去了。
一、Hibernate内容
1.创建PO类。
package cn.tgb.domain; //User实体 public class User { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2.编写User.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> <class name="cn.tgb.domain.User" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="username"></property> <property name="password"></property> </class> </hibernate-mapping>
3.编写Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- #1核心四项 --> <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=hibernate02</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password">123456</property> <!-- #2设置方言 --> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- #3 sql --> <!-- * 输出sql--> <property name="hibernate.show_sql">true</property> <!-- * 格式化sql --> <property name="hibernate.format_sql">true</property> <!-- * 是否创建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- #4整合c3p0 --> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!-- #5 web项目6.0 取消bean校验 --> <property name="javax.persistence.validation.mode">none</property> <!-- #6加入映射文件 --> <mapping resource="cn/tgb/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
二、Spring内容
1.Dao实现类:使用HibernateTemplate对PO类进行操作。
package cn.tgb.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.HibernateTemplate; import cn.tgb.dao.UserDao; import cn.tgb.domain.User; public class UserDaoImpl implements UserDao{ //使用hibernateTemplate对PO类进行操作 private HibernateTemplate hibernateTemplate; //提供set方法 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } @Override public void save(User user) { this.hibernateTemplate.save(user); } @Override public void update(User user) { this.hibernateTemplate.update(user); } @Override public void delete(User user) { this.hibernateTemplate.delete(user); } @Override public User findById(Integer uid) { return this.hibernateTemplate.get(User.class, uid); } @Override public List<User> findAllUser() { return this.hibernateTemplate.find("from User"); } }
2.Service实现类:直接使用dao层,通过spring tx管理事务
package cn.tgb.service.impl; import java.util.List; import cn.tgb.dao.UserDao; import cn.tgb.domain.User; import cn.tgb.service.UserService; public class UserServiceImpl implements UserService{ //引入UserDao private UserDao userDao; //提供set方法 public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void addUser(User user) { this.userDao.save(user); } @Override public void updateUser(User user) { this.userDao.update(user); } @Override public void deleteUser(User user) { this.userDao.delete(user); } @Override public User findUserById(Integer uid) { return this.userDao.findById(uid); } @Override public List<User> findAllUser() { return this.userDao.findAllUser(); } }
<?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"> <!-- #1配置sessionFactory, 通过LocalSessionFactoryBean载入配置获得SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- #1.1确定配置文件的位置 --> <property name="configLocation" value=http://www.mamicode.com/"classpath:hibernate.cfg.xml">>
从而获得session,再获得事务 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- #5.2事务通知(增强),对指定目标方法进行增强 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*"/> <tx:method name="update*"/> <tx:method name="delete*"/> <tx:method name="find*" read-only="true"/> </tx:attributes> </tx:advice> <!-- #5.3使用aop生成代理,进行增强 --> <aop:config> <!-- 确定切入点 --> <aop:pointcut expression="execution(* cn.tgb.service..*.*(..))" id="txPointCut"/> <!-- 声明切面 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> </beans>
至此,Spring和Hibernate就算整合好了。以下编写一个測试类測试下。
package cn.tgb.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.tgb.domain.User; import cn.tgb.service.UserService; public class TestApp { public static void main(String[] args) { //1.创建建用户 User user = new User(); user.setUsername("jiangxiao"); user.setPassword("123"); //2.获取Sping配置文件 String xmlPathString="applicationContext.xml"; ApplicationContext applicationContext= new ClassPathXmlApplicationContext(xmlPathString); //3.通过Ioc获得UserService UserService userService = (UserService) applicationContext.getBean("userService"); //4.运行加入用户操作 userService.addUser(user); } }
分析总结:
这张思维导图大概介绍了一下配置文件的配置内容和载入过程,当中蓝色的云朵部分是Spring整合Hibernate的内容。
程序执行时。通过载入配置文件,便将程序中全部的配置信息都载入了。假设有个扩展改动什么的也方便。
日后会继续介绍它们两个与Struts的整合。
sh_Spring整合Hibernate