spring與hibernate整合完成增刪改查的操作(封裝HibernateTemplate模版類對象)
今天是spring的最後一節課,這節課老師講了spring與hibernate整合完成增刪改查的操作,這是很重要的一節課,這也 是第一次真正的實現spring結合Hibernate和數據庫連接上,下面是這次課的過程實現:
首先是數據庫建表:采用 Oracle數據庫,在Scott用戶裡新建USERS表,
所用jar包:
實現源碼如下:
Users.java
package www.csdn.spring.hibernate.domain; import java.io.Serializable; import java.util.Date; public class Users implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer id; private String name; private Date regTime; public Users() { super(); // TODO Auto-generated constructor stub } public Users(Integer id, String name, Date regTime) { super(); this.id = id; this.name = name; this.regTime = regTime; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } @Override public String toString() { return "Users [id=" + id + ", name=" + name + ", regTime=" + regTime + "]"; } }
Users.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="www.csdn.spring.hibernate.domain"> <class name="Users" table="USERS" schema="SCOTT"> <id name="id" column="ID"> <generator class="sequence"> <param name="sequence">USERS_SEQ</param> </generator> </id> <property name="name" type="string" column="NAME" /> <property name="regTime" type="timestamp" column="REGTIME" /> </class> </hibernate-mapping>
UsersDao.java
package www.csdn.spring.hibernate.dao; import java.util.List; import www.csdn.spring.hibernate.domain.Users; public interface UsersDao{ public void save(Users entity); public void deleteById(Class clazz,Integer id); public List<Users> getObjects(Class clazz); public void update(Users entity); }
UsersDaoImpl.java
package www.csdn.spring.hibernate.dao; import java.util.List; import org.springframework.orm.hibernate3.HibernateTemplate; import www.csdn.spring.hibernate.domain.Users; public class UsersDaoImpl implements UsersDao{ // 封裝模版類對象 private HibernateTemplate hibernateTemplate; // 注入 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } @Override public void save(Users entity) { hibernateTemplate.save(entity); } @Override public List<Users> getObjects(Class clazz) { return hibernateTemplate.find("from "+clazz.getName()); } @Override public void deleteById(Class clazz,Integer id) { //hibernateTemplate.delete(hibernateTemplate.get(clazz.getName(), id)); hibernateTemplate.bulkUpdate("delete from "+clazz.getName()+" where id="+id); } @Override public void update(Users entity) { hibernateTemplate.update(entity); } }
UserTest.java
package www.csdn.spring.hibernate.dao; import java.util.Date; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import www.csdn.spring.hibernate.domain.Users; public class UserTest { //保存 @Test public void save(){ ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml"); UsersDao usersdao=ac.getBean("usersDaoImpl",UsersDao.class); usersdao.save(new Users(null,"chrp999999999",new Date())); System.out.println(usersdao.getClass()); } //獲取所有 @Test public void getObjects(){ ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml"); UsersDao usersdao=ac.getBean("usersDaoImpl",UsersDao.class); List<Users> user=usersdao.getObjects(Users.class); for(Users u:user){ System.out.println(u.toString()); } } //根據id刪除 @Test public void delete(){ ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml"); UsersDao usersdao=ac.getBean("usersDaoImpl",UsersDao.class); usersdao.deleteById(Users.class,5); System.out.println(usersdao.getClass()); } //更新 @Test public void update(){ ApplicationContext ac=new ClassPathXmlApplicationContext("app*.xml"); UsersDao usersdao=ac.getBean("usersDaoImpl",UsersDao.class); usersdao.update(new Users(2,"deep",new Date())); System.out.println(usersdao.getClass()); } }
jdbc.properties
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver jdbc.username=scott jdbc.password=tiger jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 導入spring-dao的文件 --> <import resource="spring.xml" /> <import resource="spring-dao.xml" /> <!-- 分散配置解析 --> <context:property-placeholder location="jdbc.properties" /> </beans>
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 數據庫連接的數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 數據庫連接驅動 --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <!-- 連接的用戶名 --> <property name="username" value="${jdbc.username}" /> <!-- 連接的用戶密碼 --> <property name="password" value="${jdbc.password}" /> <!-- 連接的url地址 --> <property name="url" value="${jdbc.url}" /> <!--數據庫的連接的最小值 --> <!--數據庫的連接的最大值 --> </bean> <!-- 怎麼與hibernate整合的 --> <!-- sessionFactory工廠 --> <bean id="localSessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 數據庫連接的數據源 --> <property name="dataSource" ref="dataSource" /> <!-- hibernate的映射文件配置 --> <property name="mappingResources"> <array> <value>www/csdn/spring/hibernate/domain/Users.hbm.xml</value> </array> </property> <!-- hibernate的屬性配置 --> <property name="hibernateProperties"> <props> <prop key="show_sql">true</prop> <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory </prop> </props> </property> </bean> <!-- hibernate封裝的模版類 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="localSessionFactoryBean" /> </bean> <!-- 事務管理器 --> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="localSessionFactoryBean" /> </bean> <!-- 事務的通知--> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"> <!-- 事務的屬性 --> <tx:attributes> <!-- 事務的具體執行方法 --> <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面 --> <!-- <aop:config> <aop:pointcut expression="execution(*..Service*.*(..))" id="mycut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="mycut" /> </aop:config> --> </beans>
spring-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="usersDaoImpl" class="www.csdn.spring.hibernate.dao.UsersDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"> </property> </bean> </beans>
到此為止簡單的spring+Hibernate完成增刪改查就實現了,運行測試類UserTest.java後結果如下