> 文章列表 > Mybatis-Plus

Mybatis-Plus

Mybatis-Plus

简介

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

官网地址https://baomidou.com/

 功能特性

  • 通用 CRUD 操作
  • 分页插件
  • 条件构造器
  • 代码生成器
  • SQL 注入器
  • 元对象( meta-object ) 功能
  • 多租户 SQL 解析器
  • 动态表名 SQL 解析器
  • 性能分析插件

Mybatis-Plus 提供了强大的CRUD操作,内置通用Mapper、通用Service,仅仅通过少量配置即可实现单表大部分CRUD操作,更有强大的条件构造器,满足各类使用需求。【基于SpringBoot

使用方法

一、导入依赖

  <dependencies><!--Spring Boot Web模块的依赖,包括了Spring MVC和Tomcat等依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--MySQL数据库驱动的依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.49</version></dependency><!--Spring Boot的核心依赖,包括了Spring和Spring Boot的各种依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--Spring Boot测试模块的依赖,包括了JUnit、Mockito等依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--MyBatis-Plus的Spring Boot Starter依赖,包括了MyBatis、MyBatis-Spring等依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3</version></dependency><!--阿里巴巴数据库连接池的依赖--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.9</version></dependency><!--JUnit 5测试框架的API依赖--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.9.0</version><scope>test</scope></dependency></dependencies>

二、在resources/application.yml中 配置数据源和 MyBatis-Plus 的相关配置

server:port: 8081
spring:datasource:driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: "0123"url: jdbc:mysql://localhost:3306/sys?characterEncoding=utf8&allowMultiQueries=truetype: com.alibaba.druid.pool.DruidDataSourcedruid:one:max-active: 100min-idle: 20max-wait: 2000mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpltype-aliases-package: com.project.beanmapper-locations: classpath:*/*Mapper.xml

这里注意两个细节:

缩进问题:

        在YAML文件中,缩进是非常重要的,因为它表示了层级关系。例如,server和spring都是顶级属性,因此它们的缩进为0。而port和datasource则是server和spring的子属性,因此它们的缩进为2。以此类推。同一层级的属性,其缩进的空格数必须相同

密码问题:

        密码作为字符串类型时会被解释为八进制数。因此,如果密码以0开头,可能会导致密码被错误地解释为八进制数,从而导致密码错误或其他问题。为了避免这种情况发生需要使用双引号将其括起来。

三、创建启动类 

  • 启动Spring Boot 应用程序并初始化必要的组件和依赖项
@SpringBootApplication
public class MainMyBatisPlus {public static void main(String[] args) {SpringApplication.run(MainMyBatisPlus.class);}
}

四、编写实体类和业务接口

//实体类映射的数据库表名
@TableName("t_student")
public class StudentBean {//定义主键列对应属性,value表示该属性映射的列名//type = IdType.AUTO 表示主键生成策略//IdType.AUTO  表示利用数据库表的自动增长列填充主键列的值@TableId(value = "pk_stuID",type = IdType.AUTO)private Integer id;//定义当前属性 对应的列名@TableField("s_name")private String name;@TableField("s_gender")private String sex;@TableField("s_birthday")private LocalDate birthday;@TableField("s_tel")private String phone;
}
public interface IStudentService {void add(StudentBean stuBean);void del(Integer id);void update(Integer id,String tel);List<StudentBean> findAll();StudentBeanfindById(Integer id);List<StudentBean> findByName(String name);List<StudentBean> findByBirthday(LocalDate start,LocalDate end);
}

五、创建mapper接口

  • 必须继承BaseMapper  且需加@Mapper注解
@Mapper
public interface IStudentMapper extends BaseMapper<StudentBean> {}

六、书写业务接口的实现类

  •  使用 MyBatis-Plus 提供的 API 进行 CRUD 操作
@Service
@Transactional
public class StudentServiceImp implements IStudentService {@Autowiredprivate IStudentMapper mapper;@Overridepublic void add(StudentBean studentBean) {mapper.insert(studentBean);}@Overridepublic void del(Integer id) {mapper.deleteById(id);}@Overridepublic void update(Integer id, String tel) {ManBean man = mapper.selectById(id);man.setPhone(tel);mapper.updateById(man);}@Overridepublic List<StudentBean> findAll() {return mapper.selectList(null);}@Overridepublic StudentBean findById(Integer id) {return mapper.selectById(id);}@Overridepublic List<StudentBean > findByName(String name) {QueryWrapper<StudentBean > qw = new QueryWrapper<>();//模糊查询  列名,值qw.like("s_name",name);return mapper.selectList(qw);}@Overridepublic List<StudentBean > findByBirthday(LocalDate start, LocalDate end) {QueryWrapper<StudentBean > qw = new QueryWrapper<>();//大于qw.ge("s_birthday",start);//小于qw.le("s_birthday",end);return mapper.selectList(qw);}
}

七、创建测试类

@SpringBootTest(classes = MainMyBatisPlus.class)
public class ManTest {@Autowiredprivate IStudentService service;@Testpublic void test(){}
}

煤炭资源网