谷粒学苑第二天
创建父工程
代码生成器:(直接去文件夹 复制粘贴)
最好写绝对路径:
需要加时区
其他的不需要改
老师的目录是这样的:
我的怎么这么奇怪:
被折叠了
好了:
这是啥
找到注解的具体实现逻辑(全局搜索注解名.class)
Controller 注service service注mapper
service已经自动调mapper了
注入mapper(自动)
小问题
直接打字是错的
快捷方法:.var 回车
也不是直接输入是错的 是刚刚导错包了
最基本的开发:
创建启动类:
创建配置类 配置mapper扫描及其他
最终测试 使用8001端口
mapper加上@Mapper注解 才会在spring容器里
http://localhost:8001/eduservice/teacher/findAll
json形式作返回
时区配置
讲师逻辑删除
逻辑删除注解:
编写controller方法:
swagger整合过程
Swagger测试
配置类:
service_edu要用service_base
要想这个配置类加载到
要在启动类上增加这个scan注解 还有包的名字 才会去扫描包里面的内容
报错:
配置文件加到这个子模块里面 Maven没刷新时报错 刷新后不报了
http://localhost:8001/swagger-ui.html
controller接口部分
需要传参数;
让其有中文提示:
package com.atguigu.commonutils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data //lombok注解 会生成get set方法
public class R {
@ApiModelProperty(value = "是否成功")
private Boolean success;
@ApiModelProperty(value = "返回码")
private Integer code;
@ApiModelProperty(value = "返回消息")
private String message;
@ApiModelProperty(value = "返回数据")
private Map<String, Object> data = new HashMap<String, Object>();
//把构造方法私有
private R() {}
//成功静态方法
public static R ok() {
R r = new R(); //在自己的类里面 所以可以new
r.setSuccess(true);
r.setCode(ResultCode.SUCCESS); // 20000 直接调ResultCode.SUCCESS
r.setMessage("成功");
return r;
}
//失败静态方法
public static R error() {
R r = new R(); //在自己的类里面 所以可以new
r.setSuccess(false);
r.setCode(ResultCode.ERROR); // 20000 直接调ResultCode.SUCCESS
r.setMessage("失败");
return r;
}
public R success(Boolean success){
this.setSuccess(success);
return this; //返回当前类的对象 return this;就能写成链式编程 R.ok().code().message()
}
public R message(String message){
this.setMessage(message);
return this;
}
public R code(Integer code){
this.setCode(code);
return this;
}
public R data(String key, Object value){
this.data.put(key, value);
return this;
}
public R data(Map<String, Object> map){
this.setData(map);
return this;
}
}
以后写接口都统一返回类型R
先引入依赖 才能够用里面相关内容
引入自己建的 别引苞米豆
统一结果返回:
讲师分页功能
配置mp分页插件
package com.atguigu.eduservice.controller;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduTeacherService;
//import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.security.PermitAll;
//import java.util.HashMap;
//import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import java.util.Map;
//import java.awt.*;
/**
* <p>
* 讲师 前端控制器
* </p>
*
* @author testjava
* @since 2023-02-20
*/
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher") //最后访问的路径 http://localhost:8001/eduservice/teacher/findAll
class EduTeacherController {
//把service注入
@Autowired
private EduTeacherService teacherService;
//1 查询讲师表所有数据
//rest风格
@ApiOperation(value = "所有讲师列表")
@GetMapping("findAll") //方法的名字 写findAll和/findAll都一样
//public List<EduTeacher> findAllTeacher() {
public R findAllTeacher() {
List<EduTeacher> list = teacherService.list(null);
return R.ok().data("items",list); //链式写法 值:list
}
//2 逻辑删除讲师
@DeleteMapping("{id}") //id值需要通过路径传递 http://localhost:8001/eduservice/teacher/findAll/1 如果id值是1
// public boolean removeById(@ApiParam(name = "id", value = "讲师ID", required = true)@PathVariable String id){ //path路径 得到路径中的id值
public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true)@PathVariable String id){ //path路径 得到路径中的id值
boolean flag = teacherService.removeById(id);
if(flag) {
return R.ok();
} else {
return R.error();
}
}
//分页查询讲师方法
//current:当前页 limit:每页记录数
@GetMapping("pageTeacher/{current}/{limit}") //通过路径传值
public R pageListTeacher(@PathVariable long current, //通过这种方式得到两个值
@PathVariable long limit) {
//创建page对象
Page<EduTeacher> pageTeacher = new Page<>(current,limit);
//调用方法实现分页 调用方法时,底层封装,把分页所有数据封装到pageTeacher对象里面
teacherService.page(pageTeacher,null);
long total = pageTeacher.getTotal();//得到一共有多少条记录数
List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
//方法1
return R.ok().data("total",total).data("rows",records);
/* //方法2
Map map = new HashMap(); // 爆红了 不知道为啥
map.put("total",total);
map.put("rows",records);
return R.ok().data(map);*/
}
}
条件查询分类
用其来进行数据的封装
遇到小问题
继续
//4 条件查询带分页的方法
@GetMapping("pageTeacherCondition/{current}/{limit}")
public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
TeacherQuery teacherQuery) { //用对象形式得到条件TeacherQuery teacherQuery
//创建page对象
Page<EduTeacher> pageTeacher = new Page<>(current, limit);
//构建条件
QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
//多条件组合查询 类似mybatis的动态mysql
String name = teacherQuery.getName();
Integer level = teacherQuery.getLevel();
String begin = teacherQuery.getBegin();
String end = teacherQuery.getEnd();
//判断条件值是否为空,如果不空 拼接条件 工具类StringUtils
if(!org.springframework.util.StringUtils.isEmpty(name)) {
//构建条件
wrapper.like("name",name);
}
if(!org.springframework.util.StringUtils.isEmpty(level)) {
wrapper.like("level",level);
}
if(!org.springframework.util.StringUtils.isEmpty(begin)) { //表中字段gmt_create名称 不是属性名称
wrapper.ge("gmt_create",begin); //ge 大于等于
}
if(!org.springframework.util.StringUtils.isEmpty(name)) {
wrapper.le("gmt_create",end);
}
//调用方法实现条件查询分页
teacherService.page(pageTeacher,wrapper);
long total = pageTeacher.getTotal();//得到一共有多少条记录数
List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
return R.ok().data("total", total).data("rows", records);
加注解
没查出来 解决
查出来了:
自动填充
查询 修改
统一异常处理
手动异常:
但还是能正常查询???
这里刚刚写错地方了
这个要注释掉 service_base里面已经有common_utils 不能重复 依赖传递