2 Mybatis-Plus快速入门 2.1 准备环境 JDK 8+ Maven 3.5.4 IDEA 2020.3 MySQL5.7 2.2 创建数据库以及表 创建数据库并设置字符集为utf-8: CREATE DATABASE `mp` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'; 创建表和测
2 Mybatis-Plus快速入门
2.1 准备环境
- JDK 8+
- Maven 3.5.4
- IDEA 2020.3
- MySQL5.7
2.2 创建数据库以及表
创建数据库并设置字符集为utf-8:
CREATE DATABASE `mp` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
创建表和测试数据:
-- 创建测试表
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_name` varchar(20) NOT NULL COMMENT '用户名',
`password` varchar(20) NOT NULL COMMENT '密码',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`birthday` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- 插入测试数据
INSERT INTO `tb_user`(`id`,`user_name`,`password`,`name`,`age`,`email`,`birthday`) VALUES
(1,'zhangsan','123456','张三',18,'test1@yh.cn','2019-09-26 11:42:01'),
(2,'lisi','123456','李四',20,'test2@yh.cn','2019-10-01 11:42:08'),
(3,'wangwu','123456','王五',28,'test3@yh.cn','2019-10-02 11:42:14'),
(4,'zhaoliu','123456','赵六',21,'test4@yh.cn','2019-10-05 11:42:18'),
(5,'sunqi','123456','孙七',24,'test5@yh.cn','2019-10-14 11:42:23');
2.3 工程搭建
2.3.1 创建工程
创建maven工程,分别填写GroupId ArtifactId和Version,如下:
<groupId>yh.mp</groupId>
<artifactId>yh-mp-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
2.3.2 导入依赖
导入maven依赖,由于本例采用Spring boot技术,使用mybatis-plus-boot-starter能与其便捷集成:
版本规划:
Spring boot: 2.2.10.RELEASE
mybatis-plus: 3.1.0
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<groupId>com.glls.mp</groupId>
<artifactId>glls-mp-sb</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--简化代码的工具包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatis‐plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</dependencies>
</project>
2.3.3 编写application.yaml
spring:
application:
name: laojun-mp-sb
datasource:
password: 123456
username: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai&useSSL=false
logging:
level:
root: debug
2.3.4 编写pojo
package com.glls.mp.pojo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
/**
* @ClassName : User
* @Author : glls
* @Date: 2021/2/19 14:42
* @Description : 实体类
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
@TableId("ID")
private Long id;
@TableField("USER_NAME")
private String userName; //驼峰命名,则无需注解
@TableField("PASSWORD")
private String password;
@TableField("NAME")
private String name;
@TableField("AGE")
private Integer age;
@TableField("EMAIL")
private String email;
@TableField("BIRTHDAY")
private LocalDateTime birthday;
}
@Data:lombok的注解,使用它可以省略getter/setter方法。
@NoArgsConstructor: 生成无参构造 方法
@AllArgsConstructor:生成所有参数构造 方法,参数顺序与属性定义顺序一致。
@TableName:指定表名
@TableId:指定主键名
@TableField:指定列名
2.3.5 编写mapper
package com.glls.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.glls.mp.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
2.3.6 编写启动类
package com.glls.mp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.glls.mp.mapper")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
2.3.7 编写测试用例
编写UserMapper的测试用例 ,使用UserMapper查询用户列表。在test下创建测试类,包名为 cn.yh.mp。
package com.glls.mp.testmp;
import com.glls.mp.mapper.UserMapper;
import com.glls.mp.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTest {
@Resource
private UserMapper userMapper;
@Test
public void testSelect() {
List<User> userList = userMapper.selectList(null);
for (User user : userList) {
System.out.println(user);
}
}
}
测试结果:
2021-02-19 14:50:15.339 DEBUG 1360 --- [ main] c.glls.mp.mapper.UserMapper.selectList : ==> Preparing: SELECT ID,USER_NAME,PASSWORD,NAME,AGE,EMAIL,BIRTHDAY FROM tb_user
2021-02-19 14:50:15.374 DEBUG 1360 --- [ main] c.glls.mp.mapper.UserMapper.selectList : ==> Parameters:
2021-02-19 14:50:15.412 DEBUG 1360 --- [ main] c.glls.mp.mapper.UserMapper.selectList : <== Total: 5
2021-02-19 14:50:15.413 DEBUG 1360 --- [ main] org.mybatis.spring.SqlSessionUtils : Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7afb1741]
User(id=1, userName=zhangsan, password=123456, name=张三, age=18, email=test1@yh.cn, birthday=2019-09-26T11:42:01)
User(id=2, userName=lisi, password=123456, name=李四, age=20, email=test2@yh.cn, birthday=2019-10-01T11:42:08)
User(id=3, userName=wangwu, password=123456, name=王五, age=28, email=test3@yh.cn, birthday=2019-10-02T11:42:14)
User(id=4, userName=zhaoliu, password=123456, name=赵六, age=21, email=test4@yh.cn, birthday=2019-10-05T11:42:18)
User(id=5, userName=sunqi, password=123456, name=孙七, age=24, email=test5@yh.cn, birthday=2019-10-14T11:42:23)