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账户表 ...
随机推荐
- SLAM中的变换(旋转与位移)表示方法
1.旋转矩阵 注:旋转矩阵标题下涉及到的SLAM均不包含位移. 根据同一点P在不同坐标系下e(e1,e2,e3)e'(e1',e2',e3')的坐标a(a1,a2,a3)a'(a1',a2',a3') ...
- 使用 SSH 秘钥远程连接
团队开发中常用到 Git.SVN 等版本控制工具,可以大大提高开发效率. 就是将代码统一放到一个代码仓库中,方便管理. 为了安全起见,每次push.pull 代码的时候,都需要输入用户名.密码, 对于 ...
- poj 3694 Network(割边+lca)
题目链接:http://poj.org/problem?id=3694 题意:一个无向图中本来有若干条桥,有Q个操作,每次加一条边(u,v),每次操作后输出桥的数目. 分析:通常的做法是:先求出该无向 ...
- JavaScript——DOM树的增查改删总结
对HTML DOM的操作是前端JavaScript编程时必备的技能,本文是我自己对DOM树操作的总结,主要是方法的罗列,原理性的讲述较少,适合大家用于理清思路或是温习 一.什么是HTML DOM? 是 ...
- BZOJ5093 图的价值(NTT+斯特林数)
显然每个点会提供相同的贡献.于是现在只考虑1号点的贡献.若其度数为i,则在2~n号点选i个连上,剩下的边随便连,这样可以算出答案为 这个式子可以O(n)计算.发现k比较小,于是考虑如何将这个式子化为与 ...
- json_encode转化为数组时带下标
//错误 data[$i]=[date("y-m-d H:i:s"),数据查询] //正确 data[]=[date("y-m-d H:i:s"),数据查询] ...
- POI获取单元格的宽和高
获取单元格的宽,即获取所在列的宽.先获取单元格所在的sheet:cell.getSheet() sheet.getColumnWidth( cell.getColumnIndex() ) 单位不是像 ...
- debian 系统安装配置apache
安装sshapt-get install ssh-server (安装失败请插入镜像)service ssh start Apache 服务安装apt-get install apache2 apa ...
- 自学Linux Shell12.4-for命令
点击返回 自学Linux命令行与Shell脚本之路 12.4-for命令 1. for命令 格式一 for var in list do commands done 格式二 for var in li ...
- ZABBIX 3.4 监控服务器TCP连接状态(六)
TCP的连接状态对于我们web服务器来说是至关重要的,尤其是并发量ESTAB:或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了攻击,或是是time_wait值比较高的话,我们要考 ...