SpringBoot 集成 MyBatis-Plus

1. MyBatis-Plus 简介

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

愿景
我们的愿景是成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。
在这里插入图片描述
官方地址https://mp.baomidou.com/

MyBatis-Plus 特性:

官网说的特性太多了,挑几个有特点的分享给大家。

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑。
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作。
  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求。
  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错。
  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用。
  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询。
  • 支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库。

框架结构:

在这里插入图片描述

二. 快速上手

  1. 创建项目
    需要添加 LomBok 及 MySQL Driver 依赖
    在这里插入图片描述
    IDEA 插件 Lombok 简介和安装
    关键依赖包:
<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>
     <version>5.1.18</version>
</dependency>
<dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
 </dependency>
  1. application.properties 添加相关配置:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

logging.level.root=warn
logging.level.com.hxy.ch04.mapper=trace
logging.pattern.console=%p%m%n
  1. 启动类
    在启动类中添加对 Mapper 包扫描 @MapperScan,Spring Boot 启动时会自动加载包路径下的 Mapper。
@SpringBootApplication
@MapperScan("com.hxy.MyBatisPlus.mapper")
public class MyBatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyBatisPlusApplication .class, args);
    }

}
  1. 编码
    编写实体类:
@Data
@TableName("sys_user")
public class User implements Serializable {
    @TableId(type = IdType.AUTO)
    @TableField("user_id")
    private Long userId;
    @TableField("user_name")
    private String userName;
    @TableField("user_password")
    private String userPassword;
    @TableField("user_role_id")
    private Long userRoleId;
    @TableField("user_flag")
    private Integer userFlag;
  • @TableName 注解在类上,指定数据库表名(实体名与表名不一致时需要指定)

  • @Tableld(type = IdType AUTO)注解在主键属性上,且指定主键生成策略为自动增长

  • @TableField注解在属性上,指定数据库字段名(不满足默认匹配规则时需要指定)。属性和字段名默认匹配规则是全小写的属性对应同名的字段;采用骆驼命名规则的属性,对应的字段名为两个单词之间使用_下划线连接,例如: usrName 属性默认对应的字段名为usr_ name。

    编写 Mapper 接口:

public interface UserMapper extends BaseMapper<User> {

}
  1. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Ch04ApplicationTests {
    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelectById(){
        User user = userMapper.selectById(2L);
        System.out.println(user);
    }

    @Test
    public void testInert(){
        User user = new User("xiaomin","111112",1L,2);
        userMapper.insert(user);
    }

    @Test
    public void testFindAll(){
        List<User> list = userMapper.selectList(null);
        list.forEach(user -> System.out.println(user));
    }
}

运行 testSelectById() 效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值