`
zscomehuyue
  • 浏览: 401632 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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可其它框架集成的可插拔性。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics