注意:
数据库中的字段 last_name 与 实体类中的 lastName 一致
官网:https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7
一、环境
1、配置maven
<!-- 数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency>
2、连接数据库
位置 application.yaml
spring: datasource: username: root password: 123456 url: jdbc:mysql://127.0.0.1:3306/mybatisplus?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true driver-class-name: com.mysql.cj.jdbc.Driver
三、编码
1、编写实体类
pojo-----User.java
package com.wt.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private Long id; private String name; private Integer age; private String email; }
2、编写mapper接口
mapper----UserMapper
package com.wt.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.wt.pojo.User; public interface UserMapper extends BaseMapper<User> { }
3、在springboot启动程序入口类添加注解
@MapperScan("com.wt.mapper")
package com.wt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.wt.mapper")
@SpringBootApplication
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
4、测试
@Test public void sampleTest(){ List<User> userList = userMapper.selectList(null); for (User user : userList) { System.out.println(user); } }