1、引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、创建实体类User
/**
* @author: xyj
* @date: 2021/4/14
*/
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
3、创建UserMapper
/**
* @author: xyj
* @date: 2021/4/14
*/
public interface UserMapper extends BaseMapper<User> {
}
4、使用测试类,测试
//自动创建spring上下文环境
@SpringBootTest
class MybatisPlusApplicationTests {
@Resource
private UserMapper userMapper;
@Test
void testSelectList() {
//queryWrapper用于复杂查询传参,设置为null表示没有参数查询
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
5、补充在控制台输出sql,在application.properties中配置
#补充、查看SQL日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl