一,创建SpringBoot项目
二,引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
三,application.yml配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
#控制台日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
四,在 Spring Boot 启动类中添加 @MapperScan
注解,扫描 Mapper 文件夹:
@MapperScan("com.zl.mapper")
五,编码
1,编写实体类TUser
package com.zl.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TUser {
private Long id;
private String username;
private Integer age;
private String password;
}
2,编写Mapper类 TUserMapper.java
package com.zl.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zl.pojo.TUser;
import java.util.List;
public interface TUserMapper extends BaseMapper<TUser> {
}
3,开始使用
package com.zl;
import com.zl.mapper.TUserMapper;
import com.zl.pojo.TUser;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MybatisPlusApplicationTests {
@Autowired
private TUserMapper tUserMapper;
@Test
void contextLoads() {
List<TUser> userList = tUserMapper.selectList(null);
userList.forEach(System.out::println);
}
}