首页 > 代码库 > Spring+Hibernate 整合_1

Spring+Hibernate 整合_1

目录结构:

 

 

 1 package com.helen.model; 2  3 import javax.persistence.Entity; 4 import javax.persistence.Id; 5 import javax.persistence.Table; 6  7 import org.springframework.stereotype.Component; 8  9 @Entity10 @Table(name = "user12")11 @Component12 public class User {13     private int userId;14 15     @Id 16 17     public int getUserId() {18         return userId;19     }20 21     public void setUserId(int userId) {22         this.userId = userId;23     }24 25     private String username;26     private String password;27 28     public String getUsername() {29         return username;30     }31 32     public void setUsername(String username) {33         this.username = username;34     }35 36     public String getPassword() {37         return password;38     }39 40     public void setPassword(String password) {41         this.password = password;42     }43 44     @Override45     public String toString() {46         return "User [username=" + username + ", password=" + password + "]";47     }48 49 }

 

1 package com.helen.dao;2 3 import com.helen.model.User;4 5 public interface UserDAO {6     void addUser(User user);7 }

 

 

 1 package com.helen.dao; 2  3 import javax.annotation.Resource; 4  5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.Transaction; 8 import org.springframework.stereotype.Component; 9 import com.helen.model.User;10 11 @Component("userDao")12 public class UserDAOImpl implements UserDAO {13 14     private SessionFactory sessionFactory;15 16     public SessionFactory getSessionFactory() {17         return sessionFactory;18     }19 20     @Resource21     public void setSessionFactory(SessionFactory sessionFactory) {22         this.sessionFactory = sessionFactory;23     }24 25     public void addUser(User user) {26 27         Session s = null;28         Transaction tx = null;29         try {30             s = sessionFactory.openSession();31             tx = s.beginTransaction();32             s.save(user);33             tx.commit();34             System.out.println("add!\n" + user);35         } catch (Exception e) {36             tx.rollback();37             System.out.println("rollback");38         } finally {39             if (s != null) {40                 s.close();41             }42         }43 44     }45 46 }

 

 1 package com.helen.service; 2  3 import javax.annotation.Resource; 4 import org.springframework.stereotype.Component; 5  6 import com.helen.dao.UserDAO; 7 import com.helen.model.User; 8  9 @Component10 public class UserService {11     private UserDAO userDao;12 13     public UserDAO getUserDao() {14         return userDao;15     }16 17     @Resource(name = "userDao")18     public void setUserDao(UserDAO userDao) {19         this.userDao = userDao;20     }21 22     public void addUserService(User user) {23         userDao.addUser(user);24     }25 26 }

Test.java:

 1 package com.helen.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 import com.helen.model.User; 7 import com.helen.service.UserService; 8  9 10 11 public class Test {12     public static void main(String arg[]){13 14     ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");15     UserService userService = context.getBean("userService", UserService.class);16     User user=(User) context.getBean("user");17     user.setUserId(1223);18     user.setUsername("helen");19     user.setPassword("123123");20     userService.addUserService(user);21     }22 23 }

 

beans.xml:

 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" xmlns:aop="http://www.springframework.org/schema/aop" 4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 9 10     <context:annotation-config />11     <context:component-scan base-package="com.helen" />12 13     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"    destroy-method="close">14     15         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />16         <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />17         <property name="username" value="system" />18         <property name="password" value="123456" />19         <!-- 连接池启动时的初始值 -->20         <property name="initialSize" value="1" />21         <!-- 连接池的最大值 -->22         <property name="maxActive" value="300" />23         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->24         <property name="maxIdle" value="2" />25         <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->26         <property name="minIdle" value="1" />27         28     </bean>29 30     <bean id="sessionFactory"31 32         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">33         <property name="dataSource" ref="dataSource" />34         <property name="annotatedClasses">35             <list>36                 <value>com.helen.model.User</value>37             </list>38         </property>39         <property name="hibernateProperties">40             <props>41                 <prop key="hibernate.dialect">42                     org.hibernate.dialect.Oracle10gDialect43                 </prop>44                 <prop key="hibernate.show_sql">true</prop>45                 <prop key="hbm2ddl.auto">update</prop>46             </props>47         </property>48         49     </bean>50     51 </beans>

 

 

dataSource 修改为:

1 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver2 jdbc.url=jdbc:oracle:thin:@localhost:1521:xe3 jdbc.username=system4 jdbc.password=123456

 

beans.xml:

 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" xmlns:aop="http://www.springframework.org/schema/aop" 4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 9 10     <context:annotation-config />11     <context:component-scan base-package="com.helen" />12 13     <!-- <context:property-placeholder location="classpath:jdbc.properties" /> -->14     <bean15         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">16         <property name="locations">17             <value>classpath:jdbc.properties</value>18         </property>19     </bean>20 21     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"22         destroy-method="close">23         <property name="driverClassName" value="${jdbc.driverClassName}" />24         <property name="url" value="${jdbc.url}" />25         <property name="username" value="${jdbc.username}" />26         <property name="password" value="${jdbc.password}" />27         <!-- 连接池启动时的初始值 -->28         <property name="initialSize" value="1" />29         <!-- 连接池的最大值 -->30         <property name="maxActive" value="2" />31         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->32         <property name="maxIdle" value="2" />33         <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->34         <property name="minIdle" value="1" />35     </bean>36 37     <bean id="sessionFactory"38 39         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">40         <property name="dataSource" ref="dataSource" />41         <property name="annotatedClasses">42             <list>43                 <value>com.helen.model.User</value>44             </list>45         </property>46         <property name="hibernateProperties">47             <props>48                 <prop key="hibernate.dialect">49                     org.hibernate.dialect.Oracle10gDialect50                 </prop>51                 <prop key="hibernate.show_sql">true</prop>52                 <prop key="hbm2ddl.auto">update</prop>53             </props>54         </property>55     </bean>56 57   60 61 </beans>

 

Spring+Hibernate 整合_1