spring入门-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)

spring入门-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)

今天是spring的最后一节课,这节课老师讲了spring与hibernate整合完成增删改查的操作,这是很重要的一节课,这也是第一次真正的实现spring结合Hibernate和数据库连接上,下面是这次课的过程实现:

首先是数据库建表:采用Oracle数据库,在Scott用户里新建USERS表,


所用jar包:


实现源码如下:

Users.java

[java]  view plain  copy
  1. package www.csdn.spring.hibernate.domain;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. public class Users implements Serializable {  
  7.   
  8.     /** 
  9.      *  
  10.      */  
  11.     private static final long serialVersionUID = 1L;  
  12.   
  13.     private Integer id;  
  14.     private String name;  
  15.     private Date regTime;  
  16.   
  17.     public Users() {  
  18.         super();  
  19.         // TODO Auto-generated constructor stub  
  20.     }  
  21.   
  22.     public Users(Integer id, String name, Date regTime) {  
  23.         super();  
  24.         this.id = id;  
  25.         this.name = name;  
  26.         this.regTime = regTime;  
  27.     }  
  28.   
  29.     public Integer getId() {  
  30.         return id;  
  31.     }  
  32.   
  33.     public void setId(Integer id) {  
  34.         this.id = id;  
  35.     }  
  36.   
  37.     public String getName() {  
  38.         return name;  
  39.     }  
  40.   
  41.     public void setName(String name) {  
  42.         this.name = name;  
  43.     }  
  44.   
  45.     public Date getRegTime() {  
  46.         return regTime;  
  47.     }  
  48.   
  49.     public void setRegTime(Date regTime) {  
  50.         this.regTime = regTime;  
  51.     }  
  52.   
  53.     @Override  
  54.     public String toString() {  
  55.         return "Users [id=" + id + ", name=" + name + ", regTime=" + regTime  
  56.                 + "]";  
  57.     }  
  58.       
  59. }  

Users.hbm.xml

[html]  view plain  copy
  1. <!DOCTYPE hibernate-mapping PUBLIC   
  2.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  4. <hibernate-mapping package="www.csdn.spring.hibernate.domain">  
  5.     <class name="Users" table="USERS" schema="SCOTT">  
  6.         <id name="id" column="ID">  
  7.             <generator class="sequence">  
  8.                 <param name="sequence">USERS_SEQ</param>  
  9.             </generator>  
  10.         </id>  
  11.   
  12.         <property name="name" type="string" column="NAME" />  
  13.         <property name="regTime" type="timestamp" column="REGTIME" />  
  14.       
  15.     </class>  
  16.   
  17. </hibernate-mapping>  

UsersDao.java

[java]  view plain  copy
  1. package www.csdn.spring.hibernate.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import www.csdn.spring.hibernate.domain.Users;  
  6.   
  7. public interface UsersDao{  
  8.     public void save(Users entity);  
  9.     public void deleteById(Class clazz,Integer id);  
  10.     public List<Users> getObjects(Class clazz);  
  11.     public void update(Users entity);  
  12. }  

UsersDaoImpl.java

[java]  view plain  copy
  1. package www.csdn.spring.hibernate.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.orm.hibernate3.HibernateTemplate;  
  6.   
  7. import www.csdn.spring.hibernate.domain.Users;  
  8.   
  9. public class UsersDaoImpl implements UsersDao{  
  10.     // 封装模版类对象  
  11.     private HibernateTemplate hibernateTemplate;  
  12.   
  13.     // 注入  
  14.     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {  
  15.         this.hibernateTemplate = hibernateTemplate;  
  16.         }  
  17.       
  18.     @Override  
  19.     public void save(Users entity) {  
  20.         hibernateTemplate.save(entity);  
  21.           
  22.     }  
  23.     @Override  
  24.     public List<Users> getObjects(Class clazz) {  
  25.           
  26.         return hibernateTemplate.find("from "+clazz.getName());  
  27.     }  
  28.   
  29.     @Override  
  30.     public void deleteById(Class clazz,Integer id) {  
  31.         //hibernateTemplate.delete(hibernateTemplate.get(clazz.getName(), id));  
  32.         hibernateTemplate.bulkUpdate("delete from "+clazz.getName()+" where id="+id);  
  33.     }  
  34.   
  35.     @Override  
  36.     public void update(Users entity) {  
  37.         hibernateTemplate.update(entity);  
  38.           
  39.     }  
  40.   
  41. }  
UserTest.java

[java]  view plain  copy
  1. package www.csdn.spring.hibernate.dao;  
  2.   
  3.   
  4.   
  5. import java.util.Date;  
  6. import java.util.List;  
  7.   
  8. import org.junit.Test;  
  9. import org.springframework.context.ApplicationContext;  
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  11.   
  12. import www.csdn.spring.hibernate.domain.Users;  
  13.   
  14. public class UserTest {  
  15.     //保存  
  16.     @Test  
  17.     public void save(){  
  18.         ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml");  
  19.         UsersDao usersdao=ac.getBean("usersDaoImpl",UsersDao.class);  
  20.         usersdao.save(new Users(null,"chrp999999999",new Date()));  
  21.           
  22.             System.out.println(usersdao.getClass());  
  23.           
  24.     }  
  25.     //获取所有  
  26.     @Test  
  27.     public void getObjects(){  
  28.         ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml");  
  29.         UsersDao usersdao=ac.getBean("usersDaoImpl",UsersDao.class);  
  30.           
  31.         List<Users> user=usersdao.getObjects(Users.class);  
  32.         for(Users u:user){  
  33.             System.out.println(u.toString());  
  34.         }  
  35.     }  
  36.     //根据id删除  
  37.         @Test  
  38.         public void delete(){  
  39.             ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml");  
  40.             UsersDao usersdao=ac.getBean("usersDaoImpl",UsersDao.class);  
  41.             usersdao.deleteById(Users.class,5);  
  42.               
  43.               
  44.                 System.out.println(usersdao.getClass());  
  45.               
  46.         }  
  47.         //更新  
  48.         @Test  
  49.         public void update(){  
  50.             ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml");  
  51.             UsersDao usersdao=ac.getBean("usersDaoImpl",UsersDao.class);  
  52.             usersdao.update(new Users(2,"deep",new Date()));  
  53.               
  54.               
  55.             System.out.println(usersdao.getClass());  
  56.               
  57.         }  
  58.           
  59. }  


jdbc.properties

[plain]  view plain  copy
  1. jdbc.driverClassName=oracle.jdbc.driver.OracleDriver  
  2. jdbc.username=scott  
  3. jdbc.password=tiger  
  4. jdbc.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl  

applicationContext.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context.xsd">  
  9.   
  10.     <!-- 导入spring-dao的文件 -->  
  11.     <import resource="spring.xml"/>  
  12.     <import resource="spring-dao.xml" />  
  13.       
  14.       
  15.     <!-- 分散配置解析 -->  
  16.     <context:property-placeholder location="jdbc.properties" />  
  17.   
  18. </beans>  


spring.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.            http://www.springframework.org/schema/context  
  10.            http://www.springframework.org/schema/context/spring-context.xsd  
  11.             http://www.springframework.org/schema/aop  
  12.            http://www.springframework.org/schema/aop/spring-aop.xsd  
  13.            http://www.springframework.org/schema/tx  
  14.            http://www.springframework.org/schema/tx/spring-tx.xsd">  
  15.     <!-- 数据库连接的数据源 -->  
  16.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  17.         <!-- 数据库连接驱动 -->  
  18.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  19.         <!-- 连接的用户名 -->  
  20.         <property name="username" value="${jdbc.username}" />  
  21.         <!-- 连接的用户密码 -->  
  22.         <property name="password" value="${jdbc.password}" />  
  23.         <!-- 连接的url地址 -->  
  24.         <property name="url" value="${jdbc.url}" />  
  25.         <!--数据库的连接的最小值  -->  
  26.         <!--数据库的连接的最大值  -->  
  27.     </bean>  
  28.     <!-- 怎么与hibernate整合的 -->  
  29.     <!-- sessionFactory工厂 -->  
  30.     <bean id="localSessionFactoryBean"  
  31.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  32.         <!-- 数据库连接的数据源 -->  
  33.         <property name="dataSource" ref="dataSource" />  
  34.         <!-- hibernate的映射文件配置 -->  
  35.         <property name="mappingResources">  
  36.             <array>  
  37.                 <value>www/csdn/spring/hibernate/domain/Users.hbm.xml</value>  
  38.             </array>  
  39.         </property>  
  40.         <!-- hibernate的属性配置 -->  
  41.         <property name="hibernateProperties">  
  42.             <props>  
  43.                 <prop key="show_sql">true</prop>  
  44.                 <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory  
  45.                 </prop>  
  46.             </props>  
  47.         </property>  
  48.     </bean>  
  49.   
  50.   
  51.    <!-- hibernate封装的模版类 -->  
  52.    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  53.      <property name="sessionFactory" ref="localSessionFactoryBean"/>  
  54.    </bean>  
  55.   
  56.       
  57.   
  58.    <!-- 事务管理器 -->  
  59.    <bean  id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  60.        <property name="sessionFactory" ref="localSessionFactoryBean"/>  
  61.    </bean>  
  62.      
  63.      
  64.    <!-- 事务的通知-->  
  65.    <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">  
  66.        <!-- 事务的属性 -->  
  67.        <tx:attributes>  
  68.            <!-- 事务的具体执行方法 -->  
  69.            <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>  
  70.            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>  
  71.            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>  
  72.            <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>  
  73.        </tx:attributes>  
  74.    </tx:advice>  
  75.      
  76.   
  77.    <!-- 切面 -->  
  78.     <!-- <aop:config>  
  79.         <aop:pointcut expression="execution(*..Service*.*(..))" id="mycut" />  
  80.         <aop:advisor advice-ref="txAdvice" pointcut-ref="mycut" />  
  81.     </aop:config>  
  82.     -->  
  83.   
  84. </beans>  

spring-dao.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.      
  7.     
  8.     <bean id="usersDaoImpl" class="www.csdn.spring.hibernate.dao.UsersDaoImpl">  
  9.          <property name="hibernateTemplate" ref="hibernateTemplate">  
  10.     </property>  
  11.     </bean>  
  12.    
  13. </beans>  
到此为止简单的spring+Hibernate完成增删改查就实现了,运行测试类UserTest.java后结果如下


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值