• 一文入门mybatis-plus


    mybatis-plus

    mybatis-plus官网
    MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生.

    可以说呢,在MP基础上还可以继续使用mybatis的特性。
    MP只给出单表查询的接口,复杂的业务还是需要我们自己来的。
    其实我并不是很喜欢这个工具包。sql写的少了sql会忘,以后出新的框架,很难快速的去兼容。

    入门配置

    1.创建springboot模块
    在这里插入图片描述
    Group ,package name,Artifict 很熟悉吧。
    2.勾选sql里的驱动模块和mybatis。
    在这里插入图片描述
    3.导入依赖包

    <dependencies>
    		//mybatis-springboot启动依赖
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.2.2version>
            dependency>
    		//mysql连接依赖
            <dependency>
                <groupId>com.mysqlgroupId>
                <artifactId>mysql-connector-jartifactId>
                <scope>runtimescope>
            dependency>
            //MP的依赖包
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.4.1version>
            dependency>
            //springboot整合的测试依赖
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
            //数据库连接池
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.2.12version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    4.创建数据库的表

    CREATE TABLE `t_user` (
    `id` bigint(20) NOT NULL COMMENT '主键ID',
    `name` varchar(30) DEFAULT NULL COMMENT '姓名',
    `age` int(11) DEFAULT NULL COMMENT '年龄',
    `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    INSERT INTO t_user (id, name, age, email) VALUES
    (1, 'Jone', 18, 'test1@baomidou.com'),
    (2, 'Jack', 20, 'test2@baomidou.com'),
    (3, 'Tom', 28, 'test3@baomidou.com'),
    (4, 'Sandy', 21, 'test4@baomidou.com'),
    (5, 'Billie', 24, 'test5@baomidou.com');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5.项目结构
    在这里插入图片描述

    5.pojo实体类

    import com.baomidou.mybatisplus.annotation.*;
    import lombok.Data;
    
    @Data
    @TableName("t_user") //设置表名
    public class User {
    
        //将属性所对应的字段设置为主键
        @TableId(value = "id",type = IdType.ASSIGN_ID)
        private Long id;
    
        @TableField(value = "name")
        private String name;
    
        private Integer age;
    
        private String email;
        @TableField("Deleted")
        @TableLogic(value = "0",delval="1")
        private Integer deleted;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    注意
    @Data是lombok包下的注解。标注实体类,可以不用写构造器,get和set方法,toString方法。
    @TableName(“t_user”) //设置对应数据库表名的
    其他的使用的再说。
    6.配置文件
    数据库username和password写自己的哈。
    还配置mybatis和MP的日志

    spring:
      datasource:
        #配置数据库连接池
        type: com.alibaba.druid.pool.DruidDataSource
        #驱动类,url,username,password
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ssm?serverTimeZone=UTC
        username: root
        password: 1234
    
    mybatis:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    mybatis-plus:
      global-config:
        db-config:
          #设置表名前缀
          table-prefix: t_
          #设置主键生成策略
          id-type: assign_id
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      type-aliases-package: com.atguigu.pojo
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    7.定义mapper接口
    BaseMapper是MP提供的接口,里面有很多的api供我们使用。
    @Mapper注解标识一下。可以被springboot识别到。

    import com.atguigu.pojo.User;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.Map;
    
    @Mapper
    public interface userMapper extends BaseMapper<User> {
    
        Map<String,Object> selectMapById(Long id);
    
        /**
         * 通过年龄查询并分页
         * @param page MP 所提供的的分页对象,必须放第一个位置
         * @param age
         * @return
         */
        Page<User> selectPageVo(@Param("page") Page<User> page,@Param("age") Integer age);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    8.测试

    import com.atguigu.mapper.userMapper;
    import com.atguigu.pojo.User;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.*;
    
    @SpringBootTest
    class MybatisplusDemo2ApplicationTests {
    
        @Autowired
        private userMapper userMapper;
    
        @Test
        public void testGetallUsers() {
    
            List<User> users = userMapper.selectList(null);
    
            for(User user:users) System.out.println(user);
        }
    
        /**
         * 测试添加功能
         * 与此同时,可以获取新增数据的id
         */
        @Test
        public void testInsertUser(){
            User user = new User();
            user.setName("张三");
            user.setAge(23);
            user.setEmail("123456@qq.com");
            int insert = userMapper.insert(user);
            System.out.println(insert);
            System.out.println("id==>"+user.getId());
        }
    
        /**
         * 根据id来删除信息
         */
        @Test
        public void testDeleteById(){
    
            int i = userMapper.deleteById(1592081315866370050l);
            System.out.println(i);
        }
        /**
         * 根据map来删除信息
         * 根据map中的条件来删除信息
         * key--字段名 value--条件值
         */
        @Test
        public void testDeleteByMap(){
            Map<String, Object> map=new HashMap<>();
            map.put("name","张三");
            map.put("age","23");
           userMapper.deleteByMap(map);
        }
    
        /**
         *  通过多个id来删除
         */
        @Test
        public void testDeleteByCollection(){
            List<Long> list=new ArrayList<>();
            list.add(1592425378192941058l);
            list.add(1592425378192941059l);
            int i = userMapper.deleteBatchIds(list);
            System.out.println(i);
        }
    
        /**
         *修改用户信息
         */
        @Test
        public void testUpdate(){
            User user = new User();
            user.setId(4l);
            user.setName("lisi");
            user.setEmail("123443@qq.com");
            int i = userMapper.updateById(user);
            System.out.println(i);
        }
    
        /**
         * 根据id来查询信息
         */
        @Test
        public void testSelectByID(){
            User user = userMapper.selectById(1);
            System.out.println(user);
        }
        @Test
        public void testSelectByIDs(){
            List<Long> list=new ArrayList<>();
            list.add(1l);
            list.add(2l);
            list.add(3l);
            List<User> users = userMapper.selectBatchIds(list);
            for(User user:users) System.out.println(user);
        }
    
        /**
         * 根据map集合中的信息来查询
         */
        @Test
        public void testSelectByMap(){
            Map<String,Object> map=new HashMap<>();
            map.put("age",20);
            List<User> users = userMapper.selectByMap(map);
            System.out.println(users);
        }
    
        /**
         * 测试自定义sql功能
         * mp对原生的mybatis只做了增强,不做影响
         */
        @Test
        public void testSelectById(){
            Map<String, Object> map = userMapper.selectMapById(1l);
            System.out.println(map);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    MP的条件查询可以通过QueryWrapper来完成!此外MP还有分页功能。
    通过Page类辅助完成!
    好了,MP入门就到这里了!!!

  • 相关阅读:
    阿里云数据盘挂载目录
    springboot事务的失效场景
    腾讯春招JAVA后端面试总结
    落地DataOps,必须克服的挑战有哪些?
    让我们进入面向对象的世界(四)
    AdminLTE 2 图标大全
    这份数据安全自查checklist请拿好,帮你补齐安全短板的妙招全在里面!
    闭坑1:Django “TemplateDoesNotExist at /index/“解决/Django加载HTML出错
    干货|红队全流程学习资料
    第二十四天 网站Getshell的方法
  • 原文地址:https://blog.csdn.net/zmm0628/article/details/127878617