MybatisPlus_01

1.1 简介

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

1.1.1 特性

1.1.2 框架结构

2.1 快速开始

首先创建一张数据表如下:

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

插入数据:


DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

2.1.1

在springboot工程中添加mybatisplus依赖包:

 <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.1</version>
    </dependency>

2.1.2 yaml文件配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
    password: xhj970829
    username: root

2.1.3 编码

编写实体类 User.java(此处使用了 Lombok 简化代码):

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

编写Mapper类 UserMapper.java

public interface UserMapper extends BaseMapper<User> {

}

2.1.4 测试

@SpringBootTest(classes = MybatisdemoApplication.class)
@RunWith(SpringRunner.class)
class MybatisdemoApplicationTests {
@Autowired
    UserMapper userMapper;
    @Test
    void contextLoads() {




    }
    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}

控制台输出:

3.1 思考

我们先来回顾一下之前Mybayis的操作一般是先编写dao接口层,第二步主要有两种一种是直接在dao接口层里使用注解的方式将每一个方法与sql语句绑定,还有一种也是我比较
喜欢的一种方式在Resource下新建mapper文件,给每一个dao接口编写对应的mapper.xml文件,这样的好处是代码可读性比较高,不会糅杂在一起。

那么MyabiysPlus改变了什么?
首先

MybatisPlus其实帮我们写好了很多基本的CRUD操作,我们可以看看这个继承类里有什么:

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);

    int deleteById(Serializable id);

    int deleteByMap(@Param("cm") Map<String, Object> columnMap);

    int delete(@Param("ew") Wrapper<T> queryWrapper);

    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    int updateById(@Param("et") T entity);

    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);

    T selectById(Serializable id);

    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);

    T selectOne(@Param("ew") Wrapper<T> queryWrapper);

    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);

    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);

    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);

    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}

这里面内置了很多咱们工作中能用到的基本CRUD操作,我们也不用写注解也不用编写mapper.xml文件。

当然注解和mapper.xml这种方式在MyabtisPlus中也是支持的,这就是它的无侵入性。