增上改查的写法总结
HTTP请求消息Request结构
POST /starry/login HTTP/1.1 #请求行
Host: 101.43.67.27:8886 #请求头 start
Connection: keep-alive
Content-Length: 46
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://101.43.67.27:8886
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Mobile Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://101.43.67.27:8886/starry/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9 #请求头 end
username=1808190222&password=020011&inout=1227 #请求体
HTTP请求消息Response结构
HTTP/1.1 200 #响应行
Set-Cookie: JSESSIONID=0A7425A4CDB150882743958F1D48EB14; Path=/starry; HttpOnly #响应头 k-v
Content-Type: text/html;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Thu, 20 Jan 2022 08:13:48 GMT
Keep-Alive: timeout=60
Connection: keep-alive
xxxx #响应体
Request Method
请求基于Spring MVC
请求方式 | 特点 | 幂等性 |
---|---|---|
GET | 1.请求数据拼接在URL上,数据格式为"?key=value&key1=value1" 2.非字母/数字通过BASE64加密 3. 因URL长度限制,参数则有限 4.对与服务端来说传输的数据作为Query Parameters | ✔ |
POST | 1.请求数据包含在RequestBody中,通过Content-Type定义数据格式 | × |
DELETE | 1.请求数据拼接在URL上,数据格式为"?key=value&key1=value1" | ✔ |
PUT | 1.请求数据可拼接在URL上,也可放入请求体中。 | ✔ |
PATCH | 1.请求数据可拼接在URL上,也可放入请求体中。 | ✔ |
@RequestBody
通过@RequestBody修饰的参数将会被参数解析器HttpMessageConverte,将HttpMessage封装为具体的JavaBean对象,通过JackJson解析RequestBody中Json格式的对象 Content-Type: application/json …
@RequestParam
@RequestParam可以解析`Request`中`ParameterMap`参数,即可以解析URL拼接参数与Request-Body表单参数(k-v)。Content-Type:application/x-www-form-urlencoded |application/x-www-form-urlencoded | Content-Type: multipart/form-data
@PathVariable
@PathVariable可以接收URL路径中占位符的值,
ps:
/test1/{option} => /test1/update 与 /test1/update 优先匹配后者。
@GETMapping-@POSTMapping获取参数与request参数位置无关
//发起请求
curl --location --request POST(get) 'http://localhost:8886/starry/t3?a=1,2,3&b=2' \
--form 'c="3"' \
--form 'd="4"'
//都可以绑定到对应参数上
@PostMapping("/t3")
public String test3(String a[],@RequestParam("b") String b,String c,String d){
}
@GetMapping("/t3")
public String test3(String a[],@RequestParam("b") String b,String c,String d){
}
Content-Type
常见数据格式:
-
Content-Type: application/json
-
#Json对象格式 { "t": 1642688041835, "username": "test1", "password": "123456", "uuid": "ce712dde-a466-489b-8edc-526deef2d767", "captcha": "1!" }
-
-
Content-Type: application/x-www-form-urlencoded
- 表单格式 Form-Data: 格式为:
username=1808190222&password=020011&inout=1227
- 表单格式 Form-Data: 格式为:
-
Content-Type: multipart/form-data
-
# boundary定义分隔符 # 数据以分割符分割每一Part 头信息中必须包含一个 Content-Disposition 头 = type(form-data) +name multipart/form-data; boundary=----WebKitFormBoundaryACAkzHcPW2nPoP3x ------WebKitFormBoundaryACAkzHcPW2nPoP3x Content-Disposition: form-data; name="file"; filename="75cd522c11dfa9ecb8983c556fd0f703908fc1d8.jpg" Content-Type: image/jpeg ------WebKitFormBoundaryACAkzHcPW2nPoP3x-- Content-Disposition:form-data;name="username" admin ------WebKitFormBoundaryACAkzHcPW2nPoP3x--
-
Mybatis generator 代码生成器流程
-
通过数据库表生成对应的
model
: Entitiy class, EntitiyExample class (条件操作类) -
通过数据库表生成对应的
interface XXMapper
class -
在Resource目录下生成
XXXMapper.xml
,自动生成了以下方法-
long countByExample(SysLogExample example); #条件查询 int deleteByExample(SysLogExample example); #条件删除 int deleteByPrimaryKey(Long id); #主键删除 int insert(SysLog record); #插入 int insertSelective(SysLog record); #非空插入 List<SysLog> selectByExample(SysLogExample example); #条件查询 SysLog selectByPrimaryKey(Long id); #主键查询 int updateByExampleSelective(@Param("record") SysLog record, @Param("example") SysLogExample example); #条件非空更新 int updateByExample(@Param("record") SysLog record, @Param("example") SysLogExample example); #条件更新 int updateByPrimaryKeySelective(SysLog record); #主键非空更新 int updateByPrimaryKey(SysLog record);#主键更新
-
增删查改:
增
controller->service->mapper
@RestController
@Api(tags = "XXXController", description = "XXX管理")
@RequestMapping("/XXX")
public class XXXController{
@ApiOperation(value = "添加品牌")
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public CommonResult create(@Validated @RequestBody XXXParam XXX) {
CommonResult commonResult;
int count = XXXService.createXXX(XXX);
if (count == 1) {
commonResult = CommonResult.success(count);
} else {
commonResult = CommonResult.failed();
}
return commonResult;
}
}
@Service
public class PmsBrandServiceImpl implements PmsBrandService {
@Override
public int createBrand(PmsBrandParam pmsBrandParam) {
PmsBrand pmsBrand = new PmsBrand();
//参数转为实体
BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
。。。
//插入非空值
return brandMapper.insertSelective(pmsBrand);
}
}
VUE代码
axois post请求头默认application/json,当需使用@RequestParam时,应将参数放置URL中
export function deleteApply(params) {
return request({
url:'/returnApply/delete',
method:'post',
params:params
})
}
/*es6 语法 用于合并/复制对象的属性
修改target对象,然后将它返回:先将source_1对象的所有可枚举属性复制给target,然后依次复制source_1等的属性。*/
Object.assign(target, source_1, ..., source_n)
//对于新增实体表单添加时首先定义默认实体
const defaultBrand={
bigPic: '',
brandStory: '',
factoryStatus: 0,
firstLetter: '',
logo: '',
name: '',
showStatus: 0,
sort: 0
};
export default{
data() {
return {
brand:Object.assign({}, defaultBrand),
}
},
methods:{
//提交按钮时,先通过表单验证
this.$refs[formName].validate((valid) => {
//弹出提示框
if (valid) {
this.$confirm('是否提交数据', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
//确定后,发起请求
createBrand(this.brand).then(response => {
//重置表单
this.$refs[formName].resetFields();
this.brand = Object.assign({},defaultBrand);
//弹出提示
this.$message({
message: '提交成功',
type: 'success',
duration:1000
});
})
})
}
}
}
}
删
@ApiOperation("批量删除")
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public CommonResult delete(@RequestParam("ids") List<Long> ids) {
int count = orderReturnReasonService.delete(ids);
if (count > 0) {
return CommonResult.success(count);
}
return CommonResult.failed();
}
}
//vue URLSearchParams
let params=new URLSearchParams();
params.append("ids",ids);
deleteApply(params).then(response=>{
this.getList();
this.$message({
type: 'success',
message: '删除成功!'
});
});
查
#QueryParam 创建一个查询类
@ApiOperation("查询商品")
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public CommonResult<CommonPage<PmsProduct>> getList(PmsProductQueryParam productQueryParam,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
List<PmsProduct> productList = productService.list(productQueryParam, pageSize, pageNum);
return CommonResult.success(CommonPage.restPage(productList));
}
#vue
const defaultListQuery = {
pageNum: 1,
pageSize: 5,
keyword: null
};
改
@ApiOperation("更新商品")
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
@ResponseBody
public CommonResult update(@PathVariable Long id, @RequestBody PmsProductParam productParam) {
int count = productService.update(id, productParam);
if (count > 0) {
return CommonResult.success(count);
} else {
return CommonResult.failed();
}
}