Springboot常见语法使用总结

这篇博客总结了SpringBoot的常用注解,并详细介绍了如何利用MybatisPlus进行代码生成,包括实体类的配置、逻辑删除和分页插件的使用。同时,还涵盖了配置Swagger、自动插入修改时间、POM和YML文件以及Logback的设置。
摘要由CSDN通过智能技术生成

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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值