MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
建议安装 MybatisX 插件
二、整合MyBatis-Pluscom.baomidou mybatis-plus-boot-starter3.4.1
自动配置
- MybatisPlusAutoConfiguration 配置类,MybatisPlusProperties 配置项绑定。yml文件中mybatis-plus:xxx 就是对mybatis-plus的定制
- SqlSessionFactory 自动配置好。底层是容器中默认的数据源、
- mapperLocations 自动配置好的。有默认值。classpath*:/mapper*.xml;任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在 mapper下
- 容器中也自动配置好了 SqlSessionTemplate
- @Mapper 标注的接口也会被自动扫描;建议直接 @MapperScan("com.atguigu.admin.mapper") 批量扫描就行
优点:只需要我们的Mapper继承 baseMapper 就可以拥有crud能力,不需要再在xml写sql语句。
先定义与数据库表对应的Bean,默认找与类名相同的表(开头大小写不区分)。@TableName("user_tb")可以修改对应的表。
@Data
//@TableName("user")
public class User {
//表中没有的属性
@TableField(exist = false)
private String userName;
@TableField(exist = false)
private String password;
//以下是数据库的资料
private Long id;
private String name;
private Integer age;
private String email;
}
再编写对应Mapper继承baseMapper,不需要写sql的xml文件。
//使用mybatis-plus编写mapper baseMapper有写好的方法 public interface UserMapper extends baseMapper{ }
测试:
@SpringBootTest
class BootWebAdminApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void testUserMapper(){
User user =userMapper.selectById(1L);
log.info("用户信息:{}",user);
}
}
三、CRUD功能
不仅Mapper可以继承baseMapper接口,service层也可以继承IService接口,接口里就不用写重复的方法。然后service的实现类继承ServiceImpl类,里面帮忙实现好了默认方法,最后直接放入容器,就可以用里面的默认方法,包括增删改查等。
public interface UserService extends IService{ }
@Service public class UserServiceImpl extends ServiceImplimplements UserService { }
@Controller
public class TableController {
@Autowired
UserService userService;
@GetMapping("/dynamic_table")
public String dynamicTable(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
List list = userService.list();
model.addAttribute("users", list);
return "/table/dynamic_table";
}
分页功能实现
配置分页插件:
@Configuration
public class MyBatisConfig {
@Bean
public MybatisPlusInterceptor paginationInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//分页拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//在最后一页点击下一页,跳回到首页
paginationInnerInterceptor.setOverflow(true);
//每页限制最多500条
paginationInnerInterceptor.setMaxLimit(500L);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
后端逻辑:
@GetMapping("/dynamic_table")
public String dynamicTable(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
List list = userService.list();
// model.addAttribute("users", list);
//第一个参数是当前页码,第二个参数是每页显示多少条记录
Page userPage = new Page<>(pn, 2);
//分页查询结果,包括当前页码,总页数,总记录,所有记录集合。
Page page = userService.page(userPage, null);
model.addAttribute("page", page);
return "/table/dynamic_table";
}
前端展示:
当前第 [[${page.current}]] 页 总计 [[${page.pages}]] 页 共 [[${page.total}]] 记录 Trident Win 95+ Win 95+ Win 95+ 4 X
- ← 上一页
- [[${num}]]
- 下一页 →
删除用户
后端逻辑:
@GetMapping("/user/delete/{id}")
public String deleteUser(@PathVariable("id") Long id, @RequestParam(value = "pn",defaultValue = "1") Integer pn, RedirectAttributes ra) {
userService.removeById(id);
//携带当前页码,确保删除后还在当前页
ra.addAttribute("pn", pn);
return "redirect:/dynamic_table";
}
需要注意重定向是两次请求,所以使用HttpServletRequest携带参数会失效,要使用RedirectAttributes 携带参数。
前端逻辑:
删除



