SSMP整合案例
- 案例实现方案分析
- 1.实体类开发———使用
Lombok
快速制作实体类 - 2.Dao(数据访问对象)开发——整合MybatisPlus,制作数据层测试类
- 3.Service开发——基于MybatisPlus进行增量开发,制作业务层测试类
- 4.Controller开发——基于Restful开发,使用
Postman
测试接口功能 - 5.Controller开发——前后端开发协议制作
- 6.页面开发——基于Vue+ElementUI制作,前后端联调,页面数据处理,页面消息处理
- 列表、新增、修改、删除、分页、查询
- 7.项目异常处理
- 8.按条件查询——页面功能调整、Controller修正功能、Service修正功能
- 1.实体类开发———使用
1. 实体类
引入lombok,在实体类中
@Data
作用:为当前实体类在编译期设置对应的get/set方法,toString方法,hashCode方法,equals方法
2. Dao(数据层)
MyBatisPlus的一些配置
mybatis-plus:
global-config:
db-config:
# 表前缀
table-prefix: tbl_
# id自增,解决mybatisplus的雪花算法
id-type: auto
# 日志log
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
MyBatisPlus分页配置–拦截器
package com.itele.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;
@Configuration
public class MPConfig {
// 交给spring 管理了一个bean,这个bean是mybatisplus的拦截器,里面有一个分页的拦截器
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor () {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
分页
void testGetPage () {
IPage page = new Page(2, 1);
bookDao.selectPage(page, null);
System.err.println(page.getTotal());
System.err.println(page.getRecords());
}
按条件查询QueryWrapper
SQL
SELECT id,type,name,description FROM tbl_book WHERE (name LIKE ?)
正常
@Test
void testGetQuerry () {
QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
// 字段为name的,包含Vue字样的
queryWrapper.like("name", "Vue");
bookDao.selectList(queryWrapper);
}
Lambda
@Test
void testGetQuerry2 () {
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
// 字段为name的,包含Vue字样的
lqw.like(Book::getName, "Vue");
bookDao.selectList(lqw);
}
3. Service开发(业务层开发)
- Service层接口定义与数据层接口定义具有较大区别
- selectByUsernameAndPassword(String username, String password);
- login(String username, String password);
业务层开发——快速开发(简便业务层)
- 快速开发方案
- 使用MyBatisPlus提供有业务层通用接(
IService<T>
)与业务层通用实现类(ServiceImpl<M,T>
) - 在通用类基础上做功能重载或功能追加
- 注意重载时不要覆盖原始操作,避免原始提供的功能丢失
快速开发demo
- 使用MyBatisPlus提供有业务层通用接(