springboot系列17:MyBatis-Plus整合使用

MyBatis-Plus 介绍

        MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。今天讲讲springboot框架如何整合MyBatis-Plus 使用 MyBatis。

 

主要特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响。

  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作。

  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求。

  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错。

  • 支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库。

  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询。

快速上手

1、pom配置

1
2
3
4
5
6
7
8
9
10
11
<dependencies>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

 

2、application.properties配置数据源

1
2
3
4
spring.datasource.url=jdbc:mysql://localhost:3306/multi_test1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=111111
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

3、实体类

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
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
 
//用户实体
@TableName("tb_user")
public class UserEntity {
 
    @TableId(type= IdType.AUTO)
    private Long id;
 
    private UserSexEnum userSex;
    @TableField(value="userName")
    private String userName;
    private String nickName;
 
    @TableField(value="passWord")
    private String passWord;
    //get set
}
 
//用户性别  
   public enum UserSexEnum {
   MAN,WOMEN;
}

 

4、分页插件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
@MapperScan("top.zlcxy.mybatis.plus.mapper")
public class MybatisPlusConfig {
 
    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

 

5、超级简单的mapper

1
2
3
4
5
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.zlcxy.mybatis.plus.domain.UserEntity;
 
public interface UserEntityMapper extends BaseMapper<UserEntity> {
}

 

6、测试单个查询

1
2
3
4
5
6
7
8
9
10
11
12
13
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyBatisPlusTest {
 
    @Autowired
    private UserEntityMapper userEntityMapper;
 
    @Test
    public void testSelectOne() {
        UserEntity user = userEntityMapper.selectById(1L);
        System.out.println(user);
    }
}

 

 

 

7、测试查询所有

1
2
3
4
5
@Test
public void testSelectAll() {
    List<UserEntity> userList = userEntityMapper.selectList(null);
    userList.forEach(System.out::println);
}

 

8、测试查询分页

1
2
3
4
5
6
7
8
9
10
11
@Test
public void testPage() {
    Page<UserEntity> page = new Page<>(0, 10);
    IPage<UserEntity> userIPage = userEntityMapper.selectPage(page, new QueryWrapper<UserEntity>()
            .eq("userName", "zhangl"));
    Assertions.assertThat(page).isSameAs(userIPage);
    System.out.println("总条数 ------> " + userIPage.getTotal());
    System.out.println("当前页数 ------> " + userIPage.getCurrent());
    System.out.println("当前每页显示数 ------> " + userIPage.getSize());
    System.out.println("显示列表 ------> " + userIPage.getRecords());
}

 

 

9、测试新增

1
2
3
4
5
6
7
8
9
10
11
@Test
public void testInsert() {
    UserEntity user = new UserEntity();
    user.setUserName("zl");
    user.setPassWord("123456");
    user.setNickName("老张");
    user.setUserSex(UserSexEnum.valueOf("WOMEN"));
 
    Assertions.assertThat(userEntityMapper.insert(user)).isGreaterThan(0);
    Assertions.assertThat(user.getId()).isNotNull();
}

 

 

10、测试更新

1
2
3
4
5
6
7
8
9
10
@Test
public void testUpdate() {
    UserEntity user = userEntityMapper.selectById(1L);
 
    userEntityMapper.update(
            user,
            Wrappers.<UserEntity>lambdaUpdate().eq(UserEntity::getId,1L).set(UserEntity::getPassWord, "12345")
    );
    Assertions.assertThat(userEntityMapper.selectById(1L).getPassWord()).isEqualTo("12345");
}

 

 

 

10、测试删除

1
2
3
4
5
6
@Test
public void testDelete() {
    Assertions.assertThat(userEntityMapper.deleteById(1L)).isGreaterThan(0);
    Assertions.assertThat(userEntityMapper.delete(new QueryWrapper<UserEntity>()
            .lambda().eq(UserEntity::getUserName, "zhangl"))).isGreaterThan(0);
}

 

posted @   IT6889  阅读(439)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示