其实这个和ssh2 基本差不多,就是sqlSessionFactory 和SessionFactory 有点区别
因为spring还没有支持Mybatis 需要mybatis-spring-1.0.2.jar 这个jar
因为MyBatis整合spring,struts2 和SSH2 基本差不多 ,就不详细了
1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Spring configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- spring加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 字符集过滤 -->
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 要过滤得类型 -->
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- 图片验证码 -->
<servlet>
<servlet-name>randCode</servlet-name>
<servlet-class>
com.yeshun.common.servlet.RandCodeServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>randCode</servlet-name>
<url-pattern>/servlet/randCode</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.spring ---applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="defaultAutoCommit" value="false" />
<property name="maxActive" value="150" />
<property name="maxIdle" value="100" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="5" />
<property name="testOnBorrow" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select 1 from dual" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations" value="classpath*:/com/yeshun/s2smb/entity/mapper/*Mapper.xml" />
<!-- 也可以在MyBatis配置文件里写Mapper映射,主要<mappers> 这个标签就行
<property name="configLocation" value="classpath:MyBatis.xml" />
-->
<property name="dataSource" ref="dataSource" />
</bean>
<!--jdbcTemplate-->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="baseDao" class="com.yeshun.common.dao.BaseDaoImpl" >
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- 使用annotation定义事务 start -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="aopPointcut"
expression="execution(* com.yeshun.s2smb.service.impl.*Impl.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="aopPointcut" />
</aop:config>
<!-- 使用annotation定义事务 end -->
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<context:component-scan base-package="com.yeshun" />
<!-- 采用注释的方式配置 aop -->
<aop:aspectj-autoproxy />
<!-- 采用annotation的方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--
<bean id="userService" class="com.yeshun.s2smb.service.impl.UserServiceImpl">
<property name="baseDao" ref="baseDao" />
</bean>
<bean id="LoginAction" class="com.yeshun.s2smb.action.LoginAction" scope="prototype">
<property name="userService" ref="userService" />
</bean>
-->
</beans>
在dao中Hibernate需要继承HibernateDaoSupport 而 MyBatis 则是SqlSessionDaoSupport
package com.yeshun.common.dao;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* @author yeshun
*/
public class BaseDaoImpl extends SqlSessionDaoSupport implements BaseDao {
private final Log logger = LogFactory.getLog(getClass());
protected JdbcTemplate jdbcTemplate;
private static final String INSERT = ".insert";
private static final String INSERT_Selective = ".insertSelective";
private static final String UPDATE = ".updateByPrimaryKey";
private static final String UPDATE_Selective = ".updateByPrimaryKeySelective";
private static final String DELETE = ".deleteByPrimaryKey";
private static final String SELECT = ".selectByPrimaryKey";
private static final String COUNT_BY_EXAMPLE = ".countByExample";
private static final String SELECT_BY_EXAMPLE = ".selectByExample";
public <T> void insert(Class<?> clazz, T entity) {
String mapId = clazz.getName() + INSERT_Selective;
getSqlSession().insert(mapId, entity);
}
public <T> void update(Class<?> clazz, T entity) {
String mapId = clazz.getName() + UPDATE_Selective;
getSqlSession().update(mapId, entity);
}
public <T> void delete(Class<?> clazz, T entity) {
String mapId = clazz.getName() + DELETE;
getSqlSession().delete(mapId, entity);
}
@SuppressWarnings("unchecked")
public <T> T get(Class<?> clazz, Object id) {
String mapId = clazz.getName() + SELECT;
return (T) getSqlSession().selectOne(mapId, id);
}
@SuppressWarnings("unchecked")
public <T> List<T> queryListByExample(Class<?> clazz, Object example) {
String querySql = clazz.getName() + SELECT_BY_EXAMPLE;
List<T> results = (List<T>) getSqlSession().selectList(querySql, example);
return results;
}
<!-- ... -->
}
附上项目源码点击打开链接