SpringBoot中集成mybatis和PageHelper

  • Springboot集成mybatis
1、在pom.xml中添加tomcat,mybaits,mysql依赖
<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
1、在pom.xml中添加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.csdn.net/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值