1.web.xml
<display-name>SSI</display-name>
<!-- 字符编码过滤器,防止乱码 -->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- load the Spring configuration file -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/springConfig/applicationContext.xml</param-value>
</context-param>
<!-- define the Spring listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 在web.xml中注册IntrospectorCleanupListener监听器以解决struts,quartz等框架可能产生的内存泄露问题 -->
<!-- 可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class loader 和由它管理的类 -->
<!-- <listener> -->
<!-- <listener-class> -->
<!-- org.springframework.web.util.IntrospectorCleanupListener -->
<!-- </listener-class> -->
<!-- </listener> -->
<!-- Struts配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<init-param>
<param-name>config</param-name>
<param-value>classpath:/struts.xml</param-value>
</init-param>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
2.struts.xml
<struts>
<!-- convertion插件配置 -->
<!-- ACTION后缀 -->
<constant name="struts.convention.action.suffix" value="Action" />
<!-- 缺省的包是什么(定义拦截器用) -->
<constant name="struts.convention.default.parent.package"
value="global" />
<!-- 是否禁止通过包扫描Action。默认值是false -->
<constant name="struts.convention.action.mapAllMatches" value="true" />
<!-- 慎用!Convention插件使用该常量指定的包作为搜寻Action的根包。对于actions.fore.LoginAction类,
按约定原本应映射到/fore/login;如果将该常量设为fore,则该Action将会映射到/login -->
<constant name="struts.convention.package.locators" value="action" />
<!-- 配置Convention插件自动重加载映射 -->
<constant name="struts.convention.classes.reload" value="true" />
<!-- 不扫描哪些包下面的java类作为action -->
<!-- <constant name="struts.Convention.exclude.packges" value="action"/> -->
<!-- 慎用!插件以该常量指定包作为根包来搜索Action类。 Convention插件除了扫描action,actions,struts,struts2四个包的类以外,还会扫描该常量指定的一个或多个包 -->
<!-- <constant name="struts.convention.action.packages" value="action"/> -->
<!-- 设置Convention插件定位视图资源的根路径。默认值为/WEB-INF/content -->
<!-- <struts.convention.result.path" value="path"/> -->
<!-- convertion插件配置 -->
<!-- spring配置,为了AOP,防止spring代理action时候出错 -->
<!-- <constant name="struts.objectFactory" value="spring"/> -->
<!-- 允许spring来创建Action、Interceptror和Result -->
<!-- <constant name="struts.objectFactory.spring.autoWire.alwaysRespect"
value="true" /> -->
<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 该属性指定需要Struts 2处理的请求后缀,多个后缀之间以(,)隔开。 -->
<constant name="struts.action.extension" value="action" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 开发模式下使用,详细错误提示+修改配置文件不用重启tomcat含有i18n.reload、configuration.xml.reload -->
<constant name="struts.devMode" value="true" />
<!-- 每次上传文件的总大小设置为不超过100MB -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 动态方法调用 ,为true时,就可以在struts.xml配置“*”的通配符 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 全局 -->
<package name="global" extends="struts-default,json-default">
</package>
</struts>
3.applicationContex.xml
<!-- <import resource="classpath:xxx.xml" /> -->
<!-- enable autowire -->
<!-- 向Spring注册BeanPostProcessor以识别注解(为了@Resource) -->
<!-- <context:annotation-config /> -->
<!-- 配置扫描范围 (其实已包含识别@Component@Controller@Service@Resource),扫描出的类默认bean名称为类名首字母小写 -->
<context:component-scan base-package="com.test"
use-default-filters="false">
<context:include-filter type="regex"
expression="com.test.manager.*" />
</context:component-scan>
<!-- @Resource没有显式提供名字的时候,如果根据默认名字找不到对应的Spring管理对象,注入机制会回滚至类型匹配(type-match)。
如果刚好只有一个Spring管理对象符合该依赖的类型,那么它会被注入。通过设置该属性为“false”(默认值是“true”)可以禁用这一特性。 -->
<!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
<property name="fallbackToDefaultTypeMatch" value="false" /> </bean> -->
<!-- 数据库配置,使用c3p0池 -->
<bean id="dataSource-mysql" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://127.0.0.1:3306/fortest?useUnicode=true&characterEncoding=utf8&autoReconnect=true&autoReconnectForPools=true" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="initialPoolSize" value="50" />
<property name="maxPoolSize" value="200" />
<property name="minPoolSize" value="50" />
<property name="maxIdleTime" value="60" />
</bean>
<bean id="sqlSessionFactory-mysql" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource-mysql" />
<property name="configLocation"
value="classpath:myBatisConfig/mybatisConfig-mysql.xml" /> </bean>
<bean id="transactionManager-mysql"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource-mysql" />
<property name="globalRollbackOnParticipationFailure" value="false" />
</bean>
<!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory-mysql" /> </bean> -->
<!-- 用于多数据源时指定数据源 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager-mysql"
/> -->
<!-- 配置多数据源使用sqlSessionFactoryBeanName -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory-mysql" />
<property name="basePackage" value="com.test.dao" />
</bean>
4.mybatisConfig-mysql
<?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>
<settings>
<setting name="cacheEnabled" value="true" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="multipleResultSetsEnabled" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="SIMPLE" />
<setting name="defaultStatementTimeout" value="25000" />
<!-- 配置日志,使用log4j -->
<!-- <setting name="logImpl" value="LOG4J"/> -->
</settings>
</configuration>
5.Dao层Mapper类与xml文件名称需要保持一致。
TestMapper.java
package com.test.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
public interface TestMapper {
public String getInfo();
}
TestMapper.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="com.test.dao.TestMapper">
<select id="getInfo" resultType="String">
SELECT VALUE FROM
test
WHERE
(ID = 1)
</select>
</mapper>
6.service层使用@Resource注解
IService.java
package com.test.manager.service;
public interface IService {
public String getInfo();
}
ServiceImpl.java
mapper类将被注册为名称为(类名首字母小写)的bean
package com.test.manager.service.impl;
import javax.annotation.Resource;
import com.test.dao.TestMapper;
import com.test.manager.service.IService;
public class ServiceImpl implements IService {
@Resource(name="testMapper")
private TestMapper testMapper;
public String getInfo() {
return testMapper.getInfo();
}
}
7.Action类
package com.test.manager.action;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
import com.test.manager.service.IService;
@Results({
@Result(name = "resultObj", type = "json", params = { "root",
"resultObj" }),
@Result(name = "resultArray", type = "json", params = { "root",
"resultArray" }) })
public class testAction extends ActionSupport {
private JSONObject resultObj = null;
// 该注释在未指定name属性时默认使用field名称作为bean name查找bean
@Resource(name="serviceImpl")
private IService service;
public String HelloWorld() {
Map result = new HashMap();
result.put("returnMessage", service.getInfo());
resultObj = JSONObject.fromObject(result);
return "resultObj";
}
public JSONObject getResultObj() {
return resultObj;
}
public void setResultObj(JSONObject resultObj) {
this.resultObj = resultObj;
}
}
8.结果