个人主页: 叶落闲庭
我的专栏:
c语言
数据结构
javaEE
操作系统
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/mybatis?serTimezone=UTC
username: root
password: 123456
@Mapper
public interface UserDao extends BaseMapper<User> {
}
@SpringBootTest
class MybatisplusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
List<User> users = userDao.selectList(null);
System.out.println(users);
}
}
MyBatisPlus特性:
功能 | 自定义接口 | MP接口 |
---|---|---|
新增 | boolean save(T t) |
int insert(T t) |
删除 | boolean delete(int id) |
int deleteById(Serializable id) |
修改 | boolean update(T t) |
int updateById(T t) |
根据id查询 | T getById(int id) |
T selectById(Serializable id) |
查询全部 | List |
List |
分页查询 | PageInfo |
IPage |
按条件查询 | List |
IPage |
@Test
void testSave() {
User user = new User();
user.setId(5);
user.setUsername("小夏");
user.setPassword("123");
user.setGender("女");
user.setAddr("北京");
userDao.insert(user);
}
@Test
void testDelete() {
userDao.deleteById(5);
}
@Test
void testUpdate() {
User user = new User();
user.setId(1);
user.setUsername("张三");
userDao.updateById(user);
}
@Test
void testGetById() {
User user = userDao.selectById(2);
System.out.println(user);
}
Lombok
是一个Java类库,提供了一组注解,简化POJO实体类开发<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<scope>providedscope>
dependency>
@Data
public class User {
private Integer id;
private String username;
private String password;
private String gender;
private String addr;
}
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpInterceptor() {
//1.定义Mp拦截器
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
//2.添加具体的拦截器
mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mpInterceptor;
}
}
@Test
void testGetByPage() {
IPage page = new Page(1,4);
userDao.selectPage(page,null);
System.out.println("当前页码数:" + page.getCurrent());
System.out.println("每页显示数:" + page.getSize());
System.out.println("一共多少页:" + page.getPages());
System.out.println("一共多少条数据:" + page.getTotal());
System.out.println("数据:" + page.getRecords());
}
# 开启mp的日志(输出到控制台)
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl