1.创建数据库,初始化数据
use plustest;
DROP TABLE IF EXISTS user ;
CREATE TABLE user
(
id BIGINT ( 20 ) NOT NULL COMMENT '主键ID' ,
name VARCHAR ( 30 ) NULL DEFAULT NULL COMMENT '姓名' ,
age INT ( 11 ) NULL DEFAULT NULL COMMENT '年龄' ,
email VARCHAR ( 50 ) NULL DEFAULT NULL COMMENT '邮箱' ,
PRIMARY KEY ( id)
) ;
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' ) ;
2. 导入相关依赖
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.mybatis.spring.boot</ groupId>
< artifactId> mybatis-spring-boot-starter</ artifactId>
< version> 2.2.2</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.5.1</ version>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-devtools</ artifactId>
< scope> runtime</ scope>
< optional> true</ optional>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< scope> runtime</ scope>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-configuration-processor</ artifactId>
< optional> true</ optional>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< optional> true</ optional>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
</ dependencies>
3. 编写yml配置文件
spring :
application :
name : user- center
datasource :
driver-class-name : com.mysql.cj.jdbc.Driver
username : ****
password : ****
url : jdbc: mysql: //localhost: 3306/plustest? useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimeZone=Asia/Shanghai
server :
port : 9000
mybatis-plus :
configuration :
log-impl : org.apache.ibatis.logging.stdout.StdOutImpl
4. 编写实体类,在启动类上添加扫描注解
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
5. 编写mapper接口
@Mapper
public interface UserMapper extends BaseMapper < User > {
}
6. 单元测试
@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 . assertEquals ( 5 , userList. size ( ) ) ;
userList. forEach ( System . out:: println ) ;
}
}
7.测试结果