SpringBoot系列文章目录
SpringBoot知识范围-学习步骤【JSB系列之000】
————————————————
本系列环境
环境 | win11 |
---|---|
工具 | idea 2017 |
jdk | 1.8 |
数据库 | mysql5.5 |
maven | 3.2.1 |
项目导入方式 | maven 导入 |
数据库前端工具 | mysql-front (navicat 也可以) |
数据库前端工具:mysql-front (navicat 也可以)
主要是这些跟PHPStudy 2018 整合了,所以有的时候懒的打开navicat
————————————————
MybatisPlus
mybatisplus 前身是iBatis ,然后发展成了myBatis
MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
在使用Mybatis进行项目开发的时候,最繁琐的事情就是实体类,dao接口,mapper.xml文件的编写,几乎每个表都需要对应写一套,并且大部分的工作量都在最基本的增删改查上。如果表中的字段进行了修改,那么实体类,mapper文件甚至dao接口都要进行修改。
MybatisPlus
MybatisPlus 可以认为一个Mybatis的外挂,用了这个技术之后 可以不写mapper文件 可以不写dao接口中的方法 然后实现增删改查 分页查询 条件查询 等等
MybatisPlus的使用
注意:要首先删除Mybatis的依赖
因为Mybatisplus中包含有Mybatis的依赖 不需要独立导入 容易jar包冲突
要在pom.xml里引入mybatis-plus
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
在项目的yml 文件里,这里是mybatis-plus
那问题来了,尤其是以前学过mybatis或ibatis的人找不到对应关系。其实,可以再一次的看JPA的部分。
SpringBoot操作数据库jpa–SB系列之006
entity
@Data
@TableName("sys_department")// 映射
public class Sys_Department {
private int id;
private int pid;//上级部门
private String name;//部门名称
private String description;//描述
private long create_time;//创建时间
private String create_by;//创建人
private long update_time;//修改时间
private String update_by;//修改人
private int del_flag;//是否删除
private String sub_ids;//所有子部门id
// get set 自己生成吧
Mapper
然后DAO层呢? 这里取代的是Mapper(跟mybatis差不多)
代码如下:
@Repository
@Mapper
public interface Sys_DepartmentMapper extends BaseMapper<Sys_Department> {
}
你没有看错,一行,只有这一行接口。
Service
那一般用到的增删改查以又怎么使用?
@Service
public class Sys_DepartmentServiceImpl implements Sys_DepartmentService {
@Autowired
private Sys_DepartmentMapper sys_departmentMapper;
public List<Sys_Department> getAllSys_Departments() {
List<Sys_Department> sys_departments = sys_departmentMapper.selectList(null);
System.out.println(sys_departments);
return sys_departments;
}
@Override
public Sys_Department getSys_DepartmentById(Integer id) {
return sys_departmentMapper.selectById(id);
}
@Override
public boolean insertSys_Department(Sys_Department sys_department) {
return sys_departmentMapper.insert(sys_department) > 0 ? true : false;
}
@Override
public boolean deleteSys_DepartmentById(Integer id) {
return sys_departmentMapper.deleteById(id) > 0 ? true : false;
}
@Override
public boolean updateSys_Department(Sys_Department sys_department) {
return sys_departmentMapper.updateById(sys_department) > 0 ? true : false;
}
@Override
public Sys_DepartmentVo queryList(Integer current, Integer size) {
Sys_DepartmentVo sys_departmentVo = new Sys_DepartmentVo();
IPage<Sys_Department> page = new Page<>(current, size);
sys_departmentMapper.selectPage(page, null);
sys_departmentVo.setCurrent(current);
sys_departmentVo.setSize(size);
sys_departmentVo.setTotal(page.getTotal());
sys_departmentVo.setSys_departmentList(page.getRecords());
return sys_departmentVo;
}
}
不但能增删改查,而且还带一个分页的查询。
控制层
事实上控制层的代码还是相当的丰富的。
@RestController
public class Sys_DepartmentController {
@Autowired
private Sys_DepartmentService sysDepartmentService;
@GetMapping("/getAllSys_Departments")
public String getAllSysDepartments() {
return sysDepartmentService.getAllSys_Departments().toString();
}
@GetMapping("/getSys_DepartmentById/{id}")
public Sys_Department getSys_DepartmentById(@PathVariable Integer id) {
return sysDepartmentService.getSys_DepartmentById(id);
}
@GetMapping("/insertSys_Department")
public String insertSys_Department() {
Sys_Department sys_department = new Sys_Department();
sys_department.setName("企划营销部-05");
sys_department.setPid(15);
sys_department.setDescription("企划营销部-05");
sys_department.setCreate_time((new Date()).getTime());
sys_department.setCreate_by(Integer.toString(3));
sys_department.setUpdate_time((new Date()).getTime());
sys_department.setUpdate_by(null);
sys_department.setDel_flag(0);
sys_department.setSub_ids(null);
boolean result = sysDepartmentService.insertSys_Department(sys_department);
if (result) {
return "添加部门成功!";
} else {
return "添加部门失败!";
}
}
@GetMapping("/deleteSys_DepartmentById/{id}")
public String deleteSys_DepartmentById(@PathVariable Integer id) {
boolean result = sysDepartmentService.deleteSys_DepartmentById(id);
if (result) {
return "删除部门成功!";
} else {
return "删除部门失败!";
}
}
@GetMapping("/updateSys_Department")
public String updateSys_Department() {
Sys_Department sys_department = new Sys_Department();
sys_department.setId(12);
sys_department.setPid(15);
sys_department.setDescription("企划销售部-031");
sys_department.setCreate_time((new Date()).getTime());
sys_department.setCreate_by(Integer.toString(3));
sys_department.setUpdate_time((new Date()).getTime());
sys_department.setUpdate_by(null);
sys_department.setDel_flag(1);
sys_department.setSub_ids(null);
boolean result = sysDepartmentService.updateSys_Department(sys_department);
if (result) {
return "修改部门成功!";
} else {
return "修改部门失败!";
}
}
@GetMapping("/querySys_Department/{current}/{size}")
public Sys_DepartmentVo queryList(@PathVariable Integer current, @PathVariable Integer size) {
return sysDepartmentService.queryList(current, size);
}
}
可以看出,增,删,改,查单个,查所有,查分页,都有了。
对于一般的字典表而言,通用的功能都有了。特殊的框架也没有办法预置上。
#分页的处理
要注意的是,这里有一个SQL级的分页。(这也是效率最高的分页方式)
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
跑起来!
说这么多,看了这么多,还是要实践出来才是硬道理!文章的代码及SQL都在本文最后的资源包里
再重新运行插入成功的URL。
修改失败。出现了一个这样的报错
这里就可以看出来mysql 的不友好了。明明是SQL执行的时候,没有更新到要更新的内容。结果页面显示更新失败,会让很多人感觉是更新不成功,程序运行失败了。
但是其实不是!
检查SQL以及参数,会发现这里更新的不是id=1 的,而是更新了一条id=12的数据。
直接进mysql-front,把里面的数据的id改成12
查找操作。
查所有
查单个
查分页
时间关系。就没有搞很多条数据去搞一个一页10条的数据了。
有兴趣的可以试一下。
总结
提示:这里对文章进行总结:
MybatisPlus 不能再引入Mybatis , 从一般的日常操作上,MybatisPlus更加的暴力,简便,甚至都不知道写什么代码了。
配送资源
https://download.csdn.net/download/dearmite/88042686