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账户表 ...
随机推荐
- Jquery Ajax xml版Get请求PHP
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- insertBefore(),appendChild()创建添加列表实例
定义: insertBefore() 方法在您指定的已有子节点之前插入新的子节点. 语法: 父级.insertBefore(新的子节点,指定的已有子节点) 实例: <input id=" ...
- hive启动方式
- SourceTree 如何下载git 管理的代码-如何创建分支,删除分支,提交代码,回退代码
把用户给的链接拿过来,然后输入浏览器,然后在左侧会有Actions 中有个Clone;点击Clone之后,有个 Clone in Source Tree 点击,打开你的本地Source Tree,然后 ...
- yield与递归的组合运用
- Flatpak 1.1.0发布:可终止运行Flatpak实例
读 Flatpak的Alex Larsson发布了流行的Linux应用程序沙盒和分发框架的新版本,该框架有望成为跨Linux操作系统的应用程序分发的未来. Flatpak 1.1.0现已作为开始推出F ...
- linux screen 命令 :离线运行程序
screen工具是linux下虚拟终端的一个常用工具.在 发现这个工具之前,笔者经常在远程ssh中运行需要长时间处理数据的命令,比如远程编译安装软件,如果在编译的过程中网络断开,那这个编译进程就会停止 ...
- 05 自学Aruba之AAA profile无法删除问题
点击返回:自学Aruba之路 05 自学Aruba之AAA profile无法删除问题 在新建完成AAA profile之后,可能存在后续不需要或者变动,那就要删除多余的AAA profile . 其 ...
- 【CF961G】Partitions(第二类斯特林数)
[CF961G]Partitions(第二类斯特林数) 题面 CodeForces 洛谷 题解 考虑每个数的贡献,显然每个数前面贡献的系数都是一样的. 枚举当前数所在的集合大小,所以前面的系数\(p\ ...
- IDEA安装和激活
IDEA安装 按照最新版本有可能会有很多BUG的原则,我们就安装IDEA 2018.1.6版本的. 首先,我们到IDEA官网去下载IDEA,官网链接,但是这个界面的版本一般为最新的. 这里我放出IDE ...