第四讲
使用Mybatis Plus实现crud
编写过滤器
package com.example.demo.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/09/6:53
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
@Configuration
//配置类这样里面的配置信息可以被读取。
public class MPConfig {
// 拦截器 limt
@Bean
// 第三方Bean mp的拦截器,当你需要定义多个拦截器时候可以创建
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
编写业务层server层
package com.example.demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.domain.User;
import java.util.List;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/09/8:42
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
//业务层 service
//接口写完了得写实现类
public interface UserService {
Boolean save(User user);
Boolean updata(User user);
Boolean delete(Integer id);
User getById(Integer id);
List<User> getALL();
IPage<User> getPage(int currentPage,int pagesize);
}
编写实现了implement
package com.example.demo.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dao.UserDao;
import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/09/8:48
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
//UserService接口的实现类的实现类
// implements 实现接口 加上之后可以实现指定泛型的抽象方法
// @Service用来定义业务层的bean
@Service
public class UserServiceImpl implements UserService {
// 转调数据层需要注入
@Autowired
private UserDao userDao;
@Override
public Boolean save(User user) {
// 返回值为null 编写查询方法
// return userDao.insert(user) 中insert是影响行操作设置
return userDao.insert(user) >0;
}
@Override
public Boolean updata(User user) {
return userDao.updateById(user) >0;
}
@Override
public Boolean delete(Integer id) {
return userDao.deleteById(id) >0;
}
@Override
public User getById(Integer id) {
return userDao.selectById(id);
}
@Override
public List<User> getALL() {
return userDao.selectList(null);
}
@Override
public IPage<User> getPage(int currentPage,int pagesize) {
IPage objectIPage = new Page(currentPage,pagesize);
userDao.selectPage(objectIPage,null);
return objectIPage;
}
}
编写测试类
package com.example.demo.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃ @Auther: wangshenghui
* ┗━┓ ┏━┛ @Date: 2022/08/09/9:40
* ┃ ┃神兽保佑
* ┃ ┃代码无BUG!
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
@SpringBootTest
//测试注解
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
void contextLoads() {
System.out.println(userService.getById(1));
}
@Test
void testsave() {
User user = new User();
// mp中使用id的增加策略是雪花算法 如果设置了id自增需要另写配置在application.yml
user.setUsername("亚索");
user.setPassword("eeeeee");
user.setAccount("aaqaqr");
userService.save(user);
}
@Test
void testupdate(){
User user = new User();
// mp中使用id的增加策略是雪花算法 如果设置了id自增需要另写配置在application.yml
user.setId(3);
user.setUsername("吴亦凡");
user.setPassword("eeeeee");
user.setAccount("wordbig");
userService.updata(user);
}
@Test
void testDelete(){
userService.delete(2);
}
@Test
void testSeletALL(){
System.out.println(userService.getALL());
}
@Test
void testbGetpage(){
IPage<User> page = userService.getPage(1,1);
System.out.println(page.getPages());
System.out.println(page.getTotal());
System.out.println(page.getSize());
System.out.println(page.getCurrent());
System.out.println(page.getRecords());
}
}