Mybatis-plus 开发教程
IService使用
首先要写一个mapper类继承mybatis-plus的BaseMapper
不需要写入@Mapper注解
public interface UserMapper extendx BaseMapper<User>{
IPage <User> getList(@Param("page") Page<User> page,@Param("data") User data);
}
service层需要继承IService,当然实现层也要继承对应的实现类。
public interface UserService extends IService<User>{
IPage <User> getList(@Param("page") Page<User> page,@Param("data") User data);
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService{
@Override
IPage <User> getList(@Param("page") Page<User> page,@Param("data") User data){
return baseMapper.getList(page,data)
}
}
controller使用
@Api(value = "AdvanceFile控制器")
@Slf4j
@ExtController
@RequestMapping("/order/advanceFile")
public class AdvanceFileController {
@Autowired
IAdvanceFileService advanceFileService;
@PostMapping("/list")
@ApiOperation(httpMethod = GlobalConstant.Method.POST,value = "预通知文件表查询")
public ExtResult<List<AdvanceFile>> list(@RequestBody ExtStoreRead<AdvanceFile> request){
if(!Boolean.TRUE.equals(request.getUsePaging())){
QueryWrapper<AdvanceFile> wrapper = new QueryWrapper<>(request.getData());
return ExtResult.ok(advanceFileService.list(wrapper));
}else{
Page<AdvanceFile> page = new Page<>(request.getPage(), request.getLimit());
QueryWrapper<AdvanceFile> wrapper = new QueryWrapper<>(request.getData());
return ExtResult.ok(advanceFileService.page(page,wrapper));
}
};
@PostMapping("/add")
@ApiOperation(httpMethod = GlobalConstant.Method.POST,value = "预通知文件表增加")
public ExtResult add(@RequestBody ExtStoreCreate<AdvanceFile> request){
advanceFileService.saveBatch(request.getData());
return ExtResult.ok();
};
@PostMapping("/delete")
@ApiOperation(httpMethod = GlobalConstant.Method.POST,value = "预通知文件表删除")
public ExtResult delete(@RequestBody ExtStoreDelete<Long> request){
advanceFileService.removeByIds(request.getData());
return ExtResult.ok();
};
@PostMapping("/update")
@ApiOperation(httpMethod = GlobalConstant.Method.POST,value = "预通知文件表修改")
public ExtResult update(@RequestBody ExtStoreUpdate<AdvanceFile> request){
advanceFileService.updateBatchById(request.getData());
return ExtResult.ok();
};
}
实体类格式
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("erp_template_maintenance")
@ApiModel(value="TemplateMaintenance", description="TemplateMaintenance对象")
public class TemplateMaintenance implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主键id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "模板编号")
private String template_bh;
@ApiModelProperty(value = "模板序号")
private Integer template_xh;
@ApiModelProperty(value = "模板名称")
private String template_name;
@ApiModelProperty(value = "模板内容")
private String template_content;
@ApiModelProperty(value = "操作员名")
private String create_name;
@ApiModelProperty(value = "操作时间")
private Date create_time;
@ApiModelProperty(value = "最后更新人")
private String last_update_name;
@ApiModelProperty(value = "最后更新时间")
private Date last_update_date;
}
分页插件的使用
BaseMapper使用
@SpringBootTest
public class TestUserDAO {
@Autowired
private UserDAO userDAO;
@Test
public void testFindAll(){
List<User> users = userDAO.selectList(null);
users.forEach(user -> System.out.println("user = "+user));
}
@Test
public void testFind(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age",3);
List<User> users = userDAO.selectList(queryWrapper);
users.forEach(user -> System.out.println(user));
}
@Test
public void testFindLike(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","a");
List<User> users = userDAO.selectList(queryWrapper);
users.forEach(user -> System.out.println("user="+user));
}
@Test
public void testSava(){
User entity = new User();
entity.setName("x").setAge(6).setBir(new Date());
userDAO.insert(entity);
}
@Test
public void testFindPage(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age",2);
IPage<User> page = new Page<>(1,2);
IPage<User> userIPage = userDAO.selectPage(page, queryWrapper);
long total = userIPage.getTotal();
System.out.println("总记录数"+total);
userIPage.getRecords().forEach(user -> System.out.println("user ="+user));
}
}
EQ 就是 EQUAL等于
NE就是 NOT EQUAL不等于
GT 就是 GREATER THAN大于
LT 就是 LESS THAN小于
GE 就是 GREATER THAN OR EQUAL 大于等于
LE 就是 LESS THAN OR EQUAL 小于等于
然后我们可以引入lambda,避免我们在代码中写类似的于user_name的硬编码
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(User::getuserName, userName);
List<User> users= userMapper.selectList(wrapper);
MP除了通用的Mapper还是通用的Servcie层,这也减少了相对应的代码工作量,把通用的接口提取到公共。其实按照MP的这种思想,可以自己也实现一些通用的Controller。
|