此文主要基于官网 case 整理,如需了解更多详情,请移步官网。
环境:
- SpringBoot:3.1.6
- JDK:17
- MySQL:5.7
数据准备
主要是 MySQL 建库建表,插入一些数据。
建库:
sql
CREATE DATABASE mybatis_demo;
建表:
sql
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
id BIGINT NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
插入数据:
sql
DELETE FROM `user`;
INSERT INTO `user` (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
依赖
此处添加依赖请多注意,注意点:
- 1.mysql的驱动,现在新版的普遍用依赖中的
mysql-connector-j
- 2.mybatis-plus,
mybatis-plus-boot-starter
->mybatis-plus-spring-boot3-starter
[1]
xml
<!-- mysql driver -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<!-- mp依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
</dependency>
<!-- @Data -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
application.yaml
yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://{ip}:3306/mybatis_demo?&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
先启动看看有没有什么问题:
启动正常。
接下来是相关编码实现,这是我的代码目录:
主要实现
主目录下新建 entity 实体类目录,新建 User.java:
java
package com.example.springbootmybatisplusdemo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
在项目主目录下建个 mapper 目录,放置 mapper 接口,新建 UserMapper.java:
java
package com.example.springbootmybatisplusdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplusdemo.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
主要继承接口,没有另外新声明方法。
项目启动入口处加入 MapperScan:
java
package com.example.springbootmybatisplusdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.example.springbootmybatisplusdemo.mapper")
public class SpringbootMybatisPlusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPlusDemoApplication.class, args);
}
}
可以重启下看看。
接下来主要是基于上面的内容进行测试。
测试
在 test 下新建 SampleTest.java:
typescript
package com.example.springbootmybatisplusdemo.test;
import com.example.springbootmybatisplusdemo.mapper.UserMapper;
import com.example.springbootmybatisplusdemo.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.Assert;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class SampleTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println("---selectAll method test---");
List<User> userList = userMapper.selectList(null);
Assert.isTrue(5 == userList.size());
userList.forEach(System.out::println);
}
@Test
public void testSelectById() {
System.out.println("--- select by id ---");
User user = userMapper.selectById(1);
System.out.println(user.toString());
}
@Test
public void testSelectByCond() {
System.out.println("--- select by specific condition ---");
Map<String, Object> cond = new HashMap<>();
cond.put("name", "Sandy");
List<User> userList = userMapper.selectByMap(cond);
System.out.println(userList.toString());
}
@Test
public void testInsert() {
System.out.println("--- insert test ---");
User newUser = new User();
newUser.setId(6L);
newUser.setName("Jordan");
newUser.setAge(56);
newUser.setEmail("Jordan@example.com");
userMapper.insert(newUser);
}
@Test
public void testDeleteByMap() {
System.out.println("--- delete test ---");
Map<String, Object> map = new HashMap<>();
map.put("name", "Jordan");
int res = userMapper.deleteByMap(map);
System.out.println("result: " + res);
}
}
运行测试。
上面的就是一个简单的运用,通过 mybatis-plus,我们通过继承 BaseMapper接口,就可以实现简单的 增删改查,更多的复杂的应用,我们将在后面进一步呈现。
参考: