Mybatis-plus
- 导入Maven 依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
- 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
- 创建POJO 实体类
@Data
public class girl {
private int id;
private String name;
private String address;
}
- 创建接口 操作数据库
@Mapper
public interface usermapper extends BaseMapper<girl> {
}
- 使用测试
@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";
}
}
- Spring Boot 主程序 加入 包扫描
@SpringBootApplication
@MapperScan("app.mapper")
public class Boot {
public static void main(String[] args) {
SpringApplication.run(Boot.class);
}
}