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 ...
随机推荐
- SpringBoot项目创建流程--SpringMVC
SpringBoot项目创建步骤 1. 创建SpringBoot工程 (1) File → New → Project → Spring Initilizr (2) Name:MySpringBoot ...
- Golang中的各种时间操作
Golang中的各种时间操作 需求 时间格式的转换比较麻烦,自己写了个工具,可以通过工具中的这些方法相互调用转成自己想要的格式,代码如下,后续有新的函数再添加 实现代码 package utils i ...
- C#调用JAVA(二)调用方法
上期我们创建了jar包并放到了unity中,那么我们继续 如果您还没有看上一期请先看上一期,这是链接 C#调用JAVA(一)制作jar包 - 执著GodShadow - 博客园 (cnblogs.co ...
- Pytest学习笔记10-生成html报告
前言 在pytest中,如何生成html测试报告呢,pytest提供了pytest-html插件,可以帮助我们生成测试报告,当然,如果希望生成更加精美的测试报告,我们还可以使用allure生成报告,下 ...
- 基于HSI和局部同态滤波的彩色图像增强
简介 在图像采集过程中,由于光照环境或物体表面反光等原因会造成图像光照不均 .图像的光照不均会直接影响图像分析的结果.因此,对光照不均图像进行增强,消除光照的影响是光照不均图像处理中不可缺少的环节 . ...
- Jenkins CI&CD 自动化发布项目实战(上篇)
Jenkins CI&CD 自动化发布项目实战(上篇) 作者 刘畅 时间 2020-11-28 实验环境 centos7.5 主机名 ip 服务配置 软件 gitlab 172.16.1.71 ...
- .NET Core如何全局获取用户信息?
前言 在增删改查中的增和改操作中,我们经常需要更新数据流的创建人和修改人,无论我们项目是基于DDD,抑或是简单仅有服务层,此时我们都需要获取用户信息,那么我们只能将用户标识从控制器层层传递到服务或仓储 ...
- acwing 4 多重背包问题 I
多重背包 有 n种物品 一共有 m大小的背包,每种物品的价值 大小 个数 为 s[i],v[i],num[i]; #include<bits/stdc++.h>//cmhao #defin ...
- redis集群环境配置
为什么需要集群 redis是一个开源的 key->value 高速存储系统,但是由于redis单线程运行,在系统中,只能利用单核的性能 当redis的调用越来越频繁时,可能会出现redis过于繁 ...
- PHP单例模式 (转)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...