spring 集成hibernate
http://tianya23.blog.51cto.com/1081650/275301
Spring整合Hibernate
1、首先Hibernate完成OR映射功能
private Integer id;
private String name;
public Person(){}
public Person(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.comp.bean">
<class name="Person" table="person">
<cache usage="read-write" region="cn.comp.bean.Person"/>
<id name="id">
<generator class="native"/>
</id>
<property name="name" length="10" not-null="true"/>
</class>
</hibernate-mapping>
2、让Hibernate完成后的对象交给Spring来管理
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context-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">
<context:annotation-config/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personService" class="cn.comp.service.impl.PersonServiceBean"/>
<bean id="personList" class="cn.comp.web.PersonAction"/>
</beans>
public void save(Person person);
public void update(Person person);
public Person getPerson(Integer personid);
public void delete(Integer personid);
public List<Person> getPersons();
}
public class PersonServiceBean implements PersonService {
@Resource private SessionFactory sessionFactory;
public void save(Person person){
sessionFactory.getCurrentSession().persist(person);
}
public void update(Person person){
sessionFactory.getCurrentSession().merge(person);
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Person getPerson(Integer personid){
return (Person)sessionFactory.getCurrentSession().get(Person.class, personid);
}
public void delete(Integer personid){
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().load(Person.class, personid));
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
@SuppressWarnings("unchecked")
public List<Person> getPersons(){
return sessionFactory.getCurrentSession().createQuery("from Person").list();
}
}
3、完成对实现类的测试
private static PersonService personService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
personService = (PersonService)applicationContext.getBean("personService");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
@Test
public void testSave() {
personService.save(new Person("小张"));
}
@Test
public void testUpdate() {
Person person = personService.getPerson(1);
//....
person.setName("小丽");
personService.update(person);
}
@Test
public void testGetPerson() {
Person person = personService.getPerson(2);
System.out.println(person.getName());
try {
System.out.println("请关闭数据库");
Thread.sleep(1000*15);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第二次开始获取");
person = personService.getPerson(2);
System.out.println(person.getName());
}
@Test
public void testDelete() {
personService.delete(1);
}
@Test
public void testGetPersons() {
List<Person> persons = personService.getPersons();
for(Person person : persons){
System.out.println(person.getName());
}
}
}
spring 集成hibernate相关推荐
- spring 集成hibernate 连接多数据库 java BaseDao 实现
2019独角兽企业重金招聘Python工程师标准>>> package com.htjf.dao.impl; import java.io.IOException; import j ...
- javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate
1.hibernate译为"越冬",指的是给java程序员带来春天,因为java程序员无需再关心各种sql了: 2.hibernate通过java类生成数据库表,通过操作对象来映射 ...
- 通过Spring集成从Hibernate 3迁移到4
本周是时候将我们的代码库升级到最新的Hibernate 4.x了. 我们推迟了迁移(仍在Hibernate 3.3上),因为3.x分支的较新维护版本需要对API进行一些更改,这些更改显然仍在不断变化中 ...
- Spring MVC Hibernate MySQL集成CRUD示例教程
Spring MVC Hibernate MySQL集成CRUD示例教程 我们在上一篇教程中学习了如何集成Spring和Hibernate.今天,我们将继续前进,并将Spring MVC和Hibern ...
- Primefaces Spring和Hibernate集成示例教程
Primefaces Spring和Hibernate集成示例教程 欢迎使用Spring Primefaces和Hibernate Integration示例.框架之间的集成是一项复杂的任务,而且大多 ...
- (转)为Spring集成的Hibernate配置二级缓存
http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...
- hibernate mysql 注解_【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)
前言 本文将基于注解配置, 集成Spring 4和Hibernate 4,开发一个增删改查应用,涉及以下内容: 创建Hibernate实体 保存数据到mysql数据库中 在事务transaction内 ...
- primefaces教程_Primefaces Spring和Hibernate集成示例教程
primefaces教程 Welcome to the Spring Primefaces and Hibernate Integration example. Integration between ...
- IntelliJ IDEA Spring Boot(5) 集成Hibernate
上一篇集成了mybatis.这一篇我来集成hibernate,顺道把ehcache缓存也集成进去.下篇我们再讲 mybatis与ehcache集成 pom依赖增加 <dependency> ...
最新文章
- mysql事件探查器_【干货】Mysql的事件探查器-之Mysql-Proxy代理实战一(安装部署与实战sql拦截与性能监控)...
- JSP第二次作业_8小题
- 整个世界都是你的绿幕:这个视频抠图换背景的方法着实真假难辨
- Java 8 Stream Api 中的 map和 flatMap 操作
- TCP和UDP的区别(转)
- 面向对象三大特性之一:多态(C++)
- 如何解决AIX的文件系统故障
- rufus中gpt和mrb磁盘_UEFI/BIOS/MBR/GPT启动过程详解与常见系统启动问题
- 树莓派html5播放器,树莓派|5 个很适合在课堂上演示的树莓派项目
- c++ printf怎么用_【经典】把脉printf中的C进阶技巧
- 牛顿新定律:凭本事得到的,凭什么要还?
- 远程拷贝 linux服务器,linux scp 服务器远程拷贝(示例代码)
- 属于程序员的黄金五年,把握这 5 年,或将迎来美好的职场生活!
- B75经典门户商业版discuz模板
- 《构建之法》前三章读后感
- 面向生态合作伙伴的实践分享回顾
- Linux 开源 ssh 工具,【原创开源】jssh linux scp ssh 免密登录工具
- php sql注入防御方法,php怎么防御sql注入
- 分布式机器学习主要笔记
- 【技术贴】Windows图片和传真查看器打开图片慢,正在生成预览的解决办法!
热门文章
- 13.2.8 用户认证
- 【STM32】GPIO功能复用
- opencv编译python库_linux 下编译安装 opencv-python
- python mysqldb安装_Flask干货:访问数据库——Python数据库框架MySQLPython
- 跑三小时的monkey测试该怎么算_百亿次的锤炼 - 带逛Dragonboat的各类测试
- python schedule多线程_Python定时任务sched模块用法示例
- java redis服务_java链接redis服务器
- CF-558E(E. A Simple Task)
- HDU - 6661 Acesrc and String Theory (后缀数组)
- HDU-6470 Count (构造矩阵+矩阵快速幂)