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 集成的更多相关文章
- maven,spring,mybatis集成错误
maven,spring,mybatis集成的时候单元测试junit测试没问题,但mvn jetty:run 就报错误 错误: org.apache.ibatis.binding.BindingExc ...
- SpringMVC+Spring+Mybatis -- 集成之旅
准备 首先介绍一下,我的工具使用的是STS, 需要的童鞋可以到官网下载:http://spring.io/tools/sts/all 使用STS是因为她集成了Maven进行 “包“ 管理以及自带 We ...
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- Spring + Mybatis 集成原理分析
由于我之前是写在wizNote上的,迁移过来比较浪费时间,所以,这里我直接贴个图片,PDF文件我上传到百度云盘了,需要的可直接下载. 地址:https://pan.baidu.com/s/12ZJmw ...
- spring Mybatis集成
pom文件 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:/ ...
- MyBatis6:MyBatis集成Spring事物管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...
- MyBatis5:MyBatis集成Spring事物管理(上篇)
前言 有些日子没写博客了,主要原因一个是工作,另一个就是健身,因为我们不仅需要努力工作,也需要有健康的身体嘛. 那有看LZ博客的网友朋友们放心,LZ博客还是会继续保持更新,只是最近两三个月LZ写博客相 ...
- springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】
项目结构: 1.maven项目的pom中引入shiro所需的jar包依赖关系 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- MyBatis6:MyBatis集成Spring事务管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事务管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事务的做法,本文的目的是在这个的基 ...
随机推荐
- MongoDB系列四(索引).
一.索引简介 再来老生常谈一番,什么是索引呢?数据库索引与书籍的索引类似.有了索引就不需要翻整本书,数据库可以直接在索引中查找,在索引中找到条目以后,就可以直接跳转到目标文档的位置,这能使查找速度提高 ...
- 【VB超简单入门】四、编译工程及传播程序文件
四.编译工程及传播程序文件 为什么在这一章中讲到编译这部分内容呢?因为当我们学会怎样用VB开发程序之后,内心肯定喜悦无比.这时候就要和朋友分享一下快乐,所以要先学习编译EXE程序文件. 首先新建工程. ...
- hive:导出数据记录中null被替换为\n的解决方案
在hive中,一般情况下通过 use my_hive_db; set hive.merge.mapfiles=true; set hive.merge.mapredfiles=true; ; ; in ...
- python的切片操作
切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割.注意这与你使用的索引操作符十分相似.记住数是可选的,而冒号是必须的. 切片操作符中的第一个数(冒号之前)表示切片开始的位置,第 ...
- jacascript 立即执行函数(IIFE)与闭包
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 一直没搞清楚立即执行函数和闭包之间的关系,总结一下: 闭包有很多种理解:访问不到内部作用域,函数就是这样, ...
- JAVA_将唐诗按照古文样式输出
1. 如有唐诗: 锄禾日当午 汗滴禾下土 谁知盘中餐 粒粒皆辛苦 要求将这首唐诗按照古文样式输出,输出格式如下: 粒谁汗锄 粒知滴禾 皆盘禾日 辛中下当 苦餐土午 public class Text ...
- [原创软件]Maya语言切换工具
软件主要功能: 切换Maya语言 软件界面截图: 开发环境及语言: c# .NET Framework 4.0 Visual Studio 2015 更新日志: v1.0(2016.7.20) 发布初 ...
- OpenGL 背面剔除
在OpenGL种可使用glEnable(GL_CULL_FACE)开启背面剔除功能,即把那些我们看不见的面删除.但在剔除之前我们需要定义正面和背面,这个可以用法线来理解.在数学学科中,法线是用右手法则 ...
- java工厂设计模式初步
没有利用反射机制定义的简单工厂类 interface Fruit{ public void eat(); } class Apple implements Fruit{ public void eat ...
- [LeetCode] Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...