配置文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lomok 简化 Java 代码 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
/*
Navicat Premium Data Transfer
Source Server : mysql57
Source Server Type : MySQL
Source Server Version : 50728
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50728
File Encoding : 65001
Date: 24/02/2020 14:51:03
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`age` int(2) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES (1, 'Jeson', 2);
INSERT INTO `t_user` VALUES (2, 'Jack', 3);
编写代码
/**
* Data 注解提供类的get、set、equals、hashCode、canEqual、toString方法
* AllArgsConstructor 提供全参构造
* NoArgsConstructor 提供无参构造
* TableName("t_user") 指定实体类对应表名为 "t_user"
* @author Jeson
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_user")
public class User {
/**
* TableId 注解指定该属性对应表中的主键 id, type 指定主键生成策略
* TableField 指定该属性对应表中字段名字(在名字不一致或下划线转驼峰命名无法对应时使用)
*/
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long userId;
@TableField("name")
private String username;
private Integer age;
}
public interface UserMapper extends BaseMapper<User> {
}
@SpringBootTest
class CurdTests {
@Autowired
private UserMapper userMapper;
@Test
void selectAll() {
System.out.println("----- selectAll method test ------");
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
@Test
void insert() {
System.out.println("----- insert method test -----");
int rows = userMapper.insert(new User(null, "testNmae", 11));
System.out.println(rows);
}
@Test
void update() {
System.out.println("----- update method test -----");
int rows = userMapper.updateById(new User(1231590206946033665L, "testName2", 10));
System.out.println(rows);
}
@Test
void delete() {
System.out.println("----- delete method test -----");
int rows = userMapper.deleteById(1231590206946033665L);
System.out.println(rows);
}
}
- 简单使用 MyBatis Plus 对数据库进行了 CURD 操作,MyBatis Plus 还有大量使用功能,详情参考官方文档