Java学习 --- mybatisplus入门

一、创建一个springboot项目

1、导入pom.xml依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.cjc
    mybatisplus
    0.0.1-SNAPSHOT
    mybatisplus
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            mysql
            mysql-connector-java
            runtime
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2、application.yml配置文件

spring:
  # 配置数据源信息
  datasource:
     # 配置数据源类型
    type: com.zaxxer.hikari.HikariDataSource
     # 配置连接数据库信息
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3307/mybatis_plus?characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

3、创建数据表

CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
CREATE TABLE `user` ( 
`id` bigint(20) NOT NULL COMMENT '主键ID', 
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
 `age` int(11) DEFAULT NULL COMMENT '年龄', 
`email` varchar(50)
 DEFAULT NULL COMMENT '邮箱',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO user (id, name, age, email) VALUES(1, 'Jone', 18, '[email protected]'),(2, 'Jack', 20, '[email protected]'),(3, 'Tom', 28, '[email protected]'),(4, 'Sandy', 21, '[email protected]'),(5, 'Billie', 24, '[email protected]');

4、创建对应实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

5、mapper接口

public interface UserMapper extends BaseMapper {

}

6、配置启动类

@SpringBootApplication
//扫描mapper包
@MapperScan("com.cjc.mybatisplus.mapper")
public class MybatisplusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisplusApplication.class, args);
    }

}

7、测试用例

@SpringBootTest
public class MyBatisPlusTest {
     @Autowired
    private UserMapper userMapper;
     @Test
    public void selectAllUser(){
         List userList = userMapper.selectList(null);
         userList.forEach(System.out::println);
     }
}

Java学习 --- mybatisplus入门_第1张图片

 

你可能感兴趣的:(mybatisplus,spring,boot,java)