第一步:导入依赖
除了核心配置mybatis-plus-boot-starter,其它为启动测试要用的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
第二步:配置yml
同上,这里先配置项目启动测试的项,关于mybatis-plus先不做配置看能不能使用,后面用到时再做配置
server:
port: 8088
spring:
application:
name: your-service-name
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/${MYSQL_DB:your database}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: ${MYSQL-USER:root}
password: ${MYSQL-PWD:root}
第三步:配置启动类
@MapperScan(basePackages = "com.nico.mybatisplus.mapper")
@SpringBootApplication
public class MyMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MyMybatisPlusApplication.class,args);
}
}
当然这里可以不配置@MapperScan直接在下面列出的CastMenuConfigMapper 接口上配置@Mapper也是可以的,注意这里要继承BaseMapper
@Mapper
public interface CastMenuConfigMapper extends BaseMapper<CastMenuConfig> {
CastMenuConfig findById(String id);
}
第四步:准备测试
前提保证数据库连接正常,数据表已经提前创建,这里我贴出实体类
public class CastMenuConfig implements Serializable {
@TableId(type = IdType.ID_WORKER)
private Long id; //主键
private String menuName; //菜单名称
private int menuType; //菜单类型
private int menuSort; //菜单顺序
private String menuContent; //菜单页的内容
//get set此处省略。。。
@Override
public String toString() {
return "CastMenuConfig{" +
"id=" + id +
", liveBasicId=" + liveBasicId +
", menuName='" + menuName + '\'' +
", menuType=" + menuType +
", menuSort=" + menuSort +
", menuContent='" + menuContent + '\'' +
'}';
}
}
这个实体类看起来很干净,除了id上的注解,没有太多冗余的注解,再看一下数据库

同样看一下项目结构

测试类生成


看一下测试类的测试方法,先定义两个,selectById是BaseMapper提供的接口,可以直接调用,同样selectOne也一样,只不过定义的入参是QueryWrapper这个给我们提供的条件构造器,顾名思义就是帮我们构造条件的,把所有条件参数扔进这个类就行了

第五步:进入测试
分别点击测试selectById、selectOne

查看输出相同的结果

好的,现在使用mybatis-plus提供的接口增删改查是没问题,当然它也兼容xml的方式以及注解的方式编写sql,我们试试看,现在可以配置一下mybatis-plus的yml配置
mybatis-plus:
#使用xml方式编写复杂sql时 必须配置不然无法启动
mapper-locations: classpath:mapper/*.xml
#使用xml方式编写复杂sql时 必须配置不然找不到resultType指定的返回的实体类型 或者在xml文件里写实体类全路径也可以 不然无法启动
type-aliases-package: com.nico.mybatisplus.entity
看看xml文件内容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nico.mybatisplus.mapper.CastMenuConfigMapper">
<select id="findById" parameterType="String" resultType="CastMenuConfig">
select * from cast_menu_config where id=#{id}
</select>
<select id="findAll" parameterType="String" resultType="CastMenuConfig">
select * from cast_menu_config
</select>
</mapper>
我们开始测试

查看结果测试通过没有问题

这里对比通用tk-mapper的使用方式,mybatis-plus不需要通过注解来映射实体与数据库字段就可以自动匹配,如果匹配不上就使用默认值null或0,也不需要在yml配置map-underscore-to-camel-case: true驼峰匹配规则
第六步:分页
使用分页助手PageHelper
@Test
void findPage() {
PageHelper.startPage(1,1);
List<CastMenuConfig> castMenuConfigList = castMenuConfigMapper.findAll();
PageInfo<CastMenuConfig> castMenuConfigPageInfo = new PageInfo<>(castMenuConfigList);
castMenuConfigPageInfo.getList().forEach(item->System.out.println("查询结果:"+item));
}
查看结果没问题

使用mybatis-plus自带的分页接口IPage
@Test
void findPagePlus() {
IPage castMenuConfigList = castMenuConfigMapper.selectPage(new Page(1, 1),new QueryWrapper<>());
castMenuConfigList.getRecords().forEach(item->System.out.println("查询结果:"+item));
}
查看结果发现没有进行分页

原因是我漏了一个mybatis-plus官方提供的配置类,参考官方:https://baomidou.com/guide/page.html
//Spring boot方式
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
以xml的方式写sql的分页
@Test
void findPageByXml() {
IPage<CastMenuConfig> castMenuConfigList = castMenuConfigMapper.selectPageVo(new Page(1, 1));
System.out.println("总页数:"+castMenuConfigList.getPages());
System.out.println("总记录数:"+castMenuConfigList.getTotal());
castMenuConfigList.getRecords().forEach(item->System.out.println("查询结果:"+item));
}
mapper接口是这样的

xml里的sql是这样的

测试,查看结果同样是没问题

第七步:打印sql日志
application.yml是这样配置
logging:
level:
com:
nico:
mybatisplus:
mapper: debug
bootstrap.yml是这样配置
logging:
#打印sql日志
level:
com.nico.mybatisplus.mapper: debug
还有这种
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
其他注解使用参考上面给出的官方文档,或者其他博客https://www.jianshu.com/p/1a163148b078
网友评论