Spring + mybatis 集成

Posted 从精于一开始

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring + mybatis 集成相关的知识,希望对你有一定的参考价值。

具体项目可参照:
https://github.com/LuoXiaoyi/springmvc

一、环境准备:
Spring4.3.5 + Mybatis3.4.6 + Mybatis-Spring 1.3.2

 

二、pom.xml
创建maven工程,加入如下配置到pom.xml中

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>lxy.study</groupId>
<artifactId>springmvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp , 数据源的配置依赖 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- 为支持restful请求中对application/json格式的数据进行处理 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.4</version>
</dependency>
<!-- 为支持restful请求中对application/xml 格式的数据进行处理 -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.0</version>
</dependency>
<!-- For Log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- Servlet 3.1 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- 添加对JSTL的支持,用于处理JSP页面 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- mybatis 核心库 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 在项目中的pom.xml指定jdk版本 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- tomcat plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>9090</port>
<path>/springmvc</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>
</plugins>
</build>
</project>

三、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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
">
<import resource="applicationContext-tx.xml"></import>
<!-- Enable the use of matrix variables -->
<mvc:annotation-driven enable-matrix-variables="true">
<!--mvc:async-support task-executor="" default-timeout=""> 异步执行的配置
<mvc:callable-interceptors>
<bean></bean>
</mvc:callable-interceptors>
<mvc:deferred-result-interceptors>
<bean></bean>
</mvc:deferred-result-interceptors>
</mvc:async-support-->
</mvc:annotation-driven>
<!-- 启动注解 -->
<context:annotation-config></context:annotation-config>
<!-- 配置自动扫描根目录 -->
<context:component-scan base-package="lxy.study">
<!-- 这个是表示要排除掉扫描annotation注解的所有Controller -->
<!--ontext:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" /-->
</context:component-scan>
<!-- mvc中提供了默认的servlet来处理静态资源,即当所有的mapping都匹配不上的时候,
最后匹配到SimpleUrlHandlerMapping,这里主要是用于ModelAndView转发的时候,处理html/CSS等资源用 -->
<mvc:default-servlet-handler/>
</beans>

四、配置dataSource

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
">
<import resource="applicationContext-dataSource.xml"></import>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置自动事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

 

五、配置事务管理

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
">
<import resource="applicationContext-dataSource.xml"></import>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置自动事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

 

六、mybatis-config的配置

<?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>
<!-- 定义Mybatis的运行时行为 -->
<settings>
<!-- 自动映射配置:
NONE:取消自动映射
PARTIAL:对对象本身进行映射,不对嵌套进行映射,默认值
FULL:对嵌套进行映射
-->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- 开启延迟加载,默认是false -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 默认的延迟加载是:层级延迟加载,如果开启aggressiveLazyLoading,
则会完全使用延迟加载,需要什么加载什么,默认为false -->
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
<typeHandlers>
<!-- 同样可以通过配置包路径,扫描指定包下面的所有handler类 -->
<package name="com.lxy.study.core.typehandlers"/>
</typeHandlers>
<plugins>
<plugin interceptor="lxy.study.core.plugins.PagePlugin">
<property name="dialect" value="mysql"></property>
<property name="pageSqlId" value=".*ByPage"></property>
</plugin>
</plugins>
<!-- mapper映射器的引入包含多种方法
1. 通过mapper的resource属性导入xml的mapping配置文件 (推荐使用)
2. 通过mapper的class属性导入java类型的Interface接口
3. 通过mapper的url属性导入xml的mapping配置文件
4. 通过package标签,配置扫描的包路径,使mybatis自动装载包下面的所有mapper对象(@Mapper标注的)
需要注意的是,这里的方式可以混合使用,但是,同一个mapper不能在多种不同的方式中被包含,不然会有异常抛出 -->
<mappers>
<mapper resource="mappings/CountryMapping.xml"/>
</mappers>
</configuration>

 

以上是关于Spring + mybatis 集成的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis 如何集成到 Spring 框架中

spring boot集成mybatis+事务控制

Mybatis和Spring集成 sessionFactory.openSession() 事务不生效问题

Mybatis和Spring集成 sessionFactory.openSession() 事务不生效问题

重构Mybatis与Spring集成的SqlSessionFactoryBean(上)

Mybatis集成到spring boot