tianyun 2 年之前
父節點
當前提交
d9c054aa12

+ 1 - 1
mybatis-test/pom.xml

@@ -41,7 +41,7 @@
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
-            <version>3.5.2</version>
+            <version>3.5.1</version>
         </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>

+ 38 - 3
mybatis-test/src/main/java/com/alvin/db/controller/AccessRoleController.java

@@ -2,8 +2,15 @@ package com.alvin.db.controller;
 
 
 import org.springframework.web.bind.annotation.RequestMapping;
+import com.alvin.db.service.IAccessRoleService;
+import com.alvin.db.entity.AccessRole;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import java.util.List;
+
 
-import org.springframework.stereotype.Controller;
 
 /**
  * <p>
@@ -13,8 +20,36 @@ import org.springframework.stereotype.Controller;
  * @author tianyun
  * @since 2022-08-17
  */
-@Controller
-@RequestMapping("/db/access-role")
+@RestController
+@RequestMapping("/access-role")
 public class AccessRoleController {
 
+    @Resource
+    private IAccessRoleService accessRoleService;
+
+    @PostMapping
+    public Boolean save(@RequestBody AccessRole accessRole) {
+        return accessRoleService.saveOrUpdate(accessRole);
+    }
+
+    @DeleteMapping("/{id}")
+    public Boolean delete(@PathVariable Integer id) {
+        return accessRoleService.removeById(id);
+    }
+
+    @GetMapping
+    public List<AccessRole> findAll() {
+        return accessRoleService.list();
+    }
+
+    @GetMapping("/{id}")
+    public List<AccessRole> findOne(@PathVariable Integer id) {
+        return accessRoleService.list();
+    }
+
+    @GetMapping("/page")
+    public Page<AccessRole> findPage(@RequestParam Integer pageNum,
+                                     @RequestParam Integer pageSize) {
+        return accessRoleService.page(new Page<>(pageNum, pageSize));
+    }
 }

+ 17 - 10
springboot-main/src/main/java/com/alvin/Generator.java

@@ -18,18 +18,18 @@ public class Generator {
         String db_password = "root";
 
         String javaDir = "C:/code";
-        String parentPackage = "com.alvin";
-        String moduleName = "db";
+        String parentPackage = "com.alvin.db";
         String tableName = "access_role";
 
         String author = "tianyun";
         String tablePrefix = "t_";
 
 
-        generatorToFile(db_url, db_username, db_password, author, javaDir, parentPackage, moduleName, tableName, tablePrefix);
+        generatorToFile(db_url, db_username, db_password, author, javaDir, parentPackage, tableName, tablePrefix);
     }
 
-    private static void generatorToFile(String db_url, String db_username, String db_password, String author, String javaDir, String parentPackage, String moduleName, String tableName, String tablePrefix) {
+    private static void generatorToFile(String db_url, String db_username, String db_password, String author,
+                                        String javaDir, String parentPackage, String tableName, String tablePrefix) {
         FastAutoGenerator.create(db_url, db_username, db_password)
                 //===全局配置
                 .globalConfig(builder -> {
@@ -42,23 +42,30 @@ public class Generator {
                 //===模块配置
                 .packageConfig(builder -> {
                     builder.parent(parentPackage) // 设置父包名
-                            .moduleName(moduleName) // 设置父包模块名
+                            .moduleName("") // 设置父包模块名
                             .pathInfo(Collections.singletonMap(OutputFile.mapperXml, javaDir + "/../resources/mapper")); // 设置mapperXml生成路径
                 })
                 .strategyConfig(builder -> {
-                    builder.addInclude(tableName) // 设置需要生成的表名
-                            .addTablePrefix(tablePrefix)
-                            .entityBuilder()
+                    //Controller
+                    builder.controllerBuilder().enableHyphenStyle()  // 开启驼峰转连字符
+                            .enableRestStyle();  // 开启生成@RestController 控制器
+                    //mapper
+                    builder.mapperBuilder().enableMapperAnnotation().build();
+                    //实体类
+                    builder.entityBuilder()
                             .enableLombok()
                             .addTableFills(
                                     new Column("create_time", FieldFill.INSERT),
                                     new Column("update_time", FieldFill.INSERT_UPDATE)
-                            )
+                            );
+                    builder.addInclude(tableName) // 设置需要生成的表名
+                            .addTablePrefix(tablePrefix)
+
                     ;
 
                 })
                 .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
-                //.templateConfig(builder -> builder.controller(""))  // 设置为空则不会生成该文件
+                .templateConfig(builder -> builder.controller("templates/controller1.java"))  // 设置为空则不会生成该文件
                 //.templateConfig(builder -> builder.service(""))
                 //.templateConfig(builder -> builder.serviceImpl(""))
                 .execute();

+ 70 - 0
springboot-main/src/main/resources/templates/controller1.java.ftl

@@ -0,0 +1,70 @@
+package ${package.Controller};
+
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import ${package.Entity}.${entity};
+import ${package.Service}.${table.serviceName};
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+
+<#if superControllerClassPackage??>
+import ${superControllerClassPackage};
+</#if>
+
+/**
+* <p>
+    * ${table.comment!} 前端控制器
+    * </p>
+*
+* @author ${author}
+* @since ${date}
+*/
+<#if restControllerStyle>
+@RestController
+<#else>
+@Controller
+</#if>
+@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
+<#if kotlin>
+    class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
+<#else>
+    <#if superControllerClass??>
+public class ${table.controllerName} extends ${superControllerClass} {
+    <#else>
+public class ${table.controllerName} {
+    </#if>
+
+    @Resource
+    private ${table.serviceName} ${table.entityPath}Service;
+
+    @PostMapping
+    public Boolean save(@RequestBody ${entity} ${table.entityPath}) {
+    return ${table.entityPath}Service.saveOrUpdate(${table.entityPath});
+    }
+
+    @DeleteMapping("/{id}")
+    public Boolean delete(@PathVariable Integer id) {
+    return ${table.entityPath}Service.removeById(id);
+    }
+
+    @GetMapping
+    public List<${entity}> findAll() {
+    return ${table.entityPath}Service.list();
+    }
+
+    @GetMapping("/{id}")
+    public List<${entity}> findOne(@PathVariable Integer id) {
+    return ${table.entityPath}Service.list();
+    }
+
+    @GetMapping("/page")
+    public Page<${entity}> findPage(@RequestParam Integer pageNum,
+    @RequestParam Integer pageSize) {
+    return ${table.entityPath}Service.page(new Page<>(pageNum, pageSize));
+    }
+}
+</#if>