Mybatis与MybatisPlus整合
Mapper继承BaseMapper,就拥有了BaseMapper中的所有方法
相关配置
SpringBoot配置文件参考
spring.application.name=itcast-mp-springboot
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123
# 指定全局配置文件
mybatis-plus.config-location=classpath:mybatis-config.xml
# 指定mapper.xml文件的路径
mybatis-plus.mapper-locations= classpath*:mybatis/*.xml
# 实体对象扫描包
mybatis-plus.type-aliases-package= cn.itcast.mp.pojo
# 关闭自动驼峰映射,该参数不能和mybatis-plus.config-location同时存在 可以配置到mybatis-config.xml里边
#mybatis-plus.configuration.map-underscore-to-camel-case=false
# 是否禁用缓存
mybatis-plus.configuration.cache-enabled=false
# 全局id策略
mybatis-plus.global-config.db-config.id-type=auto
# 全局的表明的前缀
mybatis-plus.global-config.db-config.table-prefix=tb_
SpringBoot配置类配置分页器
@Configuration
@MapperScan("cn.itcast.mp.mapper") //设置mapper接口的扫描包
public class MybatisPlusConfig {
@Bean //配置分页插件
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
在Mybatis-config.xml文件中配置分页器
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></plugin>
</plugins>
</configuration>
POJO类相关注释
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String userName;
@TableField(select = false) //查询时不反回该字段的值
private String password;
private String name;
private Integer age;
@TableField(value = "email") //指定数据库字段名映射
private String mail;
@TableField(exist = false)
private String address; //在数据库表中不存在
}