Springmvc+mybatis

来源:互联网 发布:java大数据 编辑:程序博客网 时间:2024/05/22 12:58

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>weiyin</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:weiyin-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>weiyin</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <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>
  <listener>
    <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>
  <listener>
    <description>requestListener</description>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>404</error-code>
    <location>/404.html</location>
  </error-page>
</web-app>

Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:osgi="http://www.springframework.org/schema/osgi"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <context:component-scan base-package="com.weiyin">
        <context:exclude-filter type="regex" expression="com\.weiyin\.mobile\.controller..*" />
    </context:component-scan>

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>


    <!-- =================================================================== -->
    <!-- AOP: Configuration and Aspects                                      -->
    <!-- =================================================================== -->
    <aop:config>
        <aop:advisor id="managerTx" advice-ref="txAdvice"
            pointcut="execution(* *..*Service.*(..))" order="0" />
    </aop:config>

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="change*" propagation="REQUIRED" />
            <tx:method name="import*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="resume*" propagation="REQUIRED" />
            <tx:method name="execute*" propagation="REQUIRED" />
            <tx:method name="execute*" propagation="REQUIRED" />
            <tx:method name="send*" propagation="REQUIRED" />
            <tx:method name="*Login" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- JNDI DataSource for J2EE environments -->
    <!--<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/appfuse"/>-->
     <!--
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/oa2"></property>
    </bean>
     -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="initialPoolSize">
            <value>5</value>
        </property>
        <property name="minPoolSize">
            <value>5</value>
        </property>
        <property name="maxPoolSize">
            <value>10</value>
        </property>
        <property name="acquireIncrement">
            <value>5</value>
        </property>
        <property name="maxIdleTime">
            <value>30</value>
        </property>
        <property name="maxStatements">
            <value>1000</value>
        </property>
        <property name="numHelperThreads">
            <value>5</value>
        </property>        
        <property name="unreturnedConnectionTimeout">
            <value>600</value>
        </property>
        <property name="debugUnreturnedConnectionStackTraces">
            <value>true</value>
        </property>
    </bean>

    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:sqlMapConfig.xml</value>
        </property>
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

    <!-- 缓存 -->
    <bean id="cache" class="com.weiyin.cache.LocalCache">
        <constructor-arg value="0"/>
    </bean>
    
</beans>

weiyin-servlet.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:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
    
<!--     <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
            <bean class="com.mwteck.crm.util.BindingInitializer" />
        </property>
    </bean> -->
    
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">    
        <property name="converters">    
            <list>    
                <bean class="com.weiyin.util.DateConverter" />  
               <!--  <bean class="com.mwteck.crm.util.StringConverter" />  -->  
            </list>    
        </property>    
    </bean>  
    
    <mvc:annotation-driven conversion-service="conversionService">
         <mvc:argument-resolvers>
            <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
            <bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <!-- 扫描所有的controller -->
    <context:component-scan base-package="com.weiyin.mobile.controller" />
    <context:component-scan base-package="com.weiyin.util" />
    
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="5" />
    <task:annotation-driven scheduler="scheduler"
        executor="executor" />

    <bean id="exceptionResolver"
        class="com.weiyin.exception.BaseExceptionHandler">
        <property name="defaultErrorView" value="error_all" />
    </bean>
    
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5000000000" />
        <property name="maxInMemorySize" value="4096"/>
    </bean>

    <!-- <mvc:interceptors>
        <bean class="com.mwteck.crm.filter.SecurityInterceptor" />
    </mvc:interceptors>     -->
    
     <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
            <bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
        </mvc:argument-resolvers>
    </mvc:annotation-driven>
    <mvc:interceptors>
        <!-- On pre-handle, resolve the device that originated the web request -->
        <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
        <!-- On pre-handle, manage the user's site preference (declare after DeviceResolverHandlerInterceptor) -->
        <bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
    </mvc:interceptors>
    <bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
        <constructor-arg>
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/page/"></property>
                <property name="suffix" value=".jsp"></property>
            </bean>
        </constructor-arg>
        <property name="enableFallback" value="true" />
        <property name="mobilePrefix" value="mobile/" />
    </bean>
</beans>

sqlMapConfig.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>
    <settings>
        <setting name="cacheEnabled" value="false"/>
        <setting name="lazyLoadingEnabled" value="flase"/>
        <setting name="multipleResultSetsEnabled" value="true"/>
        <setting name="useColumnLabel" value="true"/>
        <setting name="useGeneratedKeys" value="false"/>
        <setting name="defaultExecutorType" value="REUSE"/>
        <!--
        <setting name="enhancementEnabled" value="false"/>
        <setting name="defaultStatementTimeout" value="25000"/>
        -->
    </settings>
    <typeAliases>
        <!-- 抽象POJO基类 -->
        <typeAlias alias="BaseObject" type="com.weiyin.base.BaseObject" />
        <typeAlias alias="TreeObject" type="com.weiyin.base.TreeObject" />
        
        <!-- 系统管理 -->
        <typeAlias alias="TMerchant" type="com.weiyin.entity.TMerchant" />
        
    </typeAliases>
    
    <plugins>
        <plugin interceptor="se.spagettikod.optimist.impl.OptimisticLockingInterceptor">
        </plugin>
    </plugins>
        
    <mappers>
        <!-- 全局sql配置 -->
        <mapper resource="Global.xml" />
        <!-- 系统管理 -->
        <mapper resource="com/weiyin/TMerchant.xml" />
    </mappers>
</configuration>

Global.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="Global">
    <resultMap id="BaseObjectMap" type="BaseObject">
        <id property="id" column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
    </resultMap>
    <resultMap id="BaseObjectMap_Long" type="BaseObject">
        <id property="id" column="id" javaType="java.lang.Long" jdbcType="BIGINT" />
    </resultMap>
    <resultMap id="BaseObjectMap_String" type="BaseObject">
        <id property="id" column="paramkey" javaType="java.lang.String" jdbcType="VARCHAR"  />
    </resultMap>
    <resultMap id="TreeObjectMap" type="TreeObject">
        <id property="id" column="id" javaType="java.lang.Integer"  jdbcType="INTEGER" />
        <result property="name" column="name" />
        <result property="parentId" column="parentid" javaType="java.lang.Integer" jdbcType="INTEGER" />
        <result property="parentIds" column="parentids" />
        <result property="nodeIndex" column="orderidx" />
    </resultMap>
    <update id="execute" parameterType="hashmap">
        ${sql}
    </update>
    <select id="queryOne" parameterType="hashmap" resultType="hashmap" >
        ${sql}
    </select>
    <select id="queryList" parameterType="hashmap" resultType="hashmap" >
        ${sql}
    </select>
</mapper>

TMerchant.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.weiyin.entity.TMerchant">
    
    <!-- 属性配置 -->
    <resultMap id="TMerchantMap" type="TMerchant" extends="Global.BaseObjectMap">
        <result property="floginPwd" column="floginPwd" />
        <result property="fmobilePhone" column="fmobilePhone" />
    </resultMap>
    
    <!-- 新增 -->
    <insert id="insert" parameterType="TMerchant">
        INSERT INTO tmerchant (fmobilePhone,floginPwd)
        VALUES (#{floginPwd},#{fmobilePhone})
    </insert>
    <!-- 根据FPhone查询TMerchant对象 -->
    <select id="getTMerchantByPhone" parameterType="string" resultType="TMerchant">
            SELECT tmerchant.FID, tmerchant.FMerchantNo, tmerchant.FBranchCompanyID, tmerchant.FAgencyID, tmerchant.FDealerID, tmerchant.FMerchantName,
            tmerchant.FLoginName, tmerchant.FLoginPwd, tmerchant.FSalt, tmerchant.FName, tmerchant.FIdentity, tmerchant.FEmail, tmerchant.FPostCode,
            tmerchant.FCityID, tmerchant.FPhone, tmerchant.FMobilePhone, tmerchant.FAdress, tmerchant.FRegisterDate, tmerchant.FRates, tmerchant.FIsAudit,
            tmerchant.FAuditDate, tmerchant.FType, tmerchant.FStatus, tmerchant.FBusinessLicence, tmerchant.FCardForntPath, tmerchant.FCardBackPath,
            tmerchant.FIdentityForntPath, tmerchant.FIdentityBackPath, tmerchant.FOrgnizationCode, tmerchant.FTaxCertificate, tmerchant.FApproverer,
            tmerchant.FApproveTime, tmerchant.FReason, tmerchant.FMerchantID, tmerchant.FCashType, tmerchant.FisClicked, tmerchant.FverifyCode,
            tmerchant.FContractPic, tmerchant.FDoorPic, tmerchant.FCashierPic, tmerchant.FManagementPic, tmerchant.FHousePic, tmerchant.FThreePartiesPic
            FROM tmerchant
            WHERE tmerchant.FMobilePhone=#{fmobilePhone}        
    </select>    
    <!-- 查询 -->
    <select id="find" parameterType="map" resultMap="TMerchantMap">
        <include refid="findSql"/>
        
        <!-- <if test="title != null">
            and title = #{title}
        </if>
        <if test="content != null">
            and content = #{content}
        </if>
        <if test="owner != null">
            and owner = #{owner}
        </if> -->
    </select>
    
    <!-- 查询记录数 -->
    <select id="count" parameterType="map" resultType="long">
        SELECT COUNT(*) FROM (<include refid="findSql"/>) X
    </select>
    
    <sql id="findSql">
        select a.* FROM tmerchant
    </sql>
    
</mapper>



0 0