整合hibernate

整合什么?

  1. 有ioc容器来管理hibernate的SessionFactory
  2. 让hibernate使用上spring的声明式事务

先加入hibernate 驱动包

新建hibernate.cfg.xml

配置hibernate的基本属性

  1. 数据源需配置到IOC 容器中,所以在此处不再需要配置数据源
  2. 关联的.hbm.xml也在IOC 容器配置SessionFactory实例时进行配置。
  3. 配置hibernate的基本属性:方言,sql的显示及格式化,生成数据表的策略以及二级缓存等。

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<property name="hibernate.show_sql">true</property>

<property name="hibernate.format_sql">true</property>

<property name="hibernate.hbm2ddl.auto">update</propert

Ctrl+shift+T打开源码文件

在加入spring

Db.properties

jdbc.user=root

jdbc.password=root

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring

配置数据源

需导入

xmlns:context="http://www.springframework.org/schema/context"

<!-- 配置数据源 -->

<context:property-placeholder location="classpath:db.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="user" value="${jdbc.user}"></property>

<property name="password" value="${jdbc.password}"></property>

<property name="driverClass" value="${jdbc.driverClass}"></property>

<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>

<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>

</bean>

测试类测试能否拿到datasource

public class Go {

private static ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

public static void main(String[] args) throws SQLException {

DataSource d= ctx.getBean(DataSource.class);

System.out.println(d.getConnection());

}

}

通过spring来操作hibernate且使用spring的事务

Spring的applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<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">

<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

<property name="url"

value="jdbc:mysql://localhost:3306/spring?userUnicode=true&amp;characterEnchoding=utf-8">

</property>

<property name="username" value="root"></property>

<property name="password" value="root"></property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<!-- 指向hibernate的配置文件 -->

<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

<property name="mappingLocations" value="classpath:com/ssh/spring_hibernate/entity/*.hbm.xml"></property>

</bean>

<bean id="transactionManager"

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!-- 配置事务属性 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="get*" read-only="true"/>

<tx:method name="*"/>

</tx:attributes>

</tx:advice>

<!-- 配置事务的切点 -->

<aop:config>

<aop:pointcut expression="execution(* com.ssh.spring_hibernate.service.*.*(..))"

id="txPointcut"/>

<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>

</aop:config>

<tx:annotation-driven transaction-manager="transactionManager" /></beans>

两个Javabean

Account

private Integer id;

private int balance;

private String username;

Book

private Integer id;

private String bookName;

private String isbn;

private int stock;

private int price;

对应的hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2015-8-28 12:55:21 by Hibernate Tools 3.4.0.CR1 -->

<hibernate-mapping>

<class name="com.ssh.spring_hibernate.entity.Account" table="SH_ACCOUNT">

<id name="id" type="java.lang.Integer">

<column name="ID" />

<generator class="native" />

</id>

<property name="username" type="java.lang.String">

<column name="USER_NAME" />

</property>

<property name="balance" type="int">

<column name="BALANCE" />

</property>

</class>

</hibernate-mapping>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2015-8-28 12:55:21 by Hibernate Tools 3.4.0.CR1 -->

<hibernate-mapping>

<class name="com.ssh.spring_hibernate.entity.Book" table="SH_BOOK">

<id name="id" type="java.lang.Integer">

<column name="ID" />

<generator class="native" />

</id>

<property name="bookName" type="java.lang.String">

<column name="BOOK_NAME" />

</property>

<property name="isbn" type="java.lang.String">

<column name="ISBN" />

</property>

<property name="price" type="int">

<column name="PRICE" />

</property>

<property name="stock" type="int">

<column name="STOCK" />

</property>

</class>

</hibernate-mapping>

最新文章

  1. PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令
  2. FreeBSD暂时用9.X系列为宜
  3. Power Strings(poj 2406)
  4. 【转】bzero, memset ,setmem 区别
  5. python在linux上的GUI无法弹出界面
  6. http://blog.csdn.net/zhang_xinxiu/article/details/38655311
  7. Spring 3.x企业应用开发实战(9-1)----依赖注入
  8. BOM 浏览器对象模型学习
  9. 进入html+css世界的正确姿势
  10. Python文件复制(txt文件)
  11. (生活)Photoshop入门(不定时更新)
  12. Shell 脚本元组+for循环
  13. Linux学习man page
  14. RzCheckTree基本使用
  15. grandstack graphql 工具基本试用
  16. 请教如何改善C#中socket通信机客户端程序的健壮性
  17. 8-linux 安装 requests 时 pip install 安装不了
  18. java编码规范_缩进和注释
  19. Kali-linux使用SET实施攻击
  20. Spring集合 (List,Set,Map,Properties) 实例

热门文章

  1. redis五种数据结构及使用场景
  2. hisi3559的usb无线网卡驱动(rtl8192cu)(一条龙服务:内核编译、驱动编译、iw等工具编译)
  3. tomcat启动慢?自己动手打造轻量web服务器(一)
  4. Firebird3基本使用
  5. QtGUI Module&#39;s Classes
  6. 【LG3722】[HNOI2017]影魔
  7. P4198 楼房重建
  8. async/await工作机制探究--NodeJS
  9. python中的运算符的分类以及使用方法
  10. 淡雅清新教师求职简历免费word模板