Mybatis-plus 搭建环境

Mybatis-plus

  1. 导入Maven 依赖
<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
</dependency>
  1. Spring Boot 配置文件配置数据源
server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/love?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
  1. 创建POJO 实体类
@Data
public class girl {
    private int id;
    private String name;
    private String address;
}
  1. 创建接口 操作数据库
@Mapper
public interface usermapper extends BaseMapper<girl> {

}
  1. 使用测试
@RestController
public class login {

    @Autowired
    private usermapper usermapper;

    @RequestMapping("/index")
    public String test(){
        if(usermapper == null){
            return "为空";
        }
        List<girl> girls = usermapper.selectList(null);
        for (girl g:girls) {
            System.out.println(g);
        }
        return "hello world";
    }
}
  1. Spring Boot 主程序 加入 包扫描
@SpringBootApplication
@MapperScan("app.mapper")
public class Boot {
    public static void main(String[] args) {
        SpringApplication.run(Boot.class);
    }
}
posted @ 2023-03-24 23:16  起始者  阅读(40)  评论(0编辑  收藏  举报