• MybatisPlus 快速开发


    1.概述:

    MybatisPlus  (mp) 是一个 Mybatis 的增强 工具 ,在 Mybatis的基础上 只做增强不做改变,为简化开发,提高效率。

    Mybatis-Plus详解_有头发还能学的博客-CSDN博客_mybatis-plus

     2.快速开发:

     ①数据库环境准备
    ②创建SpringBoot工程,引入MyBatis-Plus起步依赖
    ③编写DataSource相关配置
    ④编写mapper
    ⑤测试

     

    1. -- 创建数据库
    2. CREATE DATABASE IF NOT EXISTS mp DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
    3. USE mp;
    4. -- 创建测试表
    5. CREATE TABLE `tb_user` (
    6. `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    7. `user_name` varchar(20) NOT NULL COMMENT '用户名',
    8. `password` varchar(20) NOT NULL COMMENT '密码',
    9. `name` varchar(30) DEFAULT NULL COMMENT '姓名',
    10. `age` int(11) DEFAULT NULL COMMENT '年龄',
    11. `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
    12. PRIMARY KEY (`id`)
    13. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    14. -- 插入测试数据
    15. INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('1', 'zhangsan', '123456', '张三', '18', 'test1@itcast.cn');
    16. INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('2', 'lisi', '123456', '李四', '20', 'test2@itcast.cn');
    17. INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('3', 'wangwu', '123456', '王五', '28', 'test3@itcast.cn');
    18. INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('4', 'zhaoliu', '123456', '赵六', '21', 'test4@itcast.cn');
    19. INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES ('5', 'sunqi', '123456', '孙七', '24', 'test5@itcast.cn');
    20. CREATE TABLE `tb_course` (
    21. `id` int(11) NOT NULL AUTO_INCREMENT,
    22. `name` varchar(50) DEFAULT NULL,
    23. `icon` varchar(100) DEFAULT NULL,
    24. `create_date` datetime DEFAULT NULL,
    25. `is_show` int(11) DEFAULT NULL COMMENT '是否显示\r\n 0 显示\r\n 1 不显示',
    26. `user_id` bigint(20) DEFAULT NULL,
    27. `order_no` int(11) DEFAULT NULL COMMENT '排序编号',
    28. PRIMARY KEY (`id`),
    29. FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`id`)
    30. ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COMMENT='学科表\r\n';
    31. -- ----------------------------
    32. -- Records of t_course
    33. -- ----------------------------
    34. INSERT INTO `tb_course` VALUES ('1', 'Java', null, '2019-08-08 00:00:00', '0', '1', '1');
    35. INSERT INTO `tb_course` VALUES ('2', 'Python', null, '2019-08-08 00:00:00', '0', '1', '1');
    36. INSERT INTO `tb_course` VALUES ('3', '大数据', null, '2019-08-08 00:00:00', '0', '2', '1');
    37. INSERT INTO `tb_course` VALUES ('4', 'Php', null, '2019-08-08 00:00:00', '0', '2', '1');
    38. INSERT INTO `tb_course` VALUES ('5', '前端', null, '2019-08-08 00:00:00', '0', '3', '1');
    39. CREATE TABLE `tb_catalog` (
    40. `id` int(11) NOT NULL AUTO_INCREMENT,
    41. `name` varchar(50) DEFAULT NULL,
    42. `create_date` datetime DEFAULT NULL,
    43. `status` int(11) DEFAULT NULL COMMENT '状态\r\n 0 启用\r\n 1 禁用',
    44. `user_id` bigint(20) DEFAULT NULL,
    45. `course_id` int(11) DEFAULT NULL,
    46. `order_no` int(11) DEFAULT NULL,
    47. PRIMARY KEY (`id`),
    48. FOREIGN KEY (`course_id`) REFERENCES `tb_course` (`id`),
    49. FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`id`)
    50. ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COMMENT='学科目录';
    51. -- ----------------------------
    52. -- Records of t_catalog
    53. -- ----------------------------
    54. INSERT INTO `tb_catalog` VALUES ('1', 'Java基础', '2019-08-08 00:00:00', '0', '1', '1', '1');
    55. INSERT INTO `tb_catalog` VALUES ('2', 'JavaWeb', '2019-08-08 00:00:00', '0', '1', '1', '1');
    56. INSERT INTO `tb_catalog` VALUES ('3', 'Spring MVC', '2019-08-08 00:00:00', '0', '1', '1', '1');
    57. INSERT INTO `tb_catalog` VALUES ('4', 'Spring boot', '2019-08-08 00:00:00', '0', '1', '1', '1');
    58. INSERT INTO `tb_catalog` VALUES ('5', 'Python基础', '2019-08-08 00:00:00', '0', '1', '2', '1');
    59. INSERT INTO `tb_catalog` VALUES ('6', '函数编程', '2019-08-08 00:00:00', '0', '1', '2', '1');
    60. INSERT INTO `tb_catalog` VALUES ('7', '面向对象编程', '2019-08-08 00:00:00', '0', '1', '2', '1');

     2.创建springBoot 项目 勾选依赖

     3.编写 DataSource 信息

    1. # datasource
    2. spring:
    3. datasource:
    4. url: jdbc:mysql:///mp?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    5. username: root
    6. password: root
    7. driver-class-name: com.mysql.cj.jdbc.Driver
    8. mybatis-plus: # 控制台打印执行日志
    9. configuration:
    10. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

     4.编写pojo

    1. @TableName("tb_user") // 指定表名
    2. @Data
    3. public class User {
    4. // 指定主键字段,type用来指定id生成策略
    5. @TableId(type = IdType.AUTO)
    6. private Long id;
    7. // 指定实体类中与表字段不一致的情况
    8. // 完全一致或下划线转驼峰是不需要指定的
    9. @TableField("user_name") // 指定字段
    10. private String userName;
    11. private String password;
    12. private String name;
    13. private Integer age;
    14. private String email;
    15. @TableField(exist = false) // 忽略字段
    16. private String info;
    17. }

    @TableName 指定表名

    @Data lombok  技术   

     5.编写 mapper

    1. /**
    2. * 使用mp定义Mapper,需要让Mapper接口继承 BaseMapper接口。
    3. */
    4. public interface UserMapper extends BaseMapper {
    5. }

     6.启动类

    1. @MapperScan("com.itheima.mp.mapper")
    2. @SpringBootApplication
    3. public class MybatisPlusSpringbootApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(MybatisPlusSpringbootApplication.class, args);
    6. }
    7. }

     7.测试

    1. @SpringBootTest
    2. public class TestUserMapper {
    3. @Autowired
    4. private UserMapper userMapper;
    5. @Test
    6. public void testSelect(){
    7. User user = userMapper.selectById(1);
    8. System.out.println(user);
    9. Assert.notNull(user,"ID-1,查无此人");
    10. }
    11. }

     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

    3.分页查询

     配置拦截器 在启动类 中添加如下代码,表名 初始化 mybatisplus分页拦截器

    1. /**
    2. * 3.4.0之后提供的拦截器的配置方式
    3. * @return
    4. */
    5. @Bean
    6. public MybatisPlusInterceptor mybatisPlusInterceptor(){
    7. MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
    8. mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    9. return mybatisPlusInterceptor;
    10. }
    11. /**
    12. * 3.4.0之前的版本用这个
    13. * @return
    14. */
    15. /* @Bean
    16. public PaginationInterceptor paginationInterceptor(){
    17. return new PaginationInterceptor();
    18. }*/

     

    1. /**
    2. * 分页查询:
    3. * 1. 当前页码:currentPage
    4. * 2. 每页显示条数:size
    5. *
    6. * 注意:使用mp的分页要设置一个拦截器!!!
    7. */
    8. @Test
    9. public void testSelectPage() {
    10. int current = 1;//当前页码
    11. int size = 2;//每页显示条数
    12. IPage page = new Page(current,size);
    13. userMapper.selectPage(page,null);
    14. List records = page.getRecords();//当前页的数据
    15. long pages = page.getPages();//总页数 2
    16. long total = page.getTotal();//总记录数 4
    17. System.out.println(records);
    18. System.out.println(pages);
    19. System.out.println(total);
    20. }

     

     

     

  • 相关阅读:
    算法刷题打卡第36天:找出字符串中第一个匹配项的下标---BM时间复杂度优化(未完成)
    Hystrix学习
    小蓝的漆房——算法思路
    Centos8上部署MySQL主从备份
    Python 深拷贝和浅拷贝的区别
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java校园二手物品交易系统051x4
    详解module.exports与exports,export与export default,import 与require
    怎样在应用中实现自助报表功能
    Windows下Redis的安装和配置
    微擎模块 美容美发营销版,最新版v1.9.6 原版可用
  • 原文地址:https://blog.csdn.net/weixin_50769390/article/details/126817788