Spring 整合 Hibernate
来源:互联网 发布:大连东华软件股份公司 编辑:程序博客网 时间:2024/05/20 22:38
源码:https://pan.baidu.com/s/1gfEPOpd 密码:4ie5
1. Spring 整合 Hibernate 整合什么 ?
1). 有 IOC 容器来管理 Hibernate 的 SessionFactory
2). 让 Hibernate 使用上 Spring 的声明式事务
2. 整合步骤:
1). 先加入 hibernate
①. jar 包
②. 添加 hibernate 的配置文件: hibernate.cfg.xml
hibernate的xml文件中配置hibernate的基本属性
--数据源需配置到IOC容器中,所以不需要在hibernate.cfg.xml文件处配置数据源
--关联的hbm.xml也在IOC容器配置SessionFactory实例时再进行配置
--这里配置hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存
--开发时可以去掉cfg.xml文件,但为了更加自然建议保留
<propertyname="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>方言
<propertyname="hibernate.show_sql">true</property>显示SQL语句
<propertyname="hibernate.format_sql">true</property>SQL语句是否格式化
<propertyname="hibernate.hbm2ddl.auto">update</property>生成表的策略为更新
③. 编写了持久化类对应的 .hbm.xml 文件。
为每一个持久化类生成对应的.hbm.xml文件
示例:这是一个普通类
public classAccount {
privateInteger id;
privateString username;
private intbalance;
publicInteger getId() {
returnid;
}
public voidsetId(Integer id) {
this.id= id;
}
publicString getUsername() {
returnusername;
}
public voidsetUsername(String username) {
this.username= username;
}
public intgetBalance() {
returnbalance;
}
public voidsetBalance(intbalance) {
this.balance= balance;
}
}
这个普通类所对应的hbm.xml
<?xml version="1.0"?>
<!DOCTYPEhibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd";>
<hibernate-mapping>
<classname="com.atguigu.spring.hibernate.entities.Account"
table="SH_ACCOUNT">这里是这个类在数据库中对应的表名
<idname="id"type="java.lang.Integer">
<columnname="ID"/>数据库对应表字段的名字
<generatorclass="native"/>主键的增长方式
</id>
<propertyname="username"type="java.lang.String">
<columnname="USERNAME"/>
</property>
<propertyname="balance"type="int">
<columnname="BALANCE"/>
</property>
</class>
</hibernate-mapping>
2). 加入 Spring
①. jar 包
②. 加入 Spring 的配置文件--在这里整合
在Spring的配置文件(application.xml)文件中
1、配置数据源
先创建一个资源文件,方便管理(db.properties)
jdbc.user=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring
#数据库初始化大小
jdbc.initPoolSize=5
#数据库最大使用连接数
jdbc.maxPoolSize=10
#...
在application.xml文件中导入资源文件需要先导入context命名空间
<context:property-placeholderlocation="classpath:db.properties"/>
接下来配置数据源
<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
<propertyname="user"value="${jdbc.user}"></property>
<propertyname="password"value="${jdbc.password}"></property>
<propertyname="driverClass"value="${jdbc.driverClass}"></property>
<propertyname="jdbcUrl"value="${jdbc.jdbcUrl}"></property>
<propertyname="initialPoolSize"value="${jdbc.initPoolSize}"></property>
<propertyname="maxPoolSize"value="${jdbc.maxPoolSize}"></property>
</bean>
2、配置Hibernate的Sessionfactory实例
<!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 -->
<beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->
<propertyname="dataSource"ref="dataSource"></property>
<!-- 配置 hibernate 配置文件的位置及名称 -->
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
<!-- 配置 hibernate 映射文件的位置及名称, 可以使用通配符 -->
<propertyname="mappingLocations"
value="classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml"></property>
</bean>
配置Spring的声明式事务,需要先在application.xml文件中导入tx命名空间
<!-- 1. 配置事务管理器 -->
<beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<!-- 2. 配置事务属性, 需要事务管理器 -->
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="get*"read-only="true"/>
<tx:methodname="purchase"propagation="REQUIRES_NEW"/>
<tx:methodname="*"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->
<aop:config>
<aop:pointcutexpression="execution(* com.atguigu.spring.hibernate.service.*.*(..))"
id="txPointcut"/>
<aop:advisoradvice-ref="txAdvice"pointcut-ref="txPointcut"/>
</aop:config>
3. 编写代码
在使用时,使用sessionFactory.getCurrentSession获取与当前线程相关的session
@Repository 这里使用了注解来标识这是一个bean,所以要在application.xml声明要扫描的类
public classBookShopDaoImpl implements BookShopDao {
@Autowired
privateSessionFactory sessionFactory;
//不推荐使用 HibernateTemplate 和 HibernateDaoSupport
//因为这样会导致 Dao 和 Spring 的 API 进行耦合
//可以移植性变差
// private HibernateTemplate hibernateTemplate;
//获取和当前线程绑定的 Session.
privateSession getSession(){
returnsessionFactory.getCurrentSession();
}
@Override
public intfindBookPriceByIsbn(String isbn) {
String hql ="SELECT b.price FROM Book b WHERE b.isbn = ?";
Query query = getSession().createQuery(hql).setString(0, isbn); hibernate的HQL语句
return(Integer)query.uniqueResult();
}
}
源码:https://pan.baidu.com/s/1gfEPOpd 密码:4ie5
阅读全文
1 0
- Spring:Spring整合Hibernate
- 【Spring】Spring+hibernate整合
- struts整合spring整合hibernate
- spring 整合hibernate整合方式
- 整合 struts spring hibernate
- struts+spring+hibernate整合
- struts,hibernate,spring整合
- struts+spring+hibernate整合
- struts+spring+hibernate整合
- Spring+hibernate+DWR整合
- webwork+spring+hibernate整合
- webwork+spring+hibernate整合
- Spring整合Hibernate
- Struts hibernate Spring 整合
- Struts hibernate Spring 整合
- Spring整合Hibernate
- 整合Spring、Struts、Hibernate
- webwork+spring+hibernate整合
- E
- VB.NET中的程序流控制
- 稳压电源 连载5:一般电源的设计
- 《大象 Thinking in UML》学习笔记(六)——UML核心视图之静态视图:用例图、类图
- android 跳转百度,高德,腾讯地图以及商店
- Spring 整合 Hibernate
- codeforces-768B Code for 1
- 2 Keys Keyboard_Week13
- 浅谈Word中文档标题及编号的设置
- MathCAD安装
- 谈谈MATLAB中cell函数
- Tab页切换时页面自动刷新效果
- B
- 拖动应用缩略图进入分屏模式流程