spring与其它框架的集成(三) spring+ibatis

spring与其它框架的集成(三) spring+ibatis

spring 2008-12-18 19:27:41 阅读122 评论0 字号:大中小

这一章主要介绍一下spring和ibatis的集成
1. applicationCtx.xml中定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans
default-autowire="byName"
default-lazy-init="false"
default-dependency-check="none"
>
<!-- spring hibernate start -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username"
value="${hibernate.connection.username}" />
<property name="password"
value="${hibernate.connection.password}" />
<property name="maxActive" value="500" />
<property name="maxIdle" value="50" />
<property name="maxWait" value="5000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
<property name="minEvictableIdleTimeMillis" value="-1" />
</bean>

<!--与配置hibernate不同的地方 start-->
<!--根据dataSource和configLocation创建一个SqlMapClient-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>/WEB-INF/sqlMapConfig.xml</value>
</property>
</bean>
<!--根据dataSource和configLocation创建一个SqlMapClient-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
abstract="false" lazy-init="default"
autowire="default" dependency-check="default">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!--与配置hibernate不同的地方 end-->

<!-- spring hibernate end -->
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>

3. 在database.properties中配置:
hibernate.connection.username=postgres
hibernate.connection.password=postgres
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.dialect.ant=jp.co.softbrain.bpm2.tools.hbm2ddl.dialect.PostgreSQLDialect
hibernate.connection.url=jdbc\:postgresql\://localhost/HibernateTest?useUnicode\=true&characterEncoding\=utf-8
hibernate.connection.driver_class=org.postgresql.Driver
database.name=postgres
database.schema=public
database.type=postgresql
以供dataSource中调用,dataSource是配置数据源。
4. transactionManager集成hibernate。
5. txProxyTemplate配置spring事务

6. 配置service使用代理
<bean id="loginService" parent="txProxyTemplate">
<property name="target">
<bean class="servletTest.service.impl.LoginServiceImpl" autowire="byName"/>
</property>
</bean>
这样loginService就有事务了
如果不要事务:
<bean id="loginService" class="servletTest.service.impl.LoginServiceImpl" autowire="byName"/>

7. 配置dao
<bean id="loginDao" class="servletTest.dao.impl.LoginDaoImpl" autowire="byName" />

8. sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
lazyLoadingEnabled="true"
useStatementNamespaces="true" />
<sqlMap resource="/ibatis/home/User.ibatis.xml"/>
</sqlMapConfig>

9. User.ibatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
<typeAlias alias="User" type="servletTest.model.User"/>
<resultMap id="User"
class="servletTest.model.User">
<result property="userId" column="user_id" />
<result property="name" column="name" />
<result property="sex" column="sex" />
<result property="age" column="age" />
</resultMap>
<select id="selectAllUsers" resultClass="User">
select * from usera
</select>
<select id="selectUser" resultClass="User" parameterClass="string">
select * from usera where user_id=#id#
</select>
<delete id="deleteUser" parameterClass="string">
delete from usera where user_id=#id#
</delete>
<insert id="saveUser" parameterClass="User">
insert into usera(user_id,name,sex,age) values(#userId#,#name#,#sex#,#age#)
</insert>
</sqlMap>
ok, 到这里就完成了spring与ibatis的整合了。不难发现ibatis与hibernate在与spring集成时。只是在 applicationCtx.xml中 区别定 义了自己的bean以及加载的配置文件。其它部分不需要任何改动,如 service,dao,controller等的配置。这很好的说明了 spring可其它框架集成的可插拔性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值