spring整合Hibernate+iBATIS
来源:互联网 发布:剑灵天族捏脸数据 编辑:程序博客网 时间:2024/05/29 08:34
为什么spring要整合hibernate:
1.通用的资源管理。
2.优秀的管理机制(基于依赖注入的sessionFactory管理机制)。
3.统一的事务管理。
4.统一的异常处理
spring整合hibernate:
1.配置数据源:
spring内置实现了org.springframework.jdbc.dataSource.DriverManageDataSource或者是使用DBCP提供的org.apache.commons.dbcp.BasicDataSource.
配置如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/dababase"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
2.配置sessionFactory:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--传入dataSource-->
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<!--添加映射文件-->
<property name="mappingResources">
<list>
<value>com/lxitedu/user.hbm.xml</value>
.................
</list>
</property>
<!--hibernate的属性-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
..............
</props>
</property>
</bean>
3.给继承spring提供的HibernateDaoSupport的dao依赖注入:
<bean id="StudentDAOImpl" class="com.lxitedu.dao.impl.StudentDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
spring提供了简洁的声明式事务控制,只需要在配置文件中增加事务控制片段,业务逻辑代码无须任何改变,spring的声明式事务,甚至支持在不同事务策略之间进行切换。
1.配置Dao
<bean id="StudentDAOImpl" class="com.lxitedu.dao.impl.StudentDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
2.装配事务管理器
<bean id="transactionManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sesssionFactory"/>
</property>
</bean>
3.抽象代理类
<bean id="proxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<!--传入事务管理器-->
<property name="transacitonManager">
<ref bean="transactionManager"/>
</property>
<!--事务属性-->
<property name="transactionAttributes">
<props>
<prop key="*" PROPAGATION_REQUIRED</prop>
...............
</props>
</property>
4.装配service层
<bean id="studentServiceImpl" class="com.lxitedu.service.impl.StudentServiceImpl">
<property name="studentDao">
<ref bean="StudentDAOImpl"/>
</propety>
5.装配具体事务代理
<bean id="proxyService" parent="proxy">
<property name="target">
<ref bean="studentSerivceImpl/>
</property>
</bean>
以上是针对spring整合Hibernate的事务。
spring整合ibatis
1.配置数据源:
跟Hibernate一样的
2.创建一个SqlMapClient
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/sql-map-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
sql.map-config.xml里主要配置:
<sqlMapConfig>
<sqlMap resource="com/lxitedu/dao/ibatis/maps/Account.xml"/>
。。。。。。。。。。
</sqlMapConfig>
3.给继承spring提供的SqlMapClientDaoSupport的dao依赖注入:
<bean id="StudentDAOImpl" class="com.lxitedu.dao.impl.StudentDAOImpl">
<property name="sqlMapClient">
<ref local="sqlMapClient" />
</property>
</bean>
spring为ibatis提供声明式事务
就是装配事务管理器跟Hibernate不同,其余的一样。
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
- spring整合Hibernate+iBATIS
- spring+hibernate+iBATIS注解方式整合
- 15Spring整合HIbernate&Ibatis(不完整版)
- Spring和Hibernate与Spring和Ibatis整合的差异
- spring与ibatis整合
- Spring 与 ibatis 整合
- Spring与Ibatis整合
- ibatis-Spring 整合
- Struts2+Ibatis+Spring整合
- spring和ibatis整合
- ibatis与spring整合
- spring整合ibatis事务管理
- Spring+iBatis整合详解
- SpringMVC+Spring+Ibatis整合
- iBATIS与Spring整合
- ibatis-Spring 整合
- flex,spring,ibatis整合
- spring整合iBATIS
- 开始写技术blog
- S60平台应用状态面板常用样式
- Hibernate总结2
- 链路层编址
- Hibernate总结3
- spring整合Hibernate+iBATIS
- 批处理设置IP
- 在VM arguments中设置参数来控制代码的执行逻辑
- 我你吵一吵,其实这样更能明白自己在他人中的形象的真实性以及自己的一些不足之处
- VS2010简介
- oracle 10g 表分区技术 影响测试
- 传智播客-多线程(1)-java里的进程和线程
- GUI编辑器
- 取坐标及变换