MyBatis-Spring-SpringMVC integration

Integration Project Address: https://github.com/mybatis/spring

A dependencies:

   "1.SpringMVC dependencies:

 

<!-- SpringMVC-核心 -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.1.7.RELEASE</version>
</dependency>
<!-- SpringMVC-文件上传 -->
<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.4</version>
</dependency>
<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.6</version>
</dependency>
<!-- SpringMVC-Restful -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.9</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.9.9</version>
</dependency>
<!-- servlet-jsp -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.3</version>
    <scope>provided</scope>
</dependency>

 

  "2.Spring dependencies:

<!-- Spring-IOC-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<!-- String-AOP -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.4</version>
</dependency>
<!- log4j logging system is specifically: by slf4j-log4j12 Log4j, initialized to a final output log -> 
    <the groupId> org.apache.logging.log4j </ the groupId>
<dependency>
    <artifactId>log4j-core</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.26</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.26</version>
    <scope>test</scope>
</dependency>
<!-- 测试 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.7.RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

  "3.MyBatis dependencies:

<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.1</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.1</version>
</dependency>

Second, the configuration file: Create Spring, SpringMVC, MyBatis configuration file

  "1. Create MyBatis configuration file (mybatis-config.xml) and map file (mapper):

 

  "2.Spring profile: (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:context="http://www.springframework.org/schema/context"
       xmlns:mvn="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

    <! - Pack scan -> 
    <context: Scan-base- Component Package = "cn.ll.action, cn.ll.dao, cn.ll.service" /> 
    ! <- load connection information database owned resources file -> 
    <context: Property-placeholder LOCATION = "the CLASSPATH: database.properties" /> 
    ! <- enable annotation comment -> 
    <AOP: AspectJ-autoproxy /> 
    ! <- database connection pool -> 
    < ID = the bean "the dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource"> 
        <Property name = "driverClass" value = "$ {datasource.driver}" /> 
        <Property name = "the jdbcUrl" value = "$ datasource.url} { "/> 
        <Property name =" User "value ="${datasource.username}"/>${datasource.username}"/>
        <property name="password" value="${datasource.password}"/>
        <property name="maxPoolSize" value="${datasource.maxPoolSize}"/> <!-- 最大连接数 -->
        <property name = "initialPoolSize" value = "$ {datasource.initialPoolSize}" /> <-! initialization connections -> 
        <Property name = "minPoolSize" value = "$ {datasource.minPoolSize}" /> <! - maintaining the minimum number of connections -> 
        <Property name = "maxIdleTime" value = "$ {datasource.maxIdleTime}" /> <-! maximum waiting time, in milliseconds -> 
    </ the bean> 
    <-! injection JdbcTemplate -> 
    <bean the above mentioned id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate"> 
        <Property name = "dataSource" ref = "dataSource" /> 
    </ bean> 

    <-! affairs - configuration Services Manager -> 
    <the bean ID = "the transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </ bean> 
    <-! affairs - at this time that the operational configuration section concerns among AOP, aimed to take effect on transaction management -> 
    <tx: advice the above mentioned id = "txAdvice" Transaction-Manager = "transactionManager" > 
        <TX:! Attributes> <- method of operation service definitions to match -> 
            <TX: method name = "the Add *" propagation = "REQUIRED" /> 
            <TX: method name = "* Create" = propagation " REQUIRED "/> 
            <TX: Method name =" Remove * "propagation =" REQUIRED "/> 
            <TX: Method name =" Delete * "propagation =" REQUIRED "/> 
            <TX: Method name =" Edit * "propagation = "REQUIRED" /> 
            <TX: Method name = "* Update" propagation = "REQUIRED"/>
            <tx:method name="login*" propagation="REQUIRED"/>
            <tx:method name="list*" propagation="REQUIRED" read-only="true"/>
            <tx: Method, name = "GET *" propagation = "REQUIRED" = the Read-only "to true" /> 
        </ tx: the Attributes> 
    </ tx: advice> 
    ! <- Services - set up after the affairs of concern, Here you need to consider setting up a transaction of cross-expression, find matching business layer -> 
    <AOP: config> 
        <AOP: pointcut the above mentioned id = "servicePointcut" = expression the "Execution (cn.ll.service * * * (.. ..)) "/> 
        <AOP: Advisor the advice-REF ="! txAdvice "the pointcut-REF =" servicePointcut "/> <- transaction control entry point -> 
    </ AOP: config> 
    ! <- integration MyBatis
         . 1 .Spring manage all components, mapper implementation classes; when service call Dao, achieve @Autowired: automatic injector
         2 by Spring management practices.
      -> 
    <the bean ID = "class="org.mybatis.spring.SqlSessionFactoryBean" > 
        <-! Data Source -> 
        <Property name = "the dataSource" REF = "the dataSource" /> 
        <- - All MyBatis specified profile!> 
        <Property name = "the configLocation" value = "CLASSPATH: MyBatis -config.xml "/> 
        ! <- designated Mapper.xml file location -> 
        <Property name =" mapperLocations "value =" the CLASSPATH:. Mapper / * xml "/> 
    </ bean> 
    <- scan all! the mapper interface mapper so that these can be automatically injected, base- package : Specifies the package name mapper interface -> 
    <MyBatis-Spring: Scan base- package = "cn.ll.dao" /> 
</ Beans>

 

  "3.SpringMVC profile: (spring-mvc.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:mvn="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc 
       HTTPS: // www.springframework.org/schema/mvc/spring-mvc.xsd "> 
    <- Pack scan ->! 
    <context: Scan-base- Component Package =" cn.ll.action "/> 
    ! <- enable control layer annotation -> 
    <mvn: annotation-Driven /> 
    <- processing requests using Dispatcher ->! 
    <mvn: default -servlet-Handler /> 
    <! - definition page analysis processing resource class, the prefix of suffix matching path -> 
    <the bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <Property name = "prefix" value = "/ the WEB-INF / Pages / "/> 
        <Property name =" suffix "value =."JSP "/> 
    </ bean> 
    <-! is stored in the WEB-INF static resource access path mapping settings ->
    <mvn:resources mapping="/js/**" location="/WEB-INF/js/" />
    <mvn:resources mapping="/css/**" location="/WEB-INF/css/" />
    <mvn:resources mapping="/images/**" location="/WEB-INF/images/" />
    <!-- 进行要加载的"*.properties"配置文件路径加载,直接通过CLASSPATH加载 -->
    <bean class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <array>
                <value>cn.ll.redis.message.pages</value>
                <value>cn.ll.redis.message.message</value>
            </array>
        </ Property> class
    <the bean
    <-! analysis processing based configuration file upload ->
    </ the bean>= "org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
        <-! setting overall maximum amount of data uploaded files, this allows a maximum file upload capacity of 5M -> 
        <Property name = "maxUploadSize" value = " 5242880 "/> 
        <! - the maximum amount of a single set of data uploaded file, this file the maximum amount allowed to upload 2M -> 
        <Property name =" maxUploadSizePerFile "value =" 253952 "/> 
        <! - allowed to occupy the the maximum amount of memory, this is set to 10M -> 
        <Property name = "maxInMemorySize" value = "10485760" /> 
        <-! temporary directory where you saved the upload file, the contents of the directory after every request needs to be cleared -> 
        <Property name = "uploadTempDir" value = "/ tmp" /> 
    </ bean> 
    <-! global configuration jump page -> 
    <bean class = "ORG.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > 
        <property name = "exceptionMappings"> < -! exception mapping -> 
            <The props>
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">plugins/errors</prop>
            </props>
        </property>
    </bean>
</beans>

  "4. Start Spring and SpringMVC in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- WEB容器启动的时候实现Spring容器的启动 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- SpringMVC的启动 -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
    <-! encoding filter ->
        <URL-pattern> / </ URL-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

Guess you like

Origin www.cnblogs.com/luliang888/p/11080580.html