springboot常见注解(此处通过具体的实例,来方便查询)
@RestController//返回rest风格请求
@RequestMapping("/eval/v1/dashboard")//下面的所有url地址钱都添加/eval/v1/dashboard
public class DashboardController {
@Autowired
DashboardService dashboardService;
@Autowired
UserService userService;
@PostMapping("/")//post请求
public ApiResult createDashboard(@RequestBody JSONObject requestBody) {
//接受请求体消息
//JSONObject view = viewService.createView(requestBody);
JSONObject dashboardWithViews = dashboardService.createDashboardWithViews(requestBody);
return new ApiResult(200, dashboardWithViews, "创建成功");
}
@GetMapping("/{id}")//get请求,获取url中/1中的数字1
public ApiResult getDashboardById(@PathVariable(name = "id") long id) {
JSONObject dashboardAndItsViews = dashboardService.getDashboardInfoById(id);
return new ApiResult(200, dashboardAndItsViews, "查询成功");
}
@DeleteMapping("/{id}")//发送delete请求 获取Url地址中/1中的1
public ApiResult deleteDashboardById(@PathVariable(name = "id") long id) {
dashboardService.deleteDashboardById(id);
return new ApiResult(200, "", "删除成功");
}
@PutMapping("/")// 发送put请求,接受请求体中的消息
public ApiResult updateDashboard(@RequestBody JSONObject requestBody) {
JSONObject dashboardWithViews = dashboardService.updateDashboardWithViews(requestBody);
return new ApiResult(200, dashboardWithViews, "修改成功");
}
@GetMapping("/views") //发送get请求,接受Url中的 ?id=1&page=1?size=3的参数
public ApiResult getDashboardAndItsViews(@RequestParam(name = "id") Long id,
@RequestParam(name = "page") int page,
@RequestParam(name = "size") int size) {
JSONObject results = dashboardService.getDashboardAndItsViews(id, page, size);
return new ApiResult(200, results, "查询成功");
}
@GetMapping("/")
public ApiResult getAllDashboards(
@RequestParam(name = "page") int page,
@RequestParam(name = "size") int size) {
JSONObject results = dashboardService.getAllDashBoards(page,size);
return new ApiResult(200, results, "查询成功");
}
}
mybatisPlus代码自动生成
可以参考mybatisplus官网代码自动生生
public static void main(String[] args) {
//构建一个代码生成器
AutoGenerator mpg = new AutoGenerator();
//配置策略
//1、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("Wangguo");
gc.setOpen(false);
gc.setFileOverride(false);
gc.setServiceImplName("%sService");
gc.setIdType(IdType.ID_WORKER);
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
//配置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/art_evaluation?useSSL=false&characterEncoding=utf-8&useUnicode=true&serverTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("000000");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
//包配置
PackageConfig pc = new PackageConfig();
//pc.setModuleName("evaluation");
pc.setParent("com.ghhd");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
//策略配置
StrategyConfig strategy = new StrategyConfig();
//配置要映射的表名
strategy.setInclude("data_domain","field","view","dash_board","view_dash_board");
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setLogicDeleteFieldName("status");
//自动填充配置
TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
TableFill updateTime = new TableFill("update_time",FieldFill.UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(createTime);
tableFills.add(updateTime);
strategy.setTableFillList(tableFills);
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true