今天是spring的最后一节课,这节课老师讲了spring与hibernate整合完成增删改查的操作,这是很重要的一节课,这也是第一次真正的实现spring结合Hibernate和数据库连接上,下面是这次课的过程实现:
首先是数据库建表:采用Oracle数据库,在Scott用户里新建USERS表,
所用jar包:
实现源码如下:
Users.java
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); } }
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>
<?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后结果如下