使用Swagger2来测试增删改查,swagger2有自己特定的访问地址:http://localhost:8201/swagger-ui.html
一、增
1.添加医院设置接口
为了生成sign_key签名秘钥 在common/service_util的java目录下新建一个utils包,在里面配置生成32位MD5加密配置类
package com.zhengyibao.yygh.common.utils;
import java.security.MessageDigest;
public class MD5 {
public static String encrypt(String str) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
char[] charArray = str.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray[i] = (byte) charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
@ApiOperation(value = "添加医院设置信息")
@PostMapping("saveHospitalSet")
public Result saveHospitalSet(@RequestBody(required = false) HospitalSet hospitalSet){
hospitalSet.setStatus(1);
Random random=new Random();
hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
boolean save=hosp~~ italSetService.save(hospitalSet);
if(save){
return Result.ok();
}else {
return Result.fail();
}
}
{
"apiUrl": "http://localhost:99/",
"contactsName": "张三",
"contactsPhone": "172636737",
"hoscode": "1000_03",
"hosname": "北京人民医院"
}
二、删
1.根据id逻辑删除医院设置信息
@ApiOperation(value = "根据id逻辑删除医院设置信息")
@DeleteMapping("{id}")
public Result removeHospitalSet(@PathVariable Long id){
boolean flag=hospitalSetService.removeById(id);
if(flag){
return Result.ok();
}else{
return Result.fail();
}
}
2.批量删除医院设置
@ApiOperation(value = "批量删除医院设置")
@PostMapping("batchRemoveHospitalSetByIds")
public Result batchRemoveHospitalSetByIds(@RequestBody List<Long> idList){
hospitalSetService.removeByIds(idList);
return Result.ok();
}
三、改
1.修改医院设置
@ApiOperation(value = "修改医院设置")
@PostMapping("updateHospitalSet")
public Result updateHospitalSet(@RequestBody(required = false) HospitalSet hospitalSet){
boolean update=hospitalSetService.updateById(hospitalSet);
if(update){
return Result.ok();
}else{
return Result.fail();
}
}
{
"apiUrl": "http://localhost:993/",
"contactsName": "肖战",
"contactsPhone": "11233930",
"hoscode": "1000_08",
"hosname": "西安人民医院",
"id": 4
}
四、查
1.查询全部医院设置
@ApiOperation(value = "获取所有医院设置信息")
@GetMapping("findAll")
public Result findAllHospitalSet(){
List<HospitalSet> list=hospitalSetService.list();
return Result.ok(list);
}
2.根据id获取医院设置
@ApiOperation(value = "获取所有医院设置信息")
@GetMapping("findAll")
public Result findAllHospitalSet(){
List<HospitalSet> list=hospitalSetService.list();
return Result.ok(list);
}
3.条件查询带分页接口
在common/service_util中做分页查询的配置类,如果不添加会导致查询时limit失效
package com.zhengyibao.yygh.common.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.zhengyibao.yygh")
public class PageConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
在model的java包下创建vo包,vo包下创建hosp包,包下创建HospitalSetQueryVo类,用于封装分页查询的条件
package com.zhengyibao.yygh.vo.hosp;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class HospitalSetQueryVo {
@ApiModelProperty(value = "医院名称")
private String hosname;
@ApiModelProperty(value = "医院编号")
private String hoscode;
}
package com.zhengyibao.yygh.hosp.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.zhengyibao.yygh.common.result.Result;
import com.zhengyibao.yygh.hosp.service.HospitalSetService;
import com.zhengyibao.yygh.model.hosp.HospitalSet;
import com.zhengyibao.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
@Autowired
private HospitalSetService hospitalSetService;
@ApiOperation(value = "条件查询带分页接口")
@PostMapping("findPageHospitalSet/{current}/{limit}")
public Result findPageHospitalSet(@PathVariable long current,
@PathVariable long limit,
@RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
Page<HospitalSet> page=new Page<>(current,limit);
QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
String hosname=hospitalSetQueryVo.getHosname();
String hoscode=hospitalSetQueryVo.getHoscode();
if(!StringUtils.isNullOrEmpty(hosname)){
wrapper.like("hosname",hosname);
}
if(!StringUtils.isNullOrEmpty(hoscode)){
wrapper.eq("hoscode",hoscode);
}
Page<HospitalSet> pageHospitalSet=hospitalSetService.page(page,wrapper);
System.out.println("这是current:"+current);
return Result.ok(pageHospitalSet);
}
}
使用GetMapping方式提交的测试界面 在HospitalSetQueryVo上添加@RequestBody(required = false)使用PostMapping提交的测试界面
五、其他
1.医院设置锁定和解锁
@ApiOperation(value = "医院设置锁定和解锁")
@PutMapping("lockHospitalSet/{id}/{status}")
public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
HospitalSet hospitalSet=hospitalSetService.getById(id);
hospitalSet.setStatus(status);
hospitalSetService.updateById(hospitalSet);
return Result.ok();
}
2.发送签名秘钥
@ApiOperation(value = "发送签名秘钥")
@PutMapping("sendKeyHospitalSet/{id}")
public Result sendKeyHospitalSet(@PathVariable Long id){
HospitalSet hospitalSet=hospitalSetService.getById(id);
String signKey=hospitalSet.getSignKey();
String hoscode=hospitalSet.getHoscode();
return Result.ok();
}
|