Spring IBatis Struts2 集成(包含junit)
??????? <param name="DatePattern" value=".yyyyMMddHH" />
??????? <layout />
??????? </layout>
??? </appender>
???
??? <root>
??????? <level value="info" />
??????? <appender-ref ref="sis_log_file" />
??? </root>
</log4j:configuration>
??????? <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="maxIdleTime" value="${maxIdleTime}"/>
??????? <property name="maxPoolSize" value="${maxPoolSize}"/>
??????? <property name="minPoolSize" value="${minPoolSize}"/>
??????? <property name="initialPoolSize" value="${initialPoolSize}"/>
??????? <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"/>
</bean>
<!DOCTYPE sqlMapConfig???? ?
??? PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"???? ?
??? "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
??? <settings cacheModelsEnabled="true" useStatementNamespaces="true"/>
??? ?
??? <!-- 此处开始添加SqlMap配置文件 -->
</sqlMapConfig>
??????? <property name="configLocation" value="classpath:SqlMapConfig.xml" /> ?
??? </bean>
?? ?
??? <!-- Spring的IBatis模板 -->
??? <bean id="sqlMapClientTemplate" ref="sqlMapClient"></property> ?
??? </bean>
?? ?
??? <!-- 事务管理配置 -->
??? <bean id="transactionManager" ref="dataSource" />
??? </bean>
?? ?
??? <tx:annotation-driven transaction-manager="transactionManager" />
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="USER" >
? <resultMap id="BaseResultMap" >
??? <result column="USER_ID" property="userId" jdbcType="DECIMAL" />
??? <result column="USER_TYPE" property="userType" jdbcType="DECIMAL" />
??? <result column="USER_STATUS" property="userStatus" jdbcType="DECIMAL" />
??? <result column="USER_NAME" property="userName" jdbcType="VARCHAR" />
??? <result column="USER_PASSWD" property="userPasswd" jdbcType="VARCHAR" />
??? <result column="CREATE_TIME" property="createTime" jdbcType="DATE" />
??? <result column="UPDATE_TIME" property="updateTime" jdbcType="DATE" />
??? <result column="LAST_LOGIN_TIME" property="lastLoginTime" jdbcType="DATE" />
? </resultMap>
?
? <select id="selectByUserIdOrUserName" resultMap="BaseResultMap" parameter>
??? select USER_ID, USER_TYPE, USER_STATUS, USER_NAME, USER_PASSWD, CREATE_TIME, UPDATE_TIME,
????? LAST_LOGIN_TIME
??? from TEST_PTL_USER
??? <dynamic prepend=" where" >
????? <isNotNull prepend="or" property="userId" >
??????? USER_ID = #userId:DECIMAL#
????? </isNotNull>
????? <isNotNull prepend="or" property="userName" >
??????? USER_NAME= #userName:VARCHAR#
????? </isNotNull>
??? </dynamic>
? </select>
?
? <delete id="deleteByUserId" parameter>
??? delete from TEST_PTL_USER
??? where USER_ID = #value#
? </delete>
</sqlMap>
2、Spring 配置文件ApplicationContext.xml的完整内容:
<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:jee="http://www.springframework.org/schema/jee"
?????? 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/jee
??????????????? http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
??? <!-- 载入资源文件,下面的dataSource就引用了资源文件中的配置项 -->
??? <context:property-placeholder location="classpath:jdbc.properties"/>
?? ?
??? <context:component-scan base-package="cn.aofeng.sis" />
??? <context:annotation-config />
?? ?
??? <!-- 数据源配置 -->
??? <bean id="dataSource" 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="maxIdleTime" value="${maxIdleTime}"/>
??????? <property name="maxPoolSize" value="${maxPoolSize}"/>
??????? <property name="minPoolSize" value="${minPoolSize}"/>
??????? <property name="initialPoolSize" value="${initialPoolSize}"/>
??????? <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"/>
??? </bean>
?? ?
??? <!-- IBatis ORM 操作类 -->
??? <bean id="sqlMapClient" ref="dataSource" /> ?
??????? <property name="configLocation" value="classpath:SqlMapConfig.xml" /> ?
??? </bean>
?? ?
??? <!-- Spring的IBatis模板 -->
??? <bean id="sqlMapClientTemplate" ref="sqlMapClient"></property> ?
??? </bean>
?? ?
??? <!-- 事务管理配置 -->
??? <bean id="transactionManager" ref="dataSource" />
??? </bean>
?? ?
??? <tx:annotation-driven transaction-manager="transactionManager" />
</beans>
<!DOCTYPE sqlMapConfig???? ?
??? PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"???? ?
??? "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
??? <settings cacheModelsEnabled="true" useStatementNamespaces="true"/>
??? ?
??? <sqlMap resource ="cn/aofeng/sis/domain/USER_SqlMap.xml" />
</sqlMapConfig>
1、<context:component-scan/>说明。
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
2、<context:annotationconfig/>说明。
<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
?
3、<tx:annotation-driven /> 说明。
<tx:annotation-driven /> 表示使用声明式事务。如果用 'transactionManager' 来定义 PlatformTransactionManager bean的名字的话,你就可以忽略 <tx:annotation-driven/> 标签里的 'transaction-manager' 属性。 如果 PlatformTransactionManager bean你要通过其它名称来注入的话,你必须用 'transaction-manager' 属性来指定它。