工作笔记struts+mybatis+spring

jsp页面注意表单form id <select name="select_term" id="select_term">
     <option></option>
     <option value="RE_GOODS_CODE">货物编号</option>
     <option value="RE_OLD_CODE">流水号</option>
     <option value="RE_GOODS_NAME">货物名称</option>
   </select>&nbsp;&nbsp;
<input type="text" style="100px;" name="text_term" id="text_term" />
&nbsp;少货类型
   <select name="si_type" id="si_type">
     <option></option>
     <option value="4">少货</option>
     <option value="3">串货</option>
     <option value="1">内埠剩货</option>
     <option value="2">外埠剩货</option>
   </select>&nbsp;&nbsp;
js页面
//页面初始化
$(function(){
 // 当窗口大小发生变化时,调整DataGrid的大小
  $(window).resize(function() {
   $('#reData').datagrid('resize', {
    ($(window).width()*0.995),
    height:($(window).height()*0.88)
   });
   widthValue = $(window).width()*0.995;
   heightValue = $(window).height()*0.88;
  });
  $('#reData').datagrid('getPager').pagination({
        beforePageText: '第',//页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',  
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
    });
 //查询
function dosubmit(){
 
   //查询参数
   var queryParams = {
   'sl_start_time':$("#sl_start_time").val(),
   'sl_end_time':$("#sl_end_time").val(),
   'select_term':$("#select_term").val(),
   'text_term':$("#text_term").val(),
   'si_type':$("#si_type").val(),
   'grSendName':$("#grSendName").val(),
   'grSendCode':$("#re_send_code").val(),
   'ghSendCode':$("#re_send_hcode").val(),
   'grArriveName':$("#grArriveName").val(),
   'ghArriveCode':$("#re_arrive_hcode").val(),
   'grArriveCode':$("#re_arrive_code").val() 
 };
 
 //获得列表数据
 var dataList=$('#reData').datagrid('getData');
 
 //判断是否有数据
 if(dataList.total > 0){
  //查询明细
  $('#reData').datagrid('load', queryParams);
 }
 else{
  //查询明细
  $('#reData').datagrid({
      url:"scanLessQuery!select",
      queryParams: queryParams,
  });
 }
 
 $('#reData').datagrid('getPager').pagination({
        beforePageText: '第',//页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',  
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
    });
 
    return true;
}
/**
 * 查询条件验证
 * @returns {Boolean}
 */
function checkPage(){
 var startTime = $("#sl_start_time").val().replace(/\-/g, "" );
 var endTime = $("#sl_end_time").val().replace(/\-/g, "" );
 if(parseInt(startTime)>parseInt(endTime)){
  $.messager.alert('提示信息','起始时间不能大于结束时间!','warning');
  return true;
 }
 return false;

机构码
select gr_name,gr_hcode from T_GROUP t order by gr_hcode
机构名称,层次码
//将查询结果给jq中的grid
public String select(){
  ScanLessQuery sg = new ScanLessQuery();
  sg = queryParameter();
        jsonObject = scanLessQueryService.select(sg);
  return "jsonObject";
 }
 //通过jq给属性赋值,把属性值赋给实体类
 private ScanLessQuery queryParameter() {
  ScanLessQuery sg = new ScanLessQuery();
  // 当前页
  intPage = Integer.parseInt((page == null || page == "0") ? "1" : page);
  // 每页显示条数
  number = Integer.parseInt((rows == null || rows == "0") ? "25" : rows);
  // 每页的开始记录 第一页为1 第二页为number +1
  start = (intPage - 1) * number;
  sg.setPage_start(start);
  sg.setPage_end(start + number);
  // 页面查询时间类型判断
  sg.setSl_start_time(sl_start_time);
  sg.setSl_end_time(sl_end_time);
  sg.setGrSendName(grSendName);
  sg.setGrSendCode(grSendCode);
  sg.setGhSendCode(ghSendCode);
  sg.setGrArriveCode(grArriveCode);
  sg.setGrArriveName(grArriveName);
  sg.setGhArriveCode(ghArriveCode);
        sg.setSi_type(si_type);
        sg.setSelect_term(select_term);
  sg.setText_term(text_term);
  return sg;
 }
service中,拼装jsonObject,格式。
 public JSONObject select(ScanLessQuery scanLessQuery) {
  List<ScanLessQuery> result = scanLessQueryDao.select(scanLessQuery);
  int count = scanLessQueryDao.selectCount(scanLessQuery);
  Map<String, Object> jsonMap = new HashMap<String, Object>();
  jsonMap.put("total", count);
  jsonMap.put("rows", result);
  return JSONObject.fromObject(jsonMap);
 }
  dao中通过mybatis将xml和dao关联起来
  public interface ScanLessQueryDao {
 
 /**
  * 查询数据
  * @param scanLessQuery
  * @return
  */
 public List<ScanLessQuery> select(ScanLessQuery scanLessQuery);
 
 /**
  * 查询数据条数
  * @param scanLessQuery
  * @return
  */
 public int selectCount(ScanLessQuery scanLessQuery);
 /**
  * 导出数据
  * @param scanLessQuery
  * @return
  */
 public List<ScanLessQuery> selectForExcel(ScanLessQuery scanLessQuery);
 
}
注意名称是xml中的名称
<mapper namespace="com.strong.cjtransit.dao.scan.ScanLessQueryDao">
 <!-- 查询数据 -->
 <select id="select" resultType="ScanLessQuery" parameterType="ScanLessQuery">
  <![CDATA[
 SELECT RE_GOODS_CODE,
       RE_GOODS_NAME,
       RE_GOODS_COUNT,
       RE_GROUP_NAME,
       RE_TARGETGOODS_NAME,
       SL_TYPE,
       SL_DUTY_GROUP,
       SL_EXCEPTION_COUNT,
       RE_RECEIVE_MAN,
       RE_PAYED_MONEY,
       RE_ARRIVE_PAY,
       RE_MAT_TRAFFIC,
       RE_GETS_GOODS
  FROM (SELECT RE_GOODS_CODE,
               RE_GOODS_NAME,
               RE_GOODS_COUNT,
               RE_GROUP_NAME,
               RE_TARGETGOODS_NAME,
               SL_TYPE,
               SL_DUTY_GROUP,
               SL_EXCEPTION_COUNT,
               RE_RECEIVE_MAN,
               RE_PAYED_MONEY,
               RE_ARRIVE_PAY,
               RE_MAT_TRAFFIC,
               RE_GETS_GOODS,
               ROWNUM AS ROWNO
          FROM (SELECT RE_GOODS_CODE,
                       RE_GOODS_NAME,
                       RE_GOODS_COUNT,
                       (SELECT GR_NAME
                          FROM T_GROUP
                         WHERE GR_CODE = RE_GROUP_CODE) AS RE_GROUP_NAME,
                       (SELECT GR_NAME
                          FROM T_GROUP
                         WHERE GR_CODE = RE_TARGETGROUP_CODE) AS RE_TARGETGOODS_NAME,
                       (CASE SL_TYPE
                         WHEN 1 THEN
                          '内埠剩货'
                         WHEN 2 THEN
                          '外埠剩货'
                         WHEN 3 THEN
                          '串货'
                         WHEN 4 THEN
                          '少货'
                         ELSE
                          ''
                       END) AS SL_TYPE,
                       (SELECT GR_NAME
                          FROM T_GROUP
                         WHERE GR_CODE = SL_DUTY_GROUP) AS SL_DUTY_GROUP,
                       SL_EXCEPTION_COUNT,
                       RE_RECEIVE_MAN,
                       RE_PAYED_MONEY,
                       RE_ARRIVE_PAY,
                       RE_MAT_TRAFFIC,
                       RE_GETS_GOODS
                  FROM T_RECEIVE
                  JOIN T_SCANNING_LESS
                    ON RE_RECEIVE_CODE = SL_RECEIVE_CODE
  WHERE SL_SEND_DATE >= TO_DATE( #{sl_start_time} ,'YYYY-MM-DD') ]]>
      <![CDATA[ AND SL_SEND_DATE < TO_DATE( #{sl_end_time} ,'YYYY-MM-DD')+1
     ]]>
     <!-- 查询条件 -->
  <if test="select_term != '' and text_term != ''">
    <![CDATA[
     AND ${select_term} LIKE '%' || #{text_term} || '%'
     ]]>
  </if>
  <!-- 类别不为空 -->
  <if test="si_type!=null and si_type !=''">
   AND SL_TYPE = #{si_type}
  </if>
  <if test="grArriveCode!=null and grArriveCode !=''">
   AND RE_TARGETGROUP_CODE IN (SELECT GR_CODE FROM T_GROUP
   WHERE GR_HCODE LIKE #{ghArriveCode} || '%')
  </if>
  <if test="grSendCode !=null and grSendCode !=''">
   AND RE_GROUP_CODE IN (SELECT GR_CODE FROM T_GROUP WHERE
   GR_HCODE LIKE #{ghSendCode} || '%')
  </if>
  ) A
  <![CDATA[
  WHERE ROWNUM <= #{page_end}
  ]]>
  ) B
 <![CDATA[
 WHERE B.ROWNO > #{page_start}
 ]]>
 </select>
 注意daoxml中的oracle 分页查询
  ***需要修改的配置文件***
  在web-info下的class中
  1.mybatis-config.xml
  配置类的别名,注意新建实体类一定要修改此文件
  配置映射,配置daoxml的位置
  2.struts-config配置
   
  <!-- 扫描少货信息查询 -->
  <action name="scanLessQuery" class="scanLessQueryAction">
   <result name="main">/WEB-INF/jsp/scan/scan_less_query.jsp</result>
   <result name="jsonObject" type="json">
     <param name="root">jsonObject</param>
   </result>
   <result name="excel" type="stream"> 
           <param name="contentType">application/vnd.ms-excel</param> 
           <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> 
           <param name="bufferSize">1024</param>
           <param name="inputName">excelFile</param> 
             </result>
   <interceptor-ref name="mydefault" />
  </action>
  配置action 配置result等
  注意class中对应地址在spring中相对应
 3.spring-config起到胶水的作用
 例如:
 <!-- 货物扫描信息查询dao -->
 <bean id="scanGoodsQueryDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="com.strong.cjtransit.dao.scan.ScanGoodsQueryDao"/>
  <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
 </bean>
  <!-- 货物扫描信息查询service -->
 <bean id="scanGoodsQueryService" class="com.strong.cjtransit.service.scan.ScanGoodsQueryService">
  <property name="scanGoodsQueryDao" ref="scanGoodsQueryDao"></property>
 </bean>
  <!-- 货物扫描信息查询Action -->
 <bean id="scanGoodsQueryAction"  class="com.strong.cjtransit.action.scan.ScanGoodsQueryAction" scope ="prototype">
  <property name="scanGoodsQueryService" ref="scanGoodsQueryService"></property>
 </bean> 
 4.applicationContext-default.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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
               
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations">
      <list>
       <value>classpath:bonecp.properties</value>
       <value>classpath:report_name_cn.properties</value>
      </list>
     </property>
    </bean>
   
<!-- 配置主库数据源,bonecp连接池配置 -->
  <bean id="dataSource" destroy-method="close" class="com.jolbox.bonecp.BoneCPDataSource">
        <property name="driverClass" value="${bonecp.driverClassName}"/>
        <property name="jdbcUrl" value="${bonecp.url}"/>
        <property name="username" value="${bonecp.username}"/>
        <property name="password" value="${bonecp.password}"/>
        <!-- 检查数据库连接池中空闲连接的间隔时间 -->
        <property name="idleConnectionTestPeriod" value="${bonecp.idleConnectionTestPeriod}"/>

        <!--连接池中未使用的链接最大存活时间-->
        <property name="idleMaxAge" value="${bonecp.idleMaxAge}"/>

        <!--设置每个分区含有connection最大个数-->
        <property name="maxConnectionsPerPartition" value="${bonecp.maxConnectionsPerPartition}"/>

        <!--设置每个分区含有connection最小个数-->
        <property name="minConnectionsPerPartition" value="${bonecp.minConnectionsPerPartition}"/>

        <!--设置每个分区数-->
        <property name="partitionCount" value="${bonecp.partitionCount}"/>

        <!--设置分区中的connection增长数量-->
        <property name="acquireIncrement" value="${bonecp.acquireIncrement}"/>

        <!--设置连接池阀值-->

        <property name="poolAvailabilityThreshold" value="${bonecp.poolAvailabilityThreshold}"></property>
        <!--连接时间-->
        <property name="connectionTimeout" value="${bonecp.connectionTimeout}"></property>
    </bean>
   
    <!-- 配置MyBatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:MyBatis-Config.xml"></property>
        <property name="dataSource" ref="dataSource" />
    </bean>

   
 <bean id="commonQuery" class="com.strong.cjtransit.entity.common.CommonQuery"/>
 <!-- ========================TransactionManager============================ -->
 <!-- spring 管理事务 -->
 <bean id="myTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 
 <!-- =======================advice====================================== -->
 <tx:advice id="txAdvice" transaction-manager="myTxManager">
  <!-- 声明事务规则 主库-->
  <tx:attributes>
   <tx:method name="find*" propagation="REQUIRED" read-only="false" />
   <tx:method name="check*" propagation="REQUIRED" read-only="false" />
   <tx:method name="test*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
   <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
   <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
   <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
   <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
   <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
   <tx:method name="reg*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
   <tx:method name="*" propagation="SUPPORTS" read-only="false" />
  </tx:attributes>
 </tx:advice>
    <aop:config>
    <aop:pointcut id="canyin" expression="execution(* com.strong.cjtransit.service..*.*(..))"/>
   <aop:advisor advice-ref="txAdvice" pointcut-ref="canyin" />
    </aop:config>
   
</beans>
配置连接池,配置事务,配置spring文件
5.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">
   
<struts> 
 <constant name="struts.i18n.encoding" value="UTF-8" />
 <!-- 设置浏览器是否缓存静态内容 -->
 <constant name="struts.serve.static.browserCache" value="false" />
 <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false
 <constant name="struts.configuration.xml.reload" value="true" />-->
 <!-- 打印出更详细的错误信息 -->
 <constant name="struts.devMode" value="true" />

 
 <package name="cjtransit" extends="struts-default,json-default">
 
  <!-- 定义一个拦截器 --> 
  <interceptors> 
   <interceptor name="sessionout"  class="interceptor.SessionIterceptor"></interceptor> 
   <!-- 拦截器栈 -->
   <interceptor-stack name="mydefault">
      <interceptor-ref name="defaultStack" />
      <interceptor-ref name="sessionout"/>
   </interceptor-stack>
  </interceptors> 
  <!-- 定义全局Result -->
  <global-results> 
   <!-- 限制使用系统提示界面 -->
   <result name="none">/WEB-INF/jsp/restricted_use.jsp</result>
   <!-- 跳转到error页面 -->
   <result name="error">/error.jsp</result>
   <!-- 当返回login视图名时,转入/login.jsp页面 -->
   <result name="login">/WEB-INF/jsp/redirect_login.jsp</result>
  </global-results>
       
  </package>
 
 <include file="struts-config/struts-*.xml"></include>
</struts>
 

原文地址:https://www.cnblogs.com/pond/p/5357793.html