MybatisPlus (mp) 是一个 Mybatis 的增强 工具 ,在 Mybatis的基础上 只做增强不做改变,为简化开发,提高效率。
①数据库环境准备
②创建SpringBoot工程,引入MyBatis-Plus起步依赖
③编写DataSource相关配置
④编写mapper
⑤测试
- -- 创建数据库
- CREATE DATABASE IF NOT EXISTS mp DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
- USE mp;
-
- -- 创建测试表
- CREATE TABLE `tb_user` (
- `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
- `user_name` varchar(20) NOT NULL COMMENT '用户名',
- `password` varchar(20) NOT NULL COMMENT '密码',
- `name` varchar(30) DEFAULT NULL COMMENT '姓名',
- `age` int(11) DEFAULT NULL COMMENT '年龄',
- `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
- -- 插入测试数据
- INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('1', 'zhangsan', '123456', '张三', '18', 'test1@itcast.cn');
- INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('2', 'lisi', '123456', '李四', '20', 'test2@itcast.cn');
- INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('3', 'wangwu', '123456', '王五', '28', 'test3@itcast.cn');
- INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('4', 'zhaoliu', '123456', '赵六', '21', 'test4@itcast.cn');
- INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('5', 'sunqi', '123456', '孙七', '24', 'test5@itcast.cn');
-
- CREATE TABLE `tb_course` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(50) DEFAULT NULL,
- `icon` varchar(100) DEFAULT NULL,
- `create_date` datetime DEFAULT NULL,
- `is_show` int(11) DEFAULT NULL COMMENT '是否显示\r\n 0 显示\r\n 1 不显示',
- `user_id` bigint(20) DEFAULT NULL,
- `order_no` int(11) DEFAULT NULL COMMENT '排序编号',
- PRIMARY KEY (`id`),
- FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COMMENT='学科表\r\n';
-
- -- ----------------------------
- -- Records of t_course
- -- ----------------------------
- INSERT INTO `tb_course` VALUES ('1', 'Java', null, '2019-08-08 00:00:00', '0', '1', '1');
- INSERT INTO `tb_course` VALUES ('2', 'Python', null, '2019-08-08 00:00:00', '0', '1', '1');
- INSERT INTO `tb_course` VALUES ('3', '大数据', null, '2019-08-08 00:00:00', '0', '2', '1');
- INSERT INTO `tb_course` VALUES ('4', 'Php', null, '2019-08-08 00:00:00', '0', '2', '1');
- INSERT INTO `tb_course` VALUES ('5', '前端', null, '2019-08-08 00:00:00', '0', '3', '1');
-
- CREATE TABLE `tb_catalog` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(50) DEFAULT NULL,
- `create_date` datetime DEFAULT NULL,
- `status` int(11) DEFAULT NULL COMMENT '状态\r\n 0 启用\r\n 1 禁用',
- `user_id` bigint(20) DEFAULT NULL,
- `course_id` int(11) DEFAULT NULL,
- `order_no` int(11) DEFAULT NULL,
- PRIMARY KEY (`id`),
- FOREIGN KEY (`course_id`) REFERENCES `tb_course` (`id`),
- FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COMMENT='学科目录';
-
- -- ----------------------------
- -- Records of t_catalog
- -- ----------------------------
- INSERT INTO `tb_catalog` VALUES ('1', 'Java基础', '2019-08-08 00:00:00', '0', '1', '1', '1');
- INSERT INTO `tb_catalog` VALUES ('2', 'JavaWeb', '2019-08-08 00:00:00', '0', '1', '1', '1');
- INSERT INTO `tb_catalog` VALUES ('3', 'Spring MVC', '2019-08-08 00:00:00', '0', '1', '1', '1');
- INSERT INTO `tb_catalog` VALUES ('4', 'Spring boot', '2019-08-08 00:00:00', '0', '1', '1', '1');
- INSERT INTO `tb_catalog` VALUES ('5', 'Python基础', '2019-08-08 00:00:00', '0', '1', '2', '1');
- INSERT INTO `tb_catalog` VALUES ('6', '函数编程', '2019-08-08 00:00:00', '0', '1', '2', '1');
- INSERT INTO `tb_catalog` VALUES ('7', '面向对象编程', '2019-08-08 00:00:00', '0', '1', '2', '1');
2.创建springBoot 项目 勾选依赖
3.编写 DataSource 信息
- # datasource
- spring:
- datasource:
- url: jdbc:mysql:///mp?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
- username: root
- password: root
- driver-class-name: com.mysql.cj.jdbc.Driver
- mybatis-plus: # 控制台打印执行日志
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.编写pojo
- @TableName("tb_user") // 指定表名
- @Data
- public class User {
- // 指定主键字段,type用来指定id生成策略
- @TableId(type = IdType.AUTO)
- private Long id;
- // 指定实体类中与表字段不一致的情况
- // 完全一致或下划线转驼峰是不需要指定的
- @TableField("user_name") // 指定字段
- private String userName;
- private String password;
- private String name;
- private Integer age;
- private String email;
-
- @TableField(exist = false) // 忽略字段
- private String info;
- }
@TableName 指定表名
@Data lombok 技术
5.编写 mapper
- /**
- * 使用mp定义Mapper,需要让Mapper接口继承 BaseMapper接口。
- */
-
- public interface UserMapper extends BaseMapper
{ - }
6.启动类
- @MapperScan("com.itheima.mp.mapper")
- @SpringBootApplication
- public class MybatisPlusSpringbootApplication {
- public static void main(String[] args) {
- SpringApplication.run(MybatisPlusSpringbootApplication.class, args);
- }
- }
7.测试
- @SpringBootTest
- public class TestUserMapper {
- @Autowired
- private UserMapper userMapper;
- @Test
- public void testSelect(){
- User user = userMapper.selectById(1);
- System.out.println(user);
- Assert.notNull(user,"ID-1,查无此人");
- }
- }
1) 表及字段
@TableName("tb_user"); 指定表名
@TableField("user_name") 指定字段映射关系
实体类的属性名和数据库的字段名自动映射:
* 名称一样
* 数据库字段使用_分割,实体类属性名使用驼峰名称
否则需要使用 @TableField("user_name") 指定映射关系
2) 忽略某个字段的查询和 插入
@TableField(exist = false)
3) 设置id生成策略: AUTO 数据库自增
@TableId(type = IdType.AUTO)mybatis-plus如果不设置ID生成策略。
默认生成策略是雪花算法,会生成一串不重复的19位的数字
mybatis-plus:
global-config:
db-config:
# 表名前缀,一般用于@TableName省略的场景,表名=表名前缀+POJO类名
table-prefix: tb_
# id生成策略 数据库自增
id-type: auto
配置拦截器 在启动类 中添加如下代码,表名 初始化 mybatisplus分页拦截器
- /**
- * 3.4.0之后提供的拦截器的配置方式
- * @return
- */
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor(){
- MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
- mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
- return mybatisPlusInterceptor;
- }
-
-
- /**
- * 3.4.0之前的版本用这个
- * @return
- */
- /* @Bean
- public PaginationInterceptor paginationInterceptor(){
- return new PaginationInterceptor();
- }*/
- /**
- * 分页查询:
- * 1. 当前页码:currentPage
- * 2. 每页显示条数:size
- *
- * 注意:使用mp的分页要设置一个拦截器!!!
- */
- @Test
- public void testSelectPage() {
- int current = 1;//当前页码
- int size = 2;//每页显示条数
- IPage
page = new Page(current,size); - userMapper.selectPage(page,null);
-
- List
records = page.getRecords();//当前页的数据 - long pages = page.getPages();//总页数 2
- long total = page.getTotal();//总记录数 4
- System.out.println(records);
- System.out.println(pages);
- System.out.println(total);
- }