struts2.1.8,spring3.1.1,mybatis3.1.1整合
mybatis 与spring整合最重要的包。mybatis-spring-1.1.1.jar
bean包中User实体。
private int id;
private String name;
private String password;
private Date birthDate;
get。。。set。。。方法。。
iface包中。。
interface IUserDao
public List<User> selectUserById(int id);
public class IUserDaoImpl extends SqlSessionDaoSupport implements IUserDao{
public List<User> selectUserById(int id) {
return getSqlSession().selectList("selectUserById", id);
}
}
service包中。
interface IUserService
public List<User> selectUserById(int id);
public class IUserServiceImpl implements IUserService{
private IUserDao serviceUserDao;
public IUserDao getServiceUserDao()
{
return serviceUserDao;
}
public void setServiceUserDao(IUserDao serviceUserDao)
{
this.serviceUserDao = serviceUserDao;
}
public List<User> selectUserById(int id) {
return serviceUserDao.selectUserById(id);
}
}
mybatis-config.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="User" type="com.zz.bean.User"/>
</typeAliases>
<mappers>
<mapper resource="sqlMap/User.xml" />
</mappers>
</configuration>
sqlMap包下面User.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="user">
<resultMap type="User" id="userResultMap">
<id property="id" column="ID"/>
<result property="name" column="NAME"/>
<result property="password" column="PASSWORD"/>
<result property="birthDate" column="birthDate" javaType="java.util.Date"/>
</resultMap>
<select id="selectUserById" parameterType="java.lang.String" resultType="User">
<![CDATA[
SELECT * FROM USER SU
WHERE SU.ID = #{id}
]]>
</select>
</mapper>
application 文件中。。。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
<!-- 数据源属性配置文件 -->
<context:property-placeholder location="classpath:dataSource.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<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="autoCommitOnClose" value="true"/>
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
<property name="minPoolSize" value="${cpool.minPoolSize}"/>
<property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
<property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
<property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
</bean>
<!-- 数据连接管理 -->
<!-- 数据库事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userServices" parent="transactionProxy">
<property name="target">
<bean class="com.zz.service.IUserServiceImpl">
<property name="serviceUserDao">
<ref bean="mapperDao" />
</property>
</bean>
</property>
</bean>
<bean id="transactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager"
ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="create*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="delete*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="update*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="dispatch*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="receive*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="revert*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="reply*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="insert*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="repair*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="*">
PROPAGATION_REQUIRED,ISOLATION_DEFAULT
</prop>
</props>
</property>
</bean>
<bean id="mapperDao" class="com.zz.iface.IUserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
dataSource.properties文件:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ruixi?characterEncoding=GBK
jdbc.username=root
jdbc.password=root
cpool.checkoutTimeout=5000
cpool.minPoolSize=20
cpool.maxPoolSize=50
cpool.maxIdleTime=7200
cpool.maxIdleTimeExcessConnections=1800
cpool.acquireIncrement=10
struts.properties
struts.objectFactory=spring
struts.enable.DynamicMethodInvocation=false
struts.devMode=false
struts.locale=zh_CN
struts.i18n.encoding=GBK
其它只要使用过s2sh架构的,都很清楚,几乎一样。