SpringBoot数据访问之整合Mybatis配置文件
环境搭建以及前置知识回顾
SpringBoot中有两种start的形式:
- 官方:spring-boot-starter-*
- 第三方:*-spring-boot-starter
Mybatis属于第三方,所以我们需要找他的官网、配置文档等。
贴心链接:Github
进入后记得切换tags

作者使用的版本是最新的2.2.0;
找到它下面的pom.xml,可以得到:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
在网页下方,找到快速开始文档:

回顾前面mybatis的使用过程:
- 导入mybatis依赖和数据库驱动
- 连接数据库
- 增加全局配置文件,一般是(mybatis-config.xml)
- 编写工具类:SqlSessionFactory
- 使用sqlSession
- 编写Mapper
- .......
Druid的配置:Druid
分析Mybatis启动器自动配置
找到MybatisAutoConfiguration,
@ConditionalOnSingleCandidate(DataSource.class)
只允许一个数据源在容器中。
@EnableConfigurationProperties(MybatisProperties.class)
@EnableConfigurationProperties(类.class)
- 开启类配置绑定功能
- 把这个类这个组件自动注册到容器中
我们进入MybatisProperties.class中查看:

由上图可知,我们只需要在配置文件中使用mybatis为前置,就可以使用了。
使用配置搞清楚后,我们来看下启动器给我们自动装配了什么功能:
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource)
对应的是之前的使用过程第4步,现在自动配置好了
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory)
对应之前的使用过程第5步,可以进入**SqlSessionTemplate **看下,SqlSessionTemplate组合了SqlSession。
@Import(AutoConfiguredMapperScannerRegistrar.class),只要我们写的操作MyBatis的接口,标注了 @Mapper 就会被自动扫描进来,对应之前的使用过程第6步。
整合Mybatis
目录结构:

首先创建一个全局配置文件(mybatis-config.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>
</configuration>
Configuration中包含:包括获取数据库连接实例的数据源(DataSource)以及决定事务作用域和控制方式的事务管理器(TransactionManager)。因为我们的数据源已经通过Druid配置好了,所以这里直接清空就好。
实体类
创建实体类,尽可能与数据库字段名保持一致。
import lombok.Data;
@Data
public class User {
private int id;
private String username;
private String password;
}
Mapper层(对照Dao)
创建GetUserMapper接口;
import com.xbhog.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface GetUserMapper {
public User getUserid(int id);
}
实现接口:
创建getUserMapper.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.xbhog.Mapper.GetUserMapper">
<select id="getUserid" resultType="com.xbhog.pojo.User">
select * from user where id = #{id}
</select>
</mapper>
其中id对应的是GetUserMapper接口中抽象方法getUserid,结果集类型对应的返回值类型User。
其实我们现在可以创建controller来实现访问了,为了方便后序功能的扩展,我们可以创建一个service层,有一句话说的好,出现问题,没有加一层解决不了的,有的话那就再加一层。
Service层:DemoService
import com.xbhog.Mapper.GetUserMapper;
import com.xbhog.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
@Autowired
GetUserMapper getUserMapper;
public User getUser(int id){
return getUserMapper.getUserid(id);
}
}
从容器中取出GetUserMapper,在service层进行调用。如果只是测试,可以直接省略创建Controller.
创建Controller:
@Controller
public class Mycontro {
@Autowired
DemoService demoService;
@ResponseBody
@GetMapping("/user")
public User getById(@RequestParam("id") int id){
return demoService.getUser(id);
}
}
上述步骤完成后需要在mybatis中注册,在Springboot中需要在配置文件中:
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
mapper-locations: classpath:mybatis/Mapper/*.xml #sql映射文件位置
完整配置:
spring:
datasource:
url: jdbc:mysql://localhost:3306/vuesite
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
druid:
aop-patterns: com.xbhog.*
filters: stat,wall
stat-view-servlet:
enabled: true
login-password: admin
login-username: admin
reset-enable: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
filter:
stat:
slow-sql-millis: 1000
log-slow-sql: true
enabled: true
wall:
enabled: true
config:
drop-table-allow: false
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/Mapper/*.xml
测试:
输入:localhost:8080/user?id=2 查询id为2的用户
结果:
{"id":2,"username":"demo","password":"123456"}
如果之前也配置了Druid,可以查看http://localhost:8080/druid/

注意点
驼峰命名:
如果数据库中的字段是驼峰命名(Camel-Case),那么我们需要在mybatis中开启驼峰命名,不开启的话,是无法查找到对应的数据。
在全局配置文件中:
<?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="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
全局配置设置:
其实在springBoot中的mybatis-start中已经有全局配置属性了。
找到MybatisProperties配置属性类。

进入Configuration,发现我们需要的配置已经在里面设置好了,以后只需要在配置文件中设置属性即可:

所以我们可以删除全局配置文件。在yaml或者properties中配置:
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/Mapper/*.xml
configuration: #全局配置文件
map-underscore-to-camel-case: true
总结mybatis整合过程:
导入mybatis官方starter
编写mapper接口。标准@Mapper注解
编写sql映射文件并绑定mapper接口
在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息 (建议;配置在mybatis.configuration)
结束:
如果你看到这里或者正好对你有所帮助,希望能点个关注或者推荐,感谢;
有错误的地方,欢迎在评论指出,作者看到会进行修改。
SpringBoot数据访问之整合Mybatis配置文件的更多相关文章
- SpringBoot数据访问之整合mybatis注解版
SpringBoot数据访问之整合mybatis注解版 mybatis注解版: 贴心链接:Github 在网页下方,找到快速开始文档 上述链接方便读者查找. 通过快速开始文档,搭建环境: 创建数据库: ...
- java框架之SpringBoot(9)-数据访问及整合MyBatis
简介 对于数据访问层,无论是 SQL 还是 NOSQL,SpringBoot 默认采用整合 SpringData 的方式进行统一处理,添加了大量的自动配置,引入了各种 Template.Reposit ...
- Spring Boot数据访问之整合Mybatis
在Mybatis整合Spring - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中谈到了Spring和Mybatis整合需要整合的点在哪些方面,需要将Mybatis中数据库连接池等相关对 ...
- 【串线篇】SpringBoot数据访问【数据源/mybatis/指定映射文件位置】
一.配置数据源 1.1.jdbc版本 JDBC(.tomcat.jdbc.pool.DataSource作为数据源) <?xml version="1.0" encoding ...
- SpringBoot数据访问之Druid启动器的使用
数据访问之Druid启动器的使用 承接上文:SpringBoot数据访问之Druid数据源的自定义使用 官方文档: Druid Spring Boot Starter 首先在在 Spring Boot ...
- Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...
- SpringBoot数据访问(一) SpringBoot整合Mybatis
前言 SpringData是Spring提供的一个用于简化数据库访问.支持云服务的开源框架.它是一个伞形项目,包含了大量关系型数据库及非关系型数据库的数据访问解决方案,其设计目的是为了使我们可以快速且 ...
- SpringBoot数据访问(二) SpringBoot整合JPA
JPA简介 Spring Data JPA是Spring Data大家族的一部分,它可以轻松实现基于JPA的存储库.该模块用于增强支持基于JPA的数据访问层,它使我们可以更加容易地构建使用数据访问技术 ...
- Springboot数据访问,棒棒哒!
Springboot对数据访问部分提供了非常强大的集成,支持mysql,oracle等传统数据库的同时,也支持Redis,MongoDB等非关系型数据库,极大的简化了DAO的代码,尤其是Spring ...
随机推荐
- Windows批处理文件编写宝典
原贴:批处理新手入门导读 现在的教程五花八门,又多又杂.如何阅读,从哪里阅读,这些问题对新手来说,都比较茫然. 这篇文章的目的就是帮助新手理清学习顺序,快速入门.进步 1.如果你从来没有接触甚至没有听 ...
- 诸多改进!Superset 1.2.0 正式发布!
Apache Superset 是一个现代的.企业级的轻量BI平台,提供了大量数据可视化组件. 距离superset上一个版本发布已经过了近三个月的时间,我们终于等到了1.2.0版本. 之前就曾提到过 ...
- JAVA 类修饰符
JAVA类的修饰符主要有public,default,protected,private,final,abstract,static 其中外部类中用到的只有public,final,abstract或 ...
- Centos-Springboot项目jar包自启动
CentOS环境下部署Springboot项目的jar包开机自启动. 部署环境 Centos 7.5 Springboot 2.1.x 操作步骤 修改pom 在pom.xml文件中<plugin ...
- python 正则表达式 初级
举例: 1.匹配hello world key = r"<h1>hello world<h1>" #源文本 p1 = r"<h1>.+ ...
- mysql中,一个数字加上null,结果为null
在mysql中,一个数字加上null,结果为null. 这个问题是我用update语句时遇见的,就像下边的例子 update tableName set number = number + x 这里的 ...
- 315M、433M和2.4G笔记
一.315M无线模块 315m无线模块广泛地运用在车辆监控.遥控.遥测.小型无线网络.无线抄表.门禁系统.小区传呼.工业数据采集系统.无线标签.身份识别.非接触RF智能卡.小型无线数据终端.安全防火系 ...
- python 10篇 操作mysql
一.操作数据库 使用pip install pymysql,安装pymysql模块,使用此模块连接MySQL数据库并操作数据库. import pymysql host = 'ip地址' # 链接的主 ...
- 前端-Vue基础1
Vue核心思想:只要改变数据,页面就会发生改变 1.引入vue 1.下载vue.js 2.在script标签的src属性中,引入vue.js <script src="js/vue.j ...
- Ubuntu20.4 bs4安装的正确姿势
一.背景 公司一小伙子反馈在内网机器上通过代理,还是安装不了bs4:于是乎,作为菜鸡的我开始排查.一直认为是网络和代理问题,所以关注点一直放在网络和安装包上:在网上搜索到,主要是以下问题: 1)更新a ...