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

db.sql

hibernate_basic数据库

表 person

字段

pid pname psex

Person.java

 package cn.edu.spring_hibernate;

 public class Person {
private Long pid;
private String pname;
private String psex;
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPsex() {
return psex;
}
public void setPsex(String psex) {
this.psex = psex;
} }

Person.hbm.xml

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
用来描述一个持久化类
name 类的全名
table 可以不写 默认值和类名一样
catalog 数据库的名称 一般不写
-->
<class name="cn.edu.spring_hibernate.Person">
<!--
标示属性 和数据库中的主键对应
name 属性的名称
column 列的名称
-->
<id name="pid" column="pid" length="200" type="java.lang.Long">
<!--
主键的产生器
就该告诉hibernate容器用什么样的方式产生主键
-->
<generator class="increment"></generator>
</id>
<!--
描述一般属性
-->
<property name="pname" column="pname" length="20" type="string">
</property> <property name="psex" column="psex" length="10" type="java.lang.String"></property>
</class>
</hibernate-mapping>

PersonDao.java

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

PersonDaoImpl.java

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

PersonService.java

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

PersonServiceImple.java

 package cn.edu.spring_hibernate;
public class PersonServiceImpl implements PersonService {
//这里要写依赖注入,配置文件中可以配置
private PersonDao personDao=new PersonDaoImpl();
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void savePerson(Person person) {
personDao.savePerson(person);
}
}

MyException.java(用于异常处理)

 package cn.edu.spring_hibernate;
public class MyException {
public void defineException(Throwable ex){
System.out.println(ex.getMessage());
}
}

test.java

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

配置文件中

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

 <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--
一个session-factory只能连接一个数据库
-->
<session-factory>
<!--
数据库的用户名
-->
<property name="connection.username">root</property>
<!--
密码
-->
<property name="connection.password">friends</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate_basic
</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--
作用:根据持久化类和映射文件生成表
validate
create-drop
create
update
-->
<property name="hbm2ddl.auto">update</property>
<!--
显示hibernate内部生成的sql语句
-->
<property name="show_sql">true</property>
<mapping resource="cn/edu/spring_hibernate/Person.hbm.xml" /> </session-factory>
</hibernate-configuration>

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

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

applicationContext-spring_hibernate.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置引入配置文件路径 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:cn/edu/spring_hibernate/config/jdbc.properties</value>
</property>
</bean> <bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <!--
sessionFactory 1、sessionFactoryImpl 2、利用spring的IOC和DI的特征
hibernate提供的 sessionFactory没有IOC和DI特征,所以spring重写了这个这个类加入了这两个特征
-->
<!-- 这是配置sessionFactory的第一种方式 -->
<!--
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSouce" ref="dataSource"></property>
<property name="mappingResources">
导入配置文件
<list>
<value>cn/edu/spring_hibernate/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value>
</property>
</bean>
-->
<!-- 配置sessionFactory的第二种方式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:cn/edu/spring_hibernate/config/hibernate.cfg.xml</value>
</property>
</bean>
<!-- 因为personDaoImpl继承了HibernateDaoSupport,所以必须要注入sessionFactory -->
<bean id="personDao" class="cn.edu.spring_hibernate.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean> <bean id="personService" class="cn.edu.spring_hibernate.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean> <bean id="myException" class="cn.itcast.spring.jdbc.transaction.MyException"></bean>
<!-- spring和hibernate整合提供的事务管理器管理类, -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!--
通知 1、告诉spring容器,采用什么样的方法处理事务 2、告诉spring容器,目标方法应该采用什么样的事务处理策略
-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!--
save开头的函数名 name规定方法 isolation 默认值为DEFAULT propagation 传播机制 REQUIRED
-->
<tx:method name="save*" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 本来事务由程序员自己写并且当切面放入,但是这里spring提供了事务处理的通知方法,所以不用程序员写切面了 -->
<aop:config >
<aop:pointcut expression="execution(* cn.edu.spring_hibernate.PersonServiceImpl.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform" />
<!-- 指定了切面和通知 -->
<aop:aspect ref="myException">
<aop:after-throwing method="defineException" pointcut-ref="perform" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>

最新文章

  1. 黄聪:phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
  2. AFNetwork ATS 网络层改造
  3. Best Practices for Background Jobs_3 Managing Device Awake State之电源锁、Alarm、WakefulBroadcastReceiver
  4. 15天玩转redis —— 第五篇 集合对象类型
  5. socket编程listen函数限制连接数的解决方案
  6. Ros集成开发环境配置
  7. css小技巧之去掉蓝色底块的方法
  8. 用C++编写一个随机产生多个两位数四则运算式子的简单程序
  9. nodejs问题整理--fs.exists无法正确判断文件的问题
  10. js keys方法和foreach方法区别
  11. SpringMVC之Http标准的头部信息
  12. 会跳高的字体插件jquery.beattext.js
  13. java消息队列--ActiveMQ
  14. jsonp形式的ajax请求:
  15. Hbase记录-备份与恢复方案推荐
  16. JPA报错, java.lang.NullPointerException
  17. iOS - push 或 pop或点击导航栏返回pop指定导航控制器
  18. spark报错处理
  19. JNI由浅入深_2_C语言基础
  20. 4.Linux文件系统层次体系标准

热门文章

  1. hdu 2955 Robberies(概率背包)
  2. [Arc074E] RGB Sequence
  3. Problem C: 指针:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面m个数
  4. 详解React的生命周期
  5. Codeforces Round #344 (Div. 2) C. Report 其他
  6. NFS迁移
  7. 手机在线更新系统MySQL数据库服务器参数优化mycnf,16G内存8核CPU,
  8. OM-销售订单行【订购项目】配置参数文件控制
  9. luci范例
  10. Android-25种开源炫酷动画框架