struts/spring/mybatis的整合

struts/spring/mybatis的整合

1.struts配置:struts-*.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 "http://struts.apache.org/dtds/struts-2.3.dtd">

<!-- START SNIPPET: xworkSample -->
<struts>

    <!-- Some or all of these can be flipped to true for debugging -->
    <constant name="struts.i18n.reload" value="false" />
    <!-- 设置开发模式,可以在改了配置文件后不需要重启服务器,刷新浏览器即可,在完成开发后要设置为false -->
    <constant name="struts.devMode" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 设置国际化全局的资源文件的基名 -->
    <constant name="struts.custom.i18n.resources" value="globalMessages" />
    <!-- 设置请求的后缀名,可以设置多个,用逗号隔开-->
    <constant name="struts.action.extension" value="action,do," />
    <!--上传文件的大小限制
    <constant name="struts.multipart.maxSize" value="10701096"/> -->
 <!--上传文件的临时目录
 <constant name="struts.multipart.saveDir" value="d://"/>-->
 <!-- 集成spring,action对象让spring容器来创建 org.apache.struts2.spring.StrutsSpringObjectFactory    -->
 <constant name="struts.objectFactory" value="spring" />
 <constant name="" value=""></constant>
 <!-- 国际化编码 -->
 <constant name="struts.i18n.encoding" value="UTF-8" /> 
 <!-- 浏览器是否缓存静态内容 ,开发阶段最好关闭-->
 <constant name="struts.serve.static.browserCache" value="false"/> 
 <!--
        指定spring框架的装配模式,装配方式有: name, type, auto, and constructor (name   
        是默认装配模式)   
    -->  
    <constant name="struts.objectFactory.spring.autoWire" value="name" />
   
    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor-stack name="crudStack">
                <interceptor-ref name="checkbox" />
                <interceptor-ref name="params" />
                <interceptor-ref name="staticParams" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
           
            <interceptor-stack name="authStack">
             <interceptor-ref name="authority"></interceptor-ref>
             <interceptor-ref name="exception"></interceptor-ref>
             <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
           
            <interceptor name="authority" class="cn.web.interceptor.AuthorityInterceptor"></interceptor>
        </interceptors>
       
        <!-- 异常处理后,能够提供友好的用户界面 -->
        <global-results>
   <result name="exceptionPage">/pages/fail.jsp</result>
  </global-results>
  <global-exception-mappings>
   <exception-mapping result="exceptionPage" exception="java.lang.Exception" />
  </global-exception-mappings>

    </package>

 
 
 <package name="sys" extends="default" namespace="/sys">
        <default-interceptor-ref name="crudStack"/>
  <!-- action使用spring的bean -->
        <action name="*_*" class="{2}" method="{1}">
         <result name="userlist">/WEB-INF/pages/user/list.jsp</result>
         <result name="useradd">/WEB-INF/pages/user/add.jsp</result>
         <result name="userupdate">/WEB-INF/pages/user/update.jsp</result>
         <result name="userlook">/WEB-INF/pages/user/look.jsp</result>
         
        </action>
 </package>
 
</struts>

public class UserAction extends ActionSupport{
 
 private IUserService userService;
 private UserVO userVO = new UserVO();
 
 private static final Logger log = Logger.getLogger(UserAction.class);
 
 
 public String list(){
  return "userlist";
 }
 
}

2.web.xml
<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   <init-param>
    <param-name>config</param-name>
    <param-value>
     struts-default.xml,struts-plugin.xml,struts.xml
    </param-value>
   </init-param> 
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring/applicationContext*.xml</param-value>
  </context-param>
 
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
  3.spring 与mybatis的集成配置: applicationContext-*.xml
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="url" value="jdbc:mysql://localhost:3306/test2"></property>
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="username" value="root"></property>
  <property name="password" value="password"></property>
  <property name="initialSize" value="2"></property>
  <property name="maxActive" value="100"></property>
  <property name="maxIdle" value="20"></property>
  <property name="minIdle" value="10"></property>
  <property name="validationQuery" value="SELECT 1"></property>
 </bean>
 
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="configLocation" value="classpath:configuration.xml"></property>
  <property name="mapperLocations" value="classpath:mybatis/**/*.xml"></property>
 </bean>
 <!--  普通处理的session -->
 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
  <constructor-arg index="1" value="SIMPLE" />
 </bean>
 
 <!-- spring的事务管理器 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 
 <!-- 声明式容器事务管理,事务管理器transactionManager -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="add*" propagation="REQUIRED"/>
   <tx:method name="insert*" propagation="REQUIRED"/>
   <tx:method name="update*" propagation="REQUIRED"/>
   <tx:method name="query*" read-only="true"/>
   <tx:method name="*" propagation="REQUIRED"/>
  </tx:attributes>
 </tx:advice>
 
 <aop:config expose-proxy="true">
  <!-- 只对业务逻辑层实施事务 -->
  <aop:pointcut id="txPointcut" expression="execution(* cn.demo.service..*.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
 </aop:config>
 
 <context:component-scan base-package="cn.demo.action"/>

 <bean id="userAction" class="cn.demo.action.UserAction" scope="prototype">
  <property name="userService" ref="userService"></property>
 </bean>
 
4.页面访问Action: sys/list_userAction.action

扫描二维码关注公众号,回复: 741450 查看本文章

5.mybatis配置:*mapper.xml, configuration.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="cn.demo.dao.IUserDao" >

 <resultMap id="userMap" type="cn.demo.model.po.UserPO">
  <id property="userId" column="USERID" ></id>
  <result property="userName" column="USERNAME"></result>
  <result property="birthday" column="BIRTHDAY"></result>
  <result property="salary" column="SALARY"></result>
<!--   <result property="birthdayStr" column="BIRTHDAY" typeHandler="myDateHandler"></result> -->
 </resultMap>
 
 <sql id="userFields">
  USERID, USERNAME, BIRTHDAY, SALARY
 </sql>
 <select id="selectById" parameterType="cn.demo.model.po.UserPO" resultType="cn.demo.model.po.UserPO">
  select
  <include refid="userFields"></include>
  from user
  where USERID = #{userId}
 </select>
</mapper>

<?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" />
  <!-- 允许JDBC支持生成的键 -->
  <setting name="useGeneratedKeys" value="false" />
  <!-- 配置默认的执行器 -->
  <setting name="defaultExecutorType" value="SIMPLE" />
  <!-- 设置超时时间 -->
  <setting name="defaultStatementTimeout" value="25000" />
 </settings>
 </configuration>
 
 
 

猜你喜欢

转载自zw7534313.iteye.com/blog/2253439
今日推荐