spring整合hibernate(二)

通过之前的讲解已经基本了解了hibernate与spring整合的基本内容,那么现在我们来通过动手实践来实现spring和hibernate的整合吧。

首先导入hibernate 和 spring 的核心包,另外因为要使用aop 和 aspectj的所以还需要导入aspectjweaver.jar、aopalliance.jar。

其中aopalliance.jar是AOP联盟的API包,里面包含了针对面向切面的接口。 通常Spring等其它具备动态织入功能的框架依赖此包。spring-context.jar和spring-aop.jar需要依赖此包。

aspectjweaver.jar该包是spring集成AspectJ LTW织入器所需包;

因为需要访问数据库所以还需要数据库的驱动包我这里用的是mysql所以需要mysql-connector-java-5.1.38-bin.jar这个包。

当然也需要数据库连接池的依赖,这里我使用的是c3p0的数据库连接池所以需要c3p0.jar该包可以从下载下来的hibernate的一下目录找到hibernate-distribution-3.3.2.GA/lib/optional。

其他jar基本不会遗漏这里就不再详细介绍。

本次整合是在之前spring与struts2整合的项目基础上进行整合的所以关于spring的基本配置就不再过多介绍了。

//hibernate的配置文件
<?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>
        <!-- 配置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="hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>
//pojo
public class User {
    private int id;
    private String name;
    private String password;
//....get/set方法省略
}
//user类对应的hbm文件
<?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>
    <class name="User" table="tb_user">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="password"/>
    </class>
</hibernate-mapping>
<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:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">
    <!-- 整合struts2时用来测试的业务类 -->
    <bean id="myService" class="MyService"/>
    <!-- 整合struts2时用来测试的action -->
    <bean id="testAction" class="TestAction">
        <!-- 通过spring依赖注入action所需要的业务类 -->
        <property name="ms" ref="myService"></property>
        <property name="us" ref="userService"></property>
    </bean>
    <!-- 整合hibernate用来测试的dao实现类 -->
    <bean id="userDao" class="UserDaoImpl"/>
    <!-- 整合hibernate时用来测试的业务实现类 -->
    <bean id="userService" class="UserServiceImpl">
    <!-- 通过spring依赖注入业务类所需要的dao类 -->
        <property name="userDao" ref="userDao"/>
    </bean>
    <!-- spring加载外部配置文件 -->
    <bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:resources/resource/jdbc.properties"></property>
    </bean>
    <!-- 定义数据源Bean,使用C3P0数据源实现 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
        <property name="minPoolSize" value="${jdbc.minPoolSize}"/>
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
        <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
    </bean>
    <!-- 定义Hibernate的SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 注入datasource,给sessionfactoryBean内setdatasource提供数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- //加载实体类的映射文件位置及名称 -->
        <property name="mappingLocations">
            <list>
                <value>classpath:resources/hibernate/*.hbm.xml</value>
            </list>
        </property>
        <property name="configLocation" value="classpath:resources/hibernate/hibernate.cfg.xml"/>
    </bean>
    <!-- 配置Spring声明式事务 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.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(* UserDaoImpl.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
</beans>
//dao实现类
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    @Override
    public void addUser(String username, String password) {
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        getHibernateTemplate().save(user);
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容