- Springboot集成mybatis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
2、在启动类上添加@MapperScan,用于配置扫描mybatis的接口类扫描包
3、代码编写
4、在application.yml中填写数据库配置(必填)和mybatis配置(可选)
问题:
1、Unregistering JMX-exposed beans on shutdown
原因:可能是org\apache\tomcat\embed\tomcat-embed-core下的包下载不完整
解决方案:先删除后,在update maven
----------------------------------------------------
- mybatis中加入分页PageHelper
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
2、配置pagehelper属性
方法(1)、在application.yml中配置pagehelper
mybatis:
#... 其他配置信息
configuration-properties:
offsetAsPageNum: true
rowBoundsWithCount: true
reasonable: true
方法(2)、新建一个类,使用@Configuration注解表示该类为一个配置类
@Configuration
public class MybatisConfig {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
pageHelper.setProperties(properties);
return pageHelper;
}
}
3、在需要分页的方法中使用PageHelper.startPage(int pageNum,int pageSize);
例如:
PageHelper.startPage(2, 2);
--------------------------------------------------
- mybatis获取主键
在@Insert的方法上添加@Options
@Options(keyColumn="id", keyProperty="id", useGeneratedKeys=true)
====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.youkuaiyun.com/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | ![]() |
![]() |