首页 > 代码库 > Spring整合hibernate4

Spring整合hibernate4

1:通过maven添加需要的jar包

 1 <dependencies> 2       <dependency> 3           <groupId>org.springframework</groupId> 4           <artifactId>spring-web</artifactId> 5           <version>3.2.9.RELEASE</version> 6       </dependency> 7        8       <dependency> 9           <groupId>org.aspectj</groupId>10           <artifactId>aspectjrt</artifactId>11           <version>1.8.1</version>12       </dependency>13 14       <dependency>15           <groupId>org.aspectj</groupId>16           <artifactId>aspectjweaver</artifactId>17           <version>1.8.1</version>18       </dependency>19       20       <dependency>21           <groupId>mysql</groupId>22           <artifactId>mysql-connector-java</artifactId>23           <version>5.1.31</version>24       </dependency>25 26       <dependency>27           <groupId>org.hibernate</groupId>28           <artifactId>hibernate-core</artifactId>29           <version>4.2.0.Final</version>30       </dependency>31 32       <dependency>33           <groupId>org.apache.openejb</groupId>34           <artifactId>commons-dbcp-all</artifactId>35           <version>1.3</version>36       </dependency>37 38       <dependency>39           <groupId>org.springframework</groupId>40           <artifactId>spring-orm</artifactId>41           <version>3.2.9.RELEASE</version>42       </dependency>43 </dependencies>

 

2:配置实体类与数据库关联

 1 @Entity 2 @Table(name="user") 3 public class User { 4     @Id 5     @GenericGenerator(name="native",strategy="native") 6     @GeneratedValue(generator="native") 7     private int id; 8      9     @Column(name="username")10     private String name;11 }

 

3:dao

 1 public interface UserDao { 2     public User findUserById(int id); 3 } 4  5 @Repository 6 public class UserDaoImpl implements UserDao { 7     @Autowired 8     private SessionFactory sessionFactory; 9     10     public User findUserById(int id) {11         Session session = sessionFactory.openSession();12         Transaction ts = session.beginTransaction();13         14         User user = (User)session.get(User.class, id);15         session.delete(user);16         17         ts.commit();18         session.close();19         return user;20     }21 }

 

4:service

 1 public interface UserService { 2     public User getUserById(int id); 3 } 4  5 @Service 6 public class UserServiceImpl implements UserService { 7     @Autowired 8     private UserDao userDao; 9     10     public User getUserById(int id) {11         return userDao.findUserById(id);12     }13 }

 

5:测试代码

 1 @Component("client") 2 public class Client { 3     @Autowired 4     private UserService userService; 5  6     public static void main(String[] args) { 7         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 8          9         Client st = (Client)ac.getBean("client");10         UserService userService = st.userService;11         User user = userService.getUserById(1);12         13         System.out.println(user.getName());14     }15 }

 

6:配置文件applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3     xmlns="http://www.springframework.org/schema/beans" 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5     xmlns:p="http://www.springframework.org/schema/p" 6     xmlns:context="http://www.springframework.org/schema/context" 7     xmlns:aop="http://www.springframework.org/schema/aop" 8     xmlns:tx="http://www.springframework.org/schema/tx" 9     xmlns:jpa="http://www.springframework.org/schema/data/jpa"10     xmlns:cache="http://www.springframework.org/schema/cache"11     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd12                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd13                         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd14                         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd15                         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd16                         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">17     18     <context:component-scan base-package="dao"/>19     <context:component-scan base-package="service"/>20     <context:component-scan base-package="test"/>21     22     <context:property-placeholder location="classpath:dbcp.properties"/>23     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">24         <property name="driverClassName" value="${driverClassName}" />25         <property name="url" value="${url}" />26         <property name="username" value="${mysqlusername}" />27         <property name="password" value="${mysqlpassword}" />28         <property name="maxActive" value="${maxActive}" />29         <property name="maxIdle" value="${maxIdle}" />30         <property name="minIdle" value="${minIdle}" />31         <property name="maxWait" value="${maxWait}" />32         <property name="initialSize" value="${initialSize}" />33         <property name="logAbandoned" value="${logAbandoned}" />34         <property name="removeAbandoned" value="${removeAbandoned}" />35         <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />36         <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />37         <property name="numTestsPerEvictionRun" value="${numTestsPerEvictionRun}" />38     </bean>39     40     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">41         <property name="dataSource" ref="dataSource" />42         43         <property name="hibernateProperties">44             <props>45                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>46                 <prop key="hibernate.show_sql">true</prop>47             </props>48         </property>49         50         <property name="packagesToScan">51             <list>52                 <value>po</value>53             </list>54         </property>55     </bean>56 </beans>

 

7:数据库连接池配置dbcp.properties

url=jdbc:mysql://localhost:3306/testdriverClassName=com.mysql.jdbc.Drivermysqlusername=rootmysqlpassword=rootmaxActive=8minIdle=3maxIdle=8maxWait=40000initialSize=2logAbandoned=trueremoveAbandoned=true removeAbandonedTimeout=30timeBetweenEvictionRunsMillis=10000numTestsPerEvictionRun=8