码迷,mamicode.com
首页 > 编程语言 > 详细

MyBatis-Spring-SpringMVC整合

时间:2019-06-25 13:37:40      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:bind   resources   edit   ppi   undle   日志系统   XML   mapping   encoding   

整合项目地址:https://github.com/mybatis/spring

一. 依赖包:

   》1.SpringMVC依赖包:

 

<!-- 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依赖包:

<!-- 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是具体的日志系统:通过slf4j-log4j12初始化Log4j,达到最终日志的输出 -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <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依赖包:

<!-- 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>

二、配置文件:创建Spring、SpringMVC、MyBatis的配置文件

  》1.创建MyBatis的配置文件(mybatis-config.xml)以及映射文件(mapper):

 

  》2.Spring的配置文件:(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">

    <!-- 包扫描 -->
    <context:component-scan base-package="cn.ll.action,cn.ll.dao,cn.ll.service"/>
    <!-- 加载数据库资连接信息资源文件 -->
    <context:property-placeholder location="classpath:database.properties"/>
    <!-- 启用Annotation注解 -->
    <aop:aspectj-autoproxy />
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${datasource.driver}"/>
        <property name="jdbcUrl" value="${datasource.url}"/>
        <property name="user" value="${datasource.username}"/>
        <property name="password" value="${datasource.password}"/>
        <property name="maxPoolSize" value="${datasource.maxPoolSize}"/> <!-- 最大连接数 -->
        <property name="initialPoolSize" value="${datasource.initialPoolSize}"/> <!-- 初始化连接数 -->
        <property name="minPoolSize" value="${datasource.minPoolSize}"/> <!-- 最小维持连接数 -->
        <property name="maxIdleTime" value="${datasource.maxIdleTime}"/>  <!-- 最大等待时间,毫秒 -->
    </bean>
    <!-- 注入JdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 事务-配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 事务-此时进行了AOP之中的切面关注点的操作配置,主要针对于事务管理生效 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes> <!-- 定义要匹配的操作业务方法 -->
            <tx:method name="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" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 事务-设置好事务的关注点之后,下面就需要考虑设置一个事务的横切表达式,找到业务层匹配 -->
    <aop:config>
        <aop:pointcut id="servicePointcut" expression="execution(* cn.ll.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>    <!-- 事务控制切入点 -->
    </aop:config>
    <!-- 整合MyBatis
        1.Spring管理所有组件,mapper的实现类;service调用Dao时,实现@Autowired:自动注入
        2.通过Spring管理实务
     -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 指定MyBatis的全部配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 指定Mapper.xml文件位置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <!-- 扫描所有的mapper接口,让这些mapper能够自动注入,base-package:指定mapper接口的包名 -->
    <mybatis-spring:scan base-package="cn.ll.dao" />
</beans>

 

  》3.SpringMVC的配置文件:(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">
    <!-- 包扫描 -->
    <context:component-scan base-package="cn.ll.action" />
    <!-- 启用控制层注解 -->
    <mvn:annotation-driven />
    <!--使用Dispatcher处理请求-->
    <mvn:default-servlet-handler />
    <!-- 定义页面资源解析处理类,匹配路径的前缀与后缀-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- 为保存在WEB-INF静态资源设置映射访问路径-->
    <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>
    </bean>
    <!-- 配置上传文件解析处理类 -->
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置整体上传文件的最大数据量,本次允许上传最大文件量为5M -->
        <property name="maxUploadSize" value="5242880"/>
        <!-- 设置单个上传文件的最大数据量,本次允许上传最大文件量为2M -->
        <property name="maxUploadSizePerFile" value="253952"/>
        <!-- 允许占用的最大内存量,本次设置为10M -->
        <property name="maxInMemorySize" value="10485760"/>
        <!-- 设置上传文件的临时保存目录,该目录的内容在每一次请求之后都需要清空 -->
        <property name="uploadTempDir" value="/tmp"/>
    </bean>
    <!-- 配置全局跳转页面 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
        <property name="exceptionMappings"><!-- 异常映射 -->
            <props>
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">plugins/errors</prop>
            </props>
        </property>
    </bean>
</beans>

  》4.在web.xml中启动Spring和SpringMVC:

<?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>
        <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>

 

MyBatis-Spring-SpringMVC整合

标签:bind   resources   edit   ppi   undle   日志系统   XML   mapping   encoding   

原文地址:https://www.cnblogs.com/luliang888/p/11080580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!