首页 > 代码库 > SSH程序框架之Spring与HIbernate整合

SSH程序框架之Spring与HIbernate整合

spring整合hibernate 有两种方式 1、注解方式 2、xml方式实现

  Spring整合Hibernate有什么好处?

  1、由IOC容器来管理Hibernate的SessionFactory

  2、让Hibernate使用上Spring的声明式事务

1.xml方式:

   大概架构:                                 

                                  技术分享

 

我们将会创建一个账户类,实现添加的功能 

首先先添加Spring 和 HIbernate的jar包节点:

 

 1  <dependencies>
 2         <dependency>
 3             <groupId>junit</groupId>
 4             <artifactId>junit</artifactId>
 5             <version>4.3</version>
 6             <scope>test</scope>
 7         </dependency>
 8 
 9         <!--jta的jar包-->
10         <dependency>
11             <groupId>javax.transaction</groupId>
12             <artifactId>jta</artifactId>
13             <version>1.1</version>
14         </dependency>
15 
16         <!--mysql数据库驱动-->
17         <dependency>
18             <groupId>org.wisdom-framework</groupId>
19             <artifactId>mysql-connector-java</artifactId>
20             <version>5.1.34_1</version>
21         </dependency>
22 
23         <dependency>
24             <groupId>org.hibernate</groupId>
25             <artifactId>hibernate-core</artifactId>
26             <version>5.0.6.Final</version>
27         </dependency>
28 
29         <!--以上是HIbernate的jar包配置-->
30 
31         <!--下面是Spring相关的jar包配置-->
32         <!--c3p0-->
33         <dependency>
34             <groupId>com.mchange</groupId>
35             <artifactId>c3p0</artifactId>
36             <version>0.9.5.2</version>
37         </dependency>
38 
39         <!--AOP的相关jar-->
40         <dependency>
41             <groupId> org.aspectj</groupId >
42             <artifactId> aspectjweaver</artifactId >
43             <version> 1.8.7</version >
44         </dependency>
45 
46         <dependency>
47             <groupId>org.springframework</groupId>
48             <artifactId>spring-context</artifactId>
49             <version>4.2.0.RELEASE</version>
50         </dependency>
51 
52         <!--Spring-ORM-->
53         <dependency>
54             <groupId>org.springframework</groupId>
55             <artifactId>spring-orm</artifactId>
56             <version> 4.2.2.RELEASE</version>
57         </dependency>
58         
59     </dependencies>

 

 

 

Stock(账户类):

 

 1 package cn.happy.beans;
 2 
 3 /**
 4  * 股票类
 5  * Created by CKW on 2017/4/2.
 6  */
 7 public class Stock {
 8     private Integer sid;//编号
 9     private String sname;//名称
10     private Integer scount;//余额股
11 
12     public Stock(String sname, Integer scount) {
13         this.sname = sname;
14         this.scount = scount;
15     }
16 
17     public Integer getSid() {
18         return sid;
19     }
20 
21     public void setSid(Integer sid) {
22         this.sid = sid;
23     }
24 
25     public String getSname() {
26         return sname;
27     }
28 
29     public void setSname(String sname) {
30         this.sname = sname;
31     }
32 
33     public Integer getScount() {
34         return scount;
35     }
36 
37     public void setScount(Integer scount) {
38         this.scount = scount;
39     }
40 }

dao层接口(StockDao):

 1 package cn.happy.dao;
 2 
 3 
 4 import cn.happy.beans.Stock;
 5 
 6 /**
 7  * Created by CKW on 2017/4/2.
 8  */
 9 public interface StockDao {
10     //添加
11     public int addStock(Stock stock);
12 }

dao层实现类(StockDaoImpl):

 1 package cn.happy.dao.impl;
 2 
 3 
 4 import cn.happy.beans.Stock;
 5 import cn.happy.dao.StockDao;
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 
 9 import java.io.Serializable;
10 
11 /**
12  * Created by CKW on 2017/4/2.
13  */
14 
15 public class StockDaoImpl implements StockDao {
16 
17     //植入一个工厂对象
18     private SessionFactory sessionFactory;
19 
20     public int addStock(Stock stock) {
21         Session session = sessionFactory.getCurrentSession();
22         Serializable count = session.save(stock);
23         return (Integer)count;
24     }
25 
26     public SessionFactory getSessionFactory() {
27         return sessionFactory;
28     }
29 
30     public void setSessionFactory(SessionFactory sessionFactory) {
31         this.sessionFactory = sessionFactory;
32     }
33 }

service层接口(AccountService):

 1 package cn.happy.service;
 2 
 3 
 4 import cn.happy.beans.Stock;
 5 
 6 /**
 7  * Created by CKW on 2017/4/2.
 8  */
 9 public interface AccountService {
10 
11     //添加
12     public int addStock(Stock stock);
13 
14 }

service层实现类():

 1 package cn.happy.service.impl;
 2 
 3 
 4 
 5 import cn.happy.beans.Stock;
 6 import cn.happy.dao.StockDao;
 7 import cn.happy.service.AccountService;
 8 
 9 import org.springframework.transaction.annotation.Transactional;
10 
11 /**
12  * Created by CKW on 2017/4/2.
13  */
14 public class AccountServiceImpl implements AccountService {
15     private StockDao stockDao;
16 
17     @Transactional
18     public int addStock(Stock stock) {
19         return stockDao.addStock(stock);
20     }
21 
22     public StockDao getStockDao() {
23         return stockDao;
24     }
25 
26     public void setStockDao(StockDao stockDao) {
27         this.stockDao = stockDao;
28     }
29 }

配置文件xml:(applicationContext.xml)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8      http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
 9      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
10      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
11 ">
12 
13 
14     <!--spring识别到配置文件-->
15     <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
16 
17     <!--1.Datasource-->
18     <!--配置数据源-->
19     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
20         <property name="driverClassName" value="http://www.mamicode.com/${jdbc.driverClassName}"></property>
21         <property name="url" value="http://www.mamicode.com/${jdbc.url}"></property>
22         <property name="username" value="http://www.mamicode.com/${jdbc.username}"></property>
23         <property name="password" value="http://www.mamicode.com/${jdbc.password}"></property>
24     </bean>
25 
26     <!--2.SessionFactory-->
27     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
28         <property name="dataSource" ref="dataSource"></property>
29         <property name="hibernateProperties">
30             <props>
31                 <prop key="hibernate.show_sql">true</prop>
32                 <prop key="hibernate.format_sql">true</prop>
33                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
34                 <!--开线程-->
35                 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
36             </props>
37         </property>
38         <!--关联小配置-->
39         <property name="mappingDirectoryLocations" value="http://www.mamicode.com/classpath:cn/happy/beans"></property>
40     </bean>
41 
42     <!--3.dao-->
43     <bean id="stockDao" class="cn.happy.dao.impl.StockDaoImpl">
44         <property name="sessionFactory" ref="sessionFactory"></property>
45     </bean>
46 
47     <!--4.service-->
48     <bean id="stockService" class="cn.happy.service.impl.AccountServiceImpl">
49         <property name="stockDao" ref="stockDao"></property>
50     </bean>
51 
52     <!--5.事务管理器-->
53     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
54         <property name="sessionFactory" ref="sessionFactory"></property>
55     </bean>
56 
57     <!--6.事务-->
58     <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
59 
60 </beans>

jdbc.properties:

1 jdbc.driverClassName=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql:///y2163
3 jdbc.username=root
4 jdbc.password=1234

Stock小配置文件:Stock.hbm.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping package="cn.happy.beans">
 6     <class name="Stock">
 7         <id name="sid">
 8             <generator class="native"></generator>
 9         </id>
10         <property name="sname"></property>
11         <property name="scount"></property>
12     </class>
13 </hibernate-mapping>

测试类:

 1 import cn.happy.beans.Stock;
 2 import cn.happy.service.AccountService;
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 
 8 /**
 9  * Created by CKW on 2017/4/4.
10  */
11 public class MySSHTest {
12     @Test
13     public void myTest(){
14         ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
15         AccountService service=(AccountService)ac.getBean("stockService");
16         Stock stock=new Stock("呵呵",30);
17         service.addStock(stock);
18     }
19 }

查看数据库:添加成功!

技术分享

 

SSH程序框架之Spring与HIbernate整合