整合hibernate
整合什么?
-
有ioc容器来管理hibernate的SessionFactory
-
让hibernate使用上spring的声明式事务
先加入hibernate 驱动包
新建hibernate.cfg.xml
配置hibernate的基本属性
-
数据源需配置到IOC 容器中,所以在此处不再需要配置数据源
-
关联的.hbm.xml也在IOC 容器配置SessionFactory实例时进行配置。
-
配置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 jdbc.initPoolSize=5 jdbc.maxPoolSize=10 |
配置数据源
需导入
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&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> |