Spring引导类
负责创建bean容器即Spring相关配置,如scan,默认扫描所在包及其子包
REST,表现形式状态转换
优点:
- 隐藏资源的访问行为,无法通过地址得知对资源是何种操作
- 书写简化
按照REST风格访问资源时使用行为动作区分对资源进行了何种操作
@RequestMapping(value=“/users/{id}”,method=RequestMethod.GET)
public void getById(@PathVariable Integer Id){}
使用新注解Post Mapping以及提取books进一步简化书写
yaml
数据读取
自定义对象 封装指定数据
enterprise:
name: itcast
age: 16
//装入Spring容器
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise{
private String name;
private Integer age;
}
@Autowired
private Enterprise enterprise;
整合第三方技术
druid
导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
junit
- 导入测试对应starter
- 测试类使用@SpringBootTest修饰
- 使用自动装配的形式添加要测试的对象
MyBatis
选择技术集:MyBatis,MySQL
数据源参数配置:数据库连接相关信息
定义数据层接口与映射配置
@Mapper
public interface AccountDao {
@Select("select * from account where id = #{id}")
public List<Account> findAll(int id);
}
//不用写Driver(?)
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
MyBatis-Plus
使用步骤
- 手动添加坐标
- 定义数据层接口与映射配置,继承BaseMapper<>
为了识别sys_role表
#Mp配置
//设置所有表前缀
mybatis-plus:
global-config:
db-config:
table-prefix: sys_
//关闭转换大写为下划线
configuration:
map-underscore-to-camel-case: false
//配置自增策略
mybatis-plus:
global-config:
db-config:
id-type: auto
//打印日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
//模糊查询
qw.like("name","spring");
//准确查询+查询条件
lqw.like(name!=null,Book::getName,name);
bookDao.selectList(lqw);
分页拦截器
放在Application所在包及其子包下并标注
@Configuration
public class MPConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//分页拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
SSMP整合
(http://localhost/pages/books.html)
业务层与数据层命名方式不同
业务层:save(逻辑事务)函数返回值为是否成功
@Override
public Boolean save(Book book) {
//bookDao.insert(Book)返回值为收到影响的行数
return bookDao.insert(book) > 0;
}
数据层:insert(数据事务)
int insert(T entity);//“插入记录”,返回值为受影响行数
业务层快速开发
- 使用MP提供的业务层通用接口(IService)与业务层通用实现类(ServiceImpl<M,T>),实现类记得添加@Service
- 在通用类基础上做功能重载或功能追加
public interface IBookService extends IService<Book> {
@Override
boolean save(Book book);
}
@Service
public class IBookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
@Override
public boolean save(Book book) {
return false;
}
}
分页查询
@GetMapping("{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return iBookService.getPage(currentPage,pageSize);
}
@Autowired
private BookDao bookDao;
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
前后端协议联调
//钩子函数
created() {
//调用查询全部数据的操作
this.getAll();
},
methods: {
//列表
getAll() {
//发送异步请求 箭头函数取出response
axios.get("/books").then((res)=>{
console.log(res.data);
this.dataList = res.data.data;
});
}
}
重置表单,添加弹出窗口,添加数据
删除操作
// 删除 row为该行数据
handleDelete(row) {
//防抖
this.$confirm("此操作永久删除当前信息,是否继续?","提示",{type: "info"}).then(()=>{
axios.delete("/books/"+row.id).then((res)=>{
if(res.data.flag){
this.dialogFormVisible = false;
this.$message.success("删除成功");
}else {
this.$message.error("删除失败");
}
}).finally(()=>{
//2.重新加载数据
this.getAll();
});
}).catch(()=>{
this.$message.info("取消操作");
})
}
修改操作(axios.put)
//修改
handleEdit() {
axios.put("/books",this.formData).then((res)=>{
if(res.data.flag){
//1.关闭弹层
this.dialogFormVisible4Edit = false;
this.$message.success("修改成功");
}else {
this.$message.error("修改失败");
}
}).finally(()=>{
//2.重新加载数据
this.getAll();
})
}
异常统一处理
- 使用注解@RestControllerAdvice定义SpringMVC异常处理器用来处理异常
- 异常处理器必须被扫描加载,否则无法生效
- 表现层返回结果的模型类中添加消息属性来传递消息到页面
分页查询
//分页查询
getPage(){
// 发送异步请求 箭头函数取出response
axios.get("/books/"+this.pagination.currentPage+"/"+this.pagination.pageSize).then((res)=>{
// console.log(res.data);
this.pagination.pageSize=res.data.data.size;
this.pagination.currentPage=res.data.data.current;
this.pagination.total=res.data.data.total;
this.dataList = res.data.data.records;
});
}
//切换页码
handleCurrentChange(currentPage) {
//修改页码值
this.pagination.currentPage = currentPage;
this.getPage();
}
条件查询
//组织参数,拼接url
//console.log(this.pagination);
param = "?type="+this.pagination.type;
param += "&name="+this.pagination.name;
param += "&description="+this.pagination.description;
console.log(param);
// 发送异步请求 箭头函数取出response
axios.get("/books/"+this.pagination.currentPage+"/"+this.pagination.pageSize+param).then((res)=>{
// console.log(res.data);
this.pagination.pageSize=res.data.data.size;
this.pagination.currentPage=res.data.data.current;
this.pagination.total=res.data.data.total;
this.dataList = res.data.data.records;
});
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Book book){
//基于SpringMVC自动填装book
IPage<Book> page = iBookService.getPage(currentPage,pageSize,book);
//如果当前页码大于总页码,重新执行查询,使用最大页码值作为当前页码值
if( currentPage > page.getPages()){
page = iBookService.getPage((int)page.getPages(),pageSize,book);
}
return new R(true,page);}
@Override
public IPage<Book> getPage(int currentPage, int pageSize, Book book) {
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
//::获得方法
lqw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());
lqw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
lqw.like(Strings.isNotEmpty(book.getDescription()),Book::getDescription,book.getDescription());
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,lqw);
return page;
}