1. 创建一个数据库(test),创建一个user表
CREATE DATABASE test;
USE test;
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. 新建一个springboot工程
2.1 引入pom依赖
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.3.1.tmp</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.18</ version>
</ dependency>
2.2 创建一个application.yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
2.3 新建一个实体类
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
2.4 新建一个userMapper类
public interface UserMapper extends BaseMapper < User> {
}
2.5 在springboot启动类中加上@MapperScan
@MapperScan ( "com.mybatisplus.demo.dao" )
@SpringBootApplication
public class DemoApplication {
public static void main ( String[ ] args) {
SpringApplication. run ( DemoApplication. class , args) ;
}
}
2.6 在测试类中写测试代码
@SpringBootTest
@RunWith ( SpringRunner. class )
class DemoApplicationTests {
@Autowired ( required = false )
private UserMapper userMapper;
@Test
public void testSelect ( ) {
List< User> userList = userMapper. selectList ( null) ;
userList. forEach ( System. out: : println) ;
}
}
3. 最后测试成功
User{ id= 1 , name= 'Jone' , age= 18 , email= 'test1@baomidou.com' }
User{ id= 2 , name= 'Jack' , age= 20 , email= 'test2@baomidou.com' }
User{ id= 3 , name= 'Tom' , age= 28 , email= 'test3@baomidou.com' }
User{ id= 4 , name= 'Sandy' , age= 21 , email= 'test4@baomidou.com' }
User{ id= 5 , name= 'Billie' , age= 24 , email= 'test5@baomidou.com' }