sh_Spring整合Hibernate
来源:互联网 发布:js div height 编辑:程序博客网 时间:2024/06/05 03:38
分别介绍了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;}@Overridepublic void save(User user) {this.hibernateTemplate.save(user);}@Overridepublic void update(User user) {this.hibernateTemplate.update(user);}@Overridepublic void delete(User user) {this.hibernateTemplate.delete(user);}@Overridepublic User findById(Integer uid) {return this.hibernateTemplate.get(User.class, uid);}@Overridepublic 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{//引入UserDaoprivate UserDao userDao;//提供set方法public void setUserDao(UserDao userDao) {this.userDao = userDao;}@Overridepublic void addUser(User user) {this.userDao.save(user);}@Overridepublic void updateUser(User user) {this.userDao.update(user);}@Overridepublic void deleteUser(User user) {this.userDao.delete(user);}@Overridepublic User findUserById(Integer uid) {return this.userDao.findById(uid);}@Overridepublic 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="classpath:hibernate.cfg.xml"></property> </bean> <!-- #2 hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <!-- 需要使用sessionFactory获得session,操作PO对象 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- #3 dao --> <bean id="userDao" class="cn.tgb.dao.impl.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- #4 service --> <bean id="userService" class="cn.tgb.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- #5 事务管理 --> <!-- #5.1事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 必须确定sessionFactory。从而获得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获得UserServiceUserService userService = (UserService) applicationContext.getBean("userService");//4.执行添加用户操作userService.addUser(user);}}
分析总结:
这张思维导图大概介绍了一下配置文件的配置内容和加载过程,其中蓝色的云朵部分是Spring整合Hibernate的内容。程序运行时,通过加载配置文件,便将程序中所有的配置信息都加载了。如果有个扩展修改什么的也方便。日后会继续介绍它们两个与Struts的整合。
1 1
- sh_Spring整合Hibernate
- struts整合spring整合hibernate
- 框架整合____Spring整合hibernate
- spring 整合hibernate整合方式
- 整合 struts spring hibernate
- struts+spring+hibernate整合
- struts,hibernate,spring整合
- struts+spring+hibernate整合
- struts+spring+hibernate整合
- Spring+hibernate+DWR整合
- webwork+spring+hibernate整合
- 整合Jboss和Hibernate
- 整合Jboss和Hibernate
- webwork+spring+hibernate整合
- Spring整合Hibernate
- Struts hibernate Spring 整合
- Struts hibernate Spring 整合
- Spring整合Hibernate
- unity 加载c++ dll文件提示DllNotFoundException的解决办法
- POJ 2114 Boatherds (树上点分治)
- 抓包工具———wireshark学习篇:1
- ARM 浮点运算
- 十进制和其他进制的相互转化(自己写的)
- sh_Spring整合Hibernate
- properties属性文件
- 23.Merge K Sorted Lists
- 抓包工具---wireshark 学习篇:0
- 第一天的日记
- Volatile的陷阱
- Hibernate QBC与HQL优缺点
- Poj 3259 Wormholes(bellman_ford判负环)
- BMP和颜色的关系总结