因为整合spring和hibernate所以,需要用到spring里面复写Hibernate的类以有DI和IOC特性

db.sql

hibernate_basic数据库

表 person

字段

pid pname psex

Person.java

 1 package cn.edu.spring_hibernate;
 2
 3 public class Person {
 4     private Long pid;
 5     private String pname;
 6     private String psex;
 7     public Long getPid() {
 8         return pid;
 9     }
10     public void setPid(Long pid) {
11         this.pid = pid;
12     }
13     public String getPname() {
14         return pname;
15     }
16     public void setPname(String pname) {
17         this.pname = pname;
18     }
19     public String getPsex() {
20         return psex;
21     }
22     public void setPsex(String psex) {
23         this.psex = psex;
24     }
25
26 }

View Code

Person.hbm.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <!--
 6         用来描述一个持久化类
 7         name  类的全名
 8          table 可以不写  默认值和类名一样
 9          catalog  数据库的名称  一般不写
10      -->
11     <class name="cn.edu.spring_hibernate.Person">
12         <!--
13             标示属性  和数据库中的主键对应
14             name  属性的名称
15             column 列的名称
16          -->
17         <id name="pid" column="pid" length="200" type="java.lang.Long">
18             <!--
19                 主键的产生器
20                   就该告诉hibernate容器用什么样的方式产生主键
21              -->
22             <generator class="increment"></generator>
23         </id>
24         <!--
25             描述一般属性
26          -->
27         <property name="pname" column="pname" length="20" type="string">
28         </property>
29
30         <property name="psex" column="psex" length="10" type="java.lang.String"></property>
31     </class>
32 </hibernate-mapping>

View Code

PersonDao.java

1 package cn.edu.spring_hibernate;
2 public interface PersonDao {
3     public void savePerson(Person person);
4 }

View Code

PersonDaoImpl.java

1 package cn.edu.spring_hibernate;
2 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
3 //继承HibernateDaoSupport操作数据库,事务管理在配置文件中配,和这个类没关系
4 public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
5     @Override
6     public void savePerson(Person person) {
7         this.getHibernateTemplate().save(person);
8     }
9 }

View Code

PersonService.java

1 package cn.edu.spring_hibernate;
2 public interface PersonService {
3     public void savePerson(Person person);
4 }

View Code

PersonServiceImple.java

 1 package cn.edu.spring_hibernate;
 2 public class PersonServiceImpl implements PersonService {
 3     //这里要写依赖注入,配置文件中可以配置
 4     private PersonDao personDao=new PersonDaoImpl();
 5     public PersonDao getPersonDao() {
 6         return personDao;
 7     }
 8     public void setPersonDao(PersonDao personDao) {
 9         this.personDao = personDao;
10     }
11     @Override
12     public void savePerson(Person person) {
13         personDao.savePerson(person);
14     }
15 }

View Code

MyException.java(用于异常处理)

1 package cn.edu.spring_hibernate;
2 public class MyException {
3     public void defineException(Throwable ex){
4         System.out.println(ex.getMessage());
5     }
6 }

View Code

test.java

 1 package cn.edu.spring_hibernate;
 2 import org.junit.Test;
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 public class test {
 6     @Test
 7     public void testSpring_Hibernate()
 8     {
 9         ApplicationContext context=new ClassPathXmlApplicationContext("cn/edu/spring_hibernate/config/applicationContext-spring_hibernate.xml");
10         //这里如果 getBean("personDao")会加入数据不成功,因为配置文件中没有对其开启事务处理
11         PersonService personService=(PersonService)context.getBean("personService");
12         Person person=new Person();
13         person.setPname("ssss");
14         person.setPsex("god");
15         personService.savePerson(person);
16     }
17 }

View Code

配置文件中

hibernate.cfg.xml (配置文件中第二种配置sessionFactory的时候用到)这种方法方便点

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <!--
 7         一个session-factory只能连接一个数据库
 8     -->
 9 <session-factory>
10     <!--
11         数据库的用户名
12     -->
13     <property name="connection.username">root</property>
14     <!--
15         密码
16     -->
17     <property name="connection.password">friends</property>
18     <!--
19         url
20     -->
21     <property name="connection.url">
22         jdbc:mysql://localhost:3306/hibernate_basic
23     </property>
24
25     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
26     <!--
27         作用:根据持久化类和映射文件生成表
28         validate
29         create-drop
30         create
31         update
32     -->
33     <property name="hbm2ddl.auto">update</property>
34     <!--
35         显示hibernate内部生成的sql语句
36     -->
37     <property name="show_sql">true</property>
38     <mapping resource="cn/edu/spring_hibernate/Person.hbm.xml" />
39
40 </session-factory>
41 </hibernate-configuration>

View Code

jdbc.properties  (配置文件中第一种配置sessionFactory的时候用到)

1 jdbc.driverClassName=com.mysql.jdbc.Driver
2 jdbc.url=jdbc\:mysql\://localhost\:3306/hibernate_basic
3 jdbc.username=root
4 jdbc.password=friends

View Code

applicationContext-spring_hibernate.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 3     xmlns:tx="http://www.springframework.org/schema/tx"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 6         http://www.springframework.org/schema/aop
 7         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
 8         http://www.springframework.org/schema/tx
 9         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
10 <!--     配置引入配置文件路径 -->
11     <bean
12         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
13         <property name="locations">
14             <value>classpath:cn/edu/spring_hibernate/config/jdbc.properties</value>
15         </property>
16     </bean>
17
18     <bean id="dataSource" destroy-method="close"
19         class="org.apache.commons.dbcp.BasicDataSource">
20         <property name="driverClassName" value="${jdbc.driverClassName}" />
21         <property name="url" value="${jdbc.url}" />
22         <property name="username" value="${jdbc.username}" />
23         <property name="password" value="${jdbc.password}" />
24     </bean>
25
26     <!--
27         sessionFactory 1、sessionFactoryImpl 2、利用spring的IOC和DI的特征
28         hibernate提供的 sessionFactory没有IOC和DI特征,所以spring重写了这个这个类加入了这两个特征
29     -->
30     <!-- 这是配置sessionFactory的第一种方式 -->
31         <!--
32     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
33         <property name="dataSouce" ref="dataSource"></property>
34         <property name="mappingResources">
35          导入配置文件
36             <list>
37                 <value>cn/edu/spring_hibernate/Person.hbm.xml</value>
38             </list>
39         </property>
40         <property name="hibernateProperties">
41             <value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value>
42         </property>
43     </bean>
44     -->
45 <!--     配置sessionFactory的第二种方式 -->
46     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
47         <property name="configLocation">
48             <value>classpath:cn/edu/spring_hibernate/config/hibernate.cfg.xml</value>
49         </property>
50     </bean>
51 <!--     因为personDaoImpl继承了HibernateDaoSupport,所以必须要注入sessionFactory -->
52     <bean id="personDao" class="cn.edu.spring_hibernate.PersonDaoImpl">
53         <property name="sessionFactory">
54             <ref bean="sessionFactory"/>
55         </property>
56     </bean>
57
58     <bean id="personService" class="cn.edu.spring_hibernate.PersonServiceImpl">
59         <property name="personDao">
60             <ref bean="personDao"/>
61         </property>
62     </bean>
63
64
65     <bean id="myException" class="cn.itcast.spring.jdbc.transaction.MyException"></bean>
66 <!--     spring和hibernate整合提供的事务管理器管理类, -->
67     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
68         <property name="sessionFactory">
69             <ref bean="sessionFactory"/>
70         </property>
71     </bean>
72     <!--
73         通知 1、告诉spring容器,采用什么样的方法处理事务 2、告诉spring容器,目标方法应该采用什么样的事务处理策略
74     -->
75     <tx:advice id="tx" transaction-manager="transactionManager">
76         <tx:attributes>
77             <!--
78                 save开头的函数名 name规定方法 isolation 默认值为DEFAULT propagation 传播机制 REQUIRED
79             -->
80             <tx:method name="save*" read-only="false" />
81         </tx:attributes>
82     </tx:advice>
83 <!--     本来事务由程序员自己写并且当切面放入,但是这里spring提供了事务处理的通知方法,所以不用程序员写切面了 -->
84     <aop:config >
85         <aop:pointcut expression="execution(* cn.edu.spring_hibernate.PersonServiceImpl.*(..))" id="perform"/>
86         <aop:advisor advice-ref="tx" pointcut-ref="perform" />
87 <!--         指定了切面和通知 -->
88         <aop:aspect  ref="myException">
89             <aop:after-throwing method="defineException" pointcut-ref="perform" throwing="ex"/>
90         </aop:aspect>
91     </aop:config>
92 </beans>

转载于:https://www.cnblogs.com/friends-wf/p/3787223.html

Spring+Hibernate整合相关推荐

  1. Java程序员从笨鸟到菜鸟之(七十九)细谈Spring(八)spring+hibernate整合基本详解

    由于spring和hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...

  2. flex3+blazeds+spring+hibernate整合小结

    近来flex盛行,因此这两天也借了本书看了两天,发觉作为非页面设计人员,flex 还是很好的,flex builder很好用,拖拉就 有很COOL的界面了,而且flex总的来说基本东西不难学,有编程基 ...

  3. Struts+Spring+Hibernate整合入门详解

    标签: strutshibernatespringbeanactionimport 2007-08-12 16:05 36280人阅读 评论(13) 收藏 举报 分类: STRUTS&SPRI ...

  4. 解决问题(七)——jsf+spring+hibernate整合(一)

    jsf+spring+hibernate整合,于struts+spring+hibernate整合的过程非常相似(就不截图了,可以参考第一篇文章),但是前者相对于后者稍微麻烦一些. 第一步:初步整合, ...

  5. Spring、Struts2+Spring+Hibernate整合步骤

    所使用的Jar包: Hibernate: Spring(使用MyEclipse自动导入框架功能) Struts2: 注解包和MySql驱动包: 1.配置Hibernate和Spring: <be ...

  6. spring mvc+spring + hibernate 整合(二)

    在上篇文章中,我建立了工程并配置了spring + hibernate.今天我们检验下上篇文章的成果,如何检查呢?那就是进行单元测试.本篇文章就让大家和我一起来就前面的建的工程进行单元测试. 本项目使 ...

  7. Spring+Hibernate整合Hessian

    2019独角兽企业重金招聘Python工程师标准>>> 软件环境: JDK1.6.Spring3.0.5.Hibernate3.2.2.Hessian3.1.5 参考Spring D ...

  8. JSF Spring Hibernate 整合:JSH1

    JSF1.1 + Spring2.0 + Hibernate3.1 项目图示: JSF1.1 Jar包: MyFaces1.1 Jar包: Spring与Hibernate包的配置与文章 web.xm ...

  9. struts+spring+hibernate整合小例子

    项目结构图 地址:http://www.blogjava.net/xiaoyi/articles/xiaoyi_ssh.html 包 com.yz.dao.impl package com.yz.da ...

最新文章

  1. Unity 中的协同程序
  2. aapt: error while loading shared libraries: libstdc++.so.6: wrong ELF class: ELFCLASS64
  3. linux apt qt下载,Linux如何安装 apt-get 软件管理工具
  4. VMware虚拟机安装centos
  5. 大剑无锋之Zookeeper面试题
  6. c++ 多线程:线程句柄可以提前关闭,但是线程并没有关闭
  7. 【原创】Proton在Android上的编译
  8. (day 52 - 先序后序遍历计数 ) 剑指 Offer 55 - II. 平衡二叉树
  9. 码农面试智力题及答案
  10. Navicat Premium 注册机 激活报错
  11. java生成随机数组_Java 生成随机数
  12. 微信小程序开发者工具下载及安装
  13. uni-app打包成Android Apk 全程详解
  14. 华为路由器 单臂路由
  15. 不可不知的JavaScript面向对象
  16. halcon与C#混合编程进阶版
  17. 使用ZXing扫描多个二维码,条形码
  18. 【在Windows7旗舰版下安装Anaconda报错failed to create menus】
  19. 悬链线锚链力的求解方法
  20. ACGAN与CGAN的区别

热门文章

  1. OCP 12c最新考试原题及答案(071-3)
  2. 轮询 长轮询 websocket
  3. 在Developerkit开发板上运行blink例程
  4. Db4o结合Linq、Lambda表达式的简单示例
  5. grep, sed, awk
  6. error: device not found - waiting for device -
  7. FragmentPagerAdapter 与 FragmentStatePagerAdapter 的区别
  8. Self-Reflection: How to Do It Right
  9. 从硬件到软件 统一沟通将引领通讯市场
  10. 微服务之配置中心ConfigKeeper