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的更多相关文章

  1. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  2. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  3. SpringBoot 集成Mybatis 连接Mysql数据库

    记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...

  4. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  5. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

  6. 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 ...

  7. BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析

    重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...

  8. SpringBoot集成Mybatis配置动态数据源

    很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...

  9. SpringBoot集成Mybatis实现多表查询的两种方式(基于xml)

     下面将在用户和账户进行一对一查询的基础上进行介绍SpringBoot集成Mybatis实现多表查询的基于xml的两种方式.   首先我们先创建两个数据库表,分别是user用户表和account账户表 ...

随机推荐

  1. Pyhton语句

    一.if条件语句 1.python 并不支持 switch 语句 num = 5 if num == 3: # 判断num的值 print 'boss' elif num == 2: print 'u ...

  2. mxnet,theano与torch的简单比较

    这篇文章我想来比较一下Theano和mxnet,Torch(Torch基本没用过,所以只能说一些直观的感觉).我主要从以下几个方面来计较它们: 1.学习框架的成本,接口设计等易用性方面. 三个框架的学 ...

  3. shell 学习笔记二

    一.break命令 break命令允许跳出所有循环(终止执行后面的所有循环). 下面的例子中,脚本进入死循环直至用户输入数字大于5.要跳出这个循环,返回到shell提示符下,就要使用break命令. ...

  4. codeforces9A

    Die Roll CodeForces - 9A Yakko,Wakko和Dot,世界著名的狂欢三宝,哈哈,不知道你是否看过这个动画片. 某一天,过年了,他们决定暂定卡通表演,并去某些地方旅游一下.Y ...

  5. ASP.NET MVC5使用Area区域

    转载:http://www.lanhusoft.com/Article/217.html 在大型的ASP.NET mvc5项目中一般都有许多个功能模块,这些功能模块可以用Area(中文翻译为区域)把它 ...

  6. MT【207】|ax^2+bx+c|中判别式$\Delta$的含义

    已知$a,b\in R^+,a+b=2$且对任意的$x\in R$,均有$|2x^2+ax-b|\ge|x^2+cx+d|$则$\dfrac{d-4c}{cd}$的最小值______ 提示:注意到$\ ...

  7. MT【21】任意基底下的距离公式

    解析: 评:$\theta=90^0$时就是正交基底下(即直角坐标系下)的距离公式.

  8. 关于Hive中常用函数需要注意的点小合集

    1.COALESCE( value1,value2,... ) The COALESCE function returns the fist not NULL value from the list ...

  9. 17 利用Zabbix完成VMare监控

    点击返回:自学Zabbix之路 17 利用Zabbix完成VMare监控 最近在研究通过Zabbix监控VMware vSphere,Zabbix Documentation 3.0 从文档中我们看到 ...

  10. 自学Linux Shell5.2-shell内建命令history alias

    点击返回 自学Linux命令行与Shell脚本之路 5.2-shell内建命令history alias 外部命令:有时称为文件系统命令,是存在于bash shell之外的程序,通常位于/bin./u ...