我们今天学习一款能够提高开发效率的基于Mybatis的插件MyBatis-Plus,本文适用于掌握SpringBoot及Mybatis的同行们食用。
首先我们了解一下MyBatis-Plus是什么?它是一款基于Mybatis开发的插件,只需要简单的配置就能够完成大量工作,极大的提高了开发效率,并且具备一系列的如代码生成,热加载,数据分页等功能,都是非常方便的。详细介绍在此 -> MyBatis-Plus官网
话不多说,实践最重要。
我们以SpringBoot项目为例进行环境搭建:
CREATE TABLE employee(
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '编号',
name varchar(35) NOT NULL COMMENT '姓名',
gender varchar(1) NOT NULL COMMENT '性别',
address VARCHAR(100) NOT NULL COMMENT '住址',
create_datetime bigint COMMENT '创建时间',
create_by bigint COMMENT '创建人',
update_datetime bigint COMMENT '更新时间',
update_by bigint COMMENT'更新人'
);
pom.xml:
<!-- 依赖SpringBootParent父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<!-- 指定jdk版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入springboot启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- mysql驱动包 由于博主使用的是Mysql进行学习 这里引入Mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis-plus的springboot启动器 目前最新版本是3.2.0 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 减少无谓的工作量 这里引入lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- springBoot test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
spring:
datasource:
url: jdbc:mysql:///数据库?characterEncoding=UTF-8&userSSL=false&serverTimezone=UTC
username:
password:
mybatis-plus:
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名
logging:
level:
com.mybatis.dao: debug # 显示sql
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class Employee {
private Long id;
private String name;
private String gender;
private String address;
private Long createDatetime;
private Long createBy;
private Long updateDatetime;
private Long updateBy;
// set/get/toString/eq/hashCode..由lombok提供
}
// 在dao包下创建一个接口继承BaseMapper并指定泛型 该接口就已经具备CRUD功能了
public interface EmployeeMapper extends BaseMapper<Employee> { }
@SpringBootApplication
@MapperScan("com.mybatis.dao") // 扫描dao接口
public class MybatisPlusDemoAApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusDemoAApplication.class, args);
}
}
// 现在开始进行测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisPlusDemoAApplicationTests {
@Resource
private EmployeeMapper employeeMapper;
// 无条件查询全部数据
@Test
public void findAll() {
List<Employee> employees = employeeMapper.selectList(null);
employees.forEach(System.out::println);
/* 发送的SQL语句
SELECT
id,
create_datetime,
create_by,update_
datetime
,address,
gender,
update_by,
name
FROM
employee
*/
}
// 根据主键查询数据
@Test
public void findById() {
Employee employee = employeeMapper.selectById(1L);
System.out.println(employee);
/**
SELECT
id,
create_datetime,
create_by,
update_datetime,
address,gender,
update_by,
name FROM
employee
WHERE
id= ?
*/
}
// 添加数据
@Test
public void insert() {
Employee employee = new Employee(null, "李四", '0', "中关村", System.currentTimeMillis(), 1L, System.currentTimeMillis(), 1L);
// 返回插入成功的记录数
int flag = employeeMapper.insert(employee);
System.out.println(flag >= 1 ? "OK" : "ERROR");
/**
INSERT
INTO
employee
(
id,
create_datetime,
create_by,
update_datetime,
address,
gender,
update_by,
name ) VALUES
( ?, ?, ?, ?, ?, ?, ?, ? )
*/
}
// 根据主键删除数据
@Test
public void del() {
// 返回删除成功记录数
int flag = employeeMapper.deleteById(1L);
System.out.println(flag >= 1 ? "OK" : "ERROR");
/**
DELETE FROM employee WHERE id=?
*/
}
// 根据主键更新数据
@Test
public void update() {
// 更新需要将带主键的模型传递进行更新
Employee employee = new Employee(1L, "李白", '1', "华山之巅", System.currentTimeMillis(), 1L, System.currentTimeMillis(), 1L);
// 返回更新成功记录数
int flag = employeeMapper.updateById(employee);
System.out.println(flag >= 1 ? "OK" : "ERROR");
/**
UPDATE
employee
SET
create_datetime=?,
create_by=?,
update_datetime=?,
address=?,
gender=?,
update_by=?,
name=?
WHERE id=?
*/
}
}
最基本的增删改查完成了!是不是感觉非常简单呢?