今天是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();
-
- }
-
- 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.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());
- }
- }
-
- @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">
-
-
- <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}" />
-
- <property name="url" value="${jdbc.url}" />
-
-
- </bean>
-
-
- <bean id="localSessionFactoryBean"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
-
- <property name="dataSource" ref="dataSource" />
-
- <property name="mappingResources">
- <array>
- <value>www/csdn/spring/hibernate/domain/Users.hbm.xml</value>
- </array>
- </property>
-
- <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>
-
-
-
- <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后结果如下