22. SpringBoot 集成 Mybatis
1. 引入Mybatis的maven 依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2. MybatisGenerator自动生成Mapper、dao、model
3. MapperDao上标注@Mapper注解
@Mapper
public interface UserMapper {
int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record);
}
4. 配置文件中指明mybatis的配置文件位置

UserMapper.xml配置文件方式实现时,需要制定配置文件位置
#MapperDaoInterface若是用注解的方式实现SQL的话就用不着/mapper/*.xml配置
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
configuration:
map-underscore-to-camel-case: true #开启驼峰标识 表:user_name ---> JavaBean:userName #配置com.example.jdbc.dao包下的mapper接口类的日志级别,配上此日志控制台会打印SQL
logging:
level:
com:
example:
jdbc:
dao: debug
注意:2.1.3.RELEASE版本的springboot中,mybatis.configuration 和mybatis.configLocation 配置项不可以一起使用
Caused by: java.lang.IllegalStateException: Property 'configuration' and 'configLocation' can not specified with together
at org.springframework.util.Assert.state(Assert.java:) ~[spring-core-5.1..RELEASE.jar:5.1..RELEASE]
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:) ~[mybatis-spring-2.0..jar:2.0.]
at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:) ~[mybatis-spring-2.0..jar:2.0.]
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:) ~[mybatis-spring-boot-autoconfigure-2.0..jar:2.0.]
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$99dc0108.CGLIB$sqlSessionFactory$(<generated>) ~[mybatis-spring-boot-autoconfigure-2.0..jar:2.0.]
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$99dc0108$$FastClassBySpringCGLIB$$677faa16.invoke(<generated>) ~[mybatis-spring-boot-autoconfigure-2.0..jar:2.0.]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:) ~[spring-core-5.1..RELEASE.jar:5.1..RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:) ~[spring-context-5.1..RELEASE.jar:5.1..RELEASE]
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$99dc0108.sqlSessionFactory(<generated>) ~[mybatis-spring-boot-autoconfigure-2.0..jar:2.0.]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:) ~[na:1.8.0_181]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:) ~[spring-beans-5.1..RELEASE.jar:5.1..RELEASE]
... common frames omitted
5. 不用配置文件,用注解
@Mapper
public interface UserMapper { @Delete("delete from user where id = #{id}")
int deleteByPrimaryKey(Integer id); @Insert("insert into user(name,age,sex) values(#{name},#{age},#{sex})")
int insert(User record); int insertSelective(User record); @Select("select * from user where id = #{id}")
User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); @Update("update user set name=#{name}, age=#{age}, sex=#{sex}")
int updateByPrimaryKey(User record);
}
@MapperScan注解与@Mapper注解选其一用即可
直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦。
通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,可以解决mapper类没有在Spring Boot主程序的情况
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.example.jdbc.dao")
public class SpringBootJdbcApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootJdbcApplication.class, args);
    }
}
@MapperScan 支持多个包扫描和包名模糊匹配
@MapperScan({"com.kfit.demo","com.kfit.user"})
@MapperScan({"com.kfit.*.mapper","org.kfit.*.mapper"})
22. SpringBoot 集成 Mybatis的更多相关文章
- springboot集成mybatis(二)
		上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ... 
- springboot集成mybatis(一)
		MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ... 
- SpringBoot 集成Mybatis 连接Mysql数据库
		记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ... 
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
		SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ... 
- SpringBoot集成Mybatis并具有分页功能PageHelper
		SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ... 
- Springboot集成mybatis(mysql),mail,mongodb,cassandra,scheduler,redis,kafka,shiro,websocket
		https://blog.csdn.net/a123demi/article/details/78234023 : Springboot集成mybatis(mysql),mail,mongodb,c ... 
- BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析
		重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ... 
- SpringBoot集成Mybatis配置动态数据源
		很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ... 
- SpringBoot集成Mybatis实现多表查询的两种方式(基于xml)
		下面将在用户和账户进行一对一查询的基础上进行介绍SpringBoot集成Mybatis实现多表查询的基于xml的两种方式. 首先我们先创建两个数据库表,分别是user用户表和account账户表 ... 
随机推荐
- Qt_深入了解信号槽(signal&slot)
			转自豆子空间 信号槽机制是Qt编程的基础.通过信号槽,能够使Qt各组件在不知道对方的情形下能够相互通讯.这就将类之间的关系做了最大程度的解耦. 槽函数和普通的C++成员函数没有很大的区别.它们也可以使 ... 
- msql  复杂练习
			https://blog.csdn.net/xiao__oaix/article/details/78122294 customer表branch 表account 表 depositor 表loan ... 
- poj 2762(强连通分量+拓扑排序)
			题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ... 
- 监控系统 & monitoring & DevOps
			监控系统 & monitoring & DevOps https://github.com/topics/monitoring https://github.com/marketpla ... 
- html image 圖像路徑
			src可以指定image路徑: alt可以設置替代的文本:當瀏覽器沒有辦法加載到圖片的時候,就會顯示替換的文本,提示什麼圖片未加載. width和heigt可以設置圖片的大小,從而對圖片進行縮放. h ... 
- C# 8小特性
			对于C# 8,有吸引了大多数注意力的重大特性,如默认接口方法和可空引用,也有许多小特性被考虑在内.本文将介绍几例可能加入C#未来版本的小特性. 新的赋值运算符:&&=和||= 从第一个 ... 
- Sql 重置自动增长列
			Sql 重置自动增长列: dbcc checkident(表名, reseed, 0) 使用的情况,一般出现在主外键关联表,导致无法 truncate 只能delete的情况. 此时我们可能会需要重置 ... 
- 自学Linux Shell3.5-目录处理命令mkdir rmdir
			点击返回 自学Linux命令行与Shell脚本之路 3.5-目录处理命令mkdir rmdir 1. mkdir命令 创建一个或多个新的目录. mkdir 命令创建由 Directory 参数指定的一 ... 
- 对 spi 的认知
			在使用 SPI 外设场景下,只需将数据送至 SPI->DR,外设将数据自动发走 在使用 DMA 外设场景下,只需指定数据缓存区地址及 SPI->DR 地址,这样就无需劳驾 CPU 而开始数 ... 
- Spring Cloud(三) --- hystrix
			Hystrix 说到Hystrix就得先说一下产生的背景等等,那就是雪崩效应. 在微服务中肯定存在多个服务层之间的调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服 ... 
