MBG配合TK.mybatis和freemarker生成SSM框架

MBG配合TK.mybatis\freemarker生成SSM框架

单独使用MBG逆向生成SSM框架文件,只能生成Model类\Xml文件\Mapper类.

要知道在其SSM项目中要有Model层=>Dao(Mapper)数据访问层=>Service业务层=>Controller控制层

所以单独使用MBG生成的文件只能用来减少4/1的工作量,还需要单独生成业务类(甚至还有业务实现类)和控制类,非常麻烦.

TK.Mybatis的基础类提供了生成插件,可以配合MBG生成,再结合freemarker模板渲染,可以直接生成Model\Dao\Service\Controller.对于简单的增删改查的业务来说,非常便利.

而且配合TKMybatis生成的Xml文件非常简洁,没有过多的sql语句.下图对比.

普通MBG生成的XML文件,SQL内容非常多,不容易后续扩展

MBG配合TK生成的XML文件,非常简洁

下面展示配合内容,其使用方法和MBG一样直接执行生成类的Main方法即可.

生成类编写

在Test下创建生成类:

/**
 * @description: 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
 * @author: Zhaotianyi
 * @time: 2021/7/29 14:58
 */
public class CodeGenerator {
    // 数据库连接内容
    public static final String JDBC_URL = "jdbc:mysql://localhost:3306/sys";
    public static final String JDBC_USERNAME = "root";
    private static final String JDBC_PASSWORD = "200428";
    // 数据库连接器
    // MySql8.0以上使用 'com.mysql.cj.jdbc.Driver'
    // Mysql8.0以下使用 'com.mysql.jdbc.Driver'
    private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";

    private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径
    private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/test/resources/generator/template";//模板位置

    private static final String JAVA_PATH = "/src/main/java"; //java文件路径
    private static final String RESOURCES_PATH = "/src/main/resources";//资源文件路径

    public static final String BASE_PACKAGE = "com.test.ipp.demo";//项目基础包名称
    public static final String FRAMEWORK_PACKAGE = "com.test.ipp.demo";//项目核心包位置
    public static final String BUSINESS_PACKAGE= "";//项目业务包位置
    public static final String MAPPER_INTERFACE_REFERENCE = "com.test.ipp.demo.core.Mapper";//通用Mapper类路径

    private static final String PACKAGE_PATH_SERVICE = packageConvertPath("com.test.ipp.demo.service");//生成的Service存放路径
    private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath("com.test.ipp.demo.service.impl");//生成的Service实现存放路径
    private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath("com.test.ipp.demo.controller");//生成的Controller存放路径
    public static final String MODEL_PACKAGE = "com.test.ipp.demo.model";//生成的Model类存放路径
    public static final String MAPPER_PACKAGE = "com.test.ipp.demo.mapper";//Mapper所在包

    private static final String AUTHOR = "CodeGenerator"; //生成模板作者
    private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date()); //生成模板日期

    public static void main(String[] args) {
        genCode("sys_config");
    }
    /**
     * 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
     * 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
     *
     * @param tableNames 数据表名称...
     */
    public static void genCode(String... tableNames) {
        for (String tableName : tableNames) {
            genCodeByCustomModelName(tableName, null);
        }
    }

    /**
     * 通过数据表名称,和自定义的 Model 名称生成代码
     * 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
     *
     * @param tableName 数据表名称
     * @param modelName 自定义的 Model 名称
     */
    public static void genCodeByCustomModelName(String tableName, String modelName) {
        genModelAndMapper(tableName, modelName);
        genService(tableName, modelName);
        genController(tableName, modelName);
    }

    /**
     * 生成Model和Mapper XML文件以及Mapper类
     *
     * @param tableName 数据表名称
     * @param modelName 定义的 Model 名称
     */
    public static void genModelAndMapper(String tableName, String modelName) {
        Context context = new Context(ModelType.FLAT);
        context.setId("Potato");
        context.setTargetRuntime("MyBatis3Simple");
        context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
        context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");

        JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
        jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
        jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
        jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
        jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
        context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);

        PluginConfiguration pluginConfiguration = new PluginConfiguration();
        pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");

        pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE);
        context.addPluginConfiguration(pluginConfiguration);

        JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
        javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);

        javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
        context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);

        SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
        sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
        sqlMapGeneratorConfiguration.setTargetPackage("mapper");
        context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);

        JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
        javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
        javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE);
        javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
        context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);

        TableConfiguration tableConfiguration = new TableConfiguration(context);
        tableConfiguration.setTableName(tableName);
        if (StringUtils.isNotEmpty(modelName)) tableConfiguration.setDomainObjectName(modelName);
        // 默认自增主键名为'id'
        tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
        context.addTableConfiguration(tableConfiguration);

        List<String> warnings;
        MyBatisGenerator generator;
        try &#123;
            Configuration config = new Configuration();
            config.addContext(context);
            config.validate();

            boolean overwrite = true;
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            warnings = new ArrayList<String>();
            generator = new MyBatisGenerator(config, callback, warnings);
            generator.generate(null);
        &#125; catch (Exception e) &#123;
            throw new RuntimeException("生成Model和Mapper失败", e);
        &#125;

        if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) &#123;
            throw new RuntimeException("生成Model和Mapper失败:" + warnings);
        &#125;
        if (StringUtils.isEmpty(modelName)) modelName = tableNameConvertUpperCamel(tableName);
        System.out.println(modelName + ".java 生成成功");
        System.out.println(modelName + "Mapper.java 生成成功");
        System.out.println(modelName + "Mapper.xml 生成成功");
    &#125;

    /**
     * 使用freemaker模板生成Service类
     *
     * @param tableName 数据表名称
     * @param modelName 定义的 Model 名称
     */
    public static void genService(String tableName, String modelName) &#123;
        try &#123;
            freemarker.template.Configuration cfg = getConfiguration();

            Map<String, Object> data = new HashMap<>();
            data.put("date", DATE);
            data.put("author", AUTHOR);
            String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
            data.put("modelNameUpperCamel", modelNameUpperCamel);
            data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
            data.put("basePackage", BASE_PACKAGE);
            data.put("bussnessPackage", BASE_PACKAGE);
            data.put("frameworkPackage", FRAMEWORK_PACKAGE);

            File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java");
            if (!file.getParentFile().exists()) &#123;
                file.getParentFile().mkdirs();
            &#125;
            cfg.getTemplate("service.ftl").process(data,
                    new FileWriter(file));
            System.out.println(modelNameUpperCamel + "Service.java 生成成功");

            File file1 = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE_IMPL + modelNameUpperCamel + "ServiceImpl.java");
            if (!file1.getParentFile().exists()) &#123;
                file1.getParentFile().mkdirs();
            &#125;
            cfg.getTemplate("service-impl.ftl").process(data,
                    new FileWriter(file1));
            System.out.println(modelNameUpperCamel + "ServiceImpl.java 生成成功");
        &#125; catch (Exception e) &#123;
            throw new RuntimeException("生成Service失败", e);
        &#125;
    &#125;

    /**
     * 使用freemaker模板生成Controller类
     * @param tableName
     * @param modelName
     */
    public static void genController(String tableName, String modelName) &#123;
        try &#123;
            freemarker.template.Configuration cfg = getConfiguration();

            Map<String, Object> data = new HashMap<>();
            data.put("date", DATE);
            data.put("author", AUTHOR);
            String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
            data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
            data.put("modelNameUpperCamel", modelNameUpperCamel);
            data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
            data.put("basePackage", BASE_PACKAGE);
            data.put("bussnessPackage", BASE_PACKAGE + BUSINESS_PACKAGE);
            data.put("frameworkPackage", FRAMEWORK_PACKAGE);

            File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java");
            if (!file.getParentFile().exists()) &#123;
                file.getParentFile().mkdirs();
            &#125;
            cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
//            cfg.getTemplate("controller.ftl").process(data, new FileWriter(file));

            System.out.println(modelNameUpperCamel + "Controller.java 生成成功");
        &#125; catch (Exception e) &#123;
            throw new RuntimeException("生成Controller失败", e);
        &#125;

    &#125;

    private static freemarker.template.Configuration getConfiguration() throws IOException &#123;
        freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
        cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
        return cfg;
    &#125;

    private static String tableNameConvertUpperCamel(String tableName) &#123;
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());

    &#125;

    private static String tableNameConvertLowerCamel(String tableName) &#123;
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
    &#125;

    private static String modelNameConvertMappingPath(String modelName) &#123;
        String tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, modelName);
        return tableNameConvertMappingPath(tableName);
    &#125;

    private static String tableNameConvertMappingPath(String tableName) &#123;
        tableName = tableName.toLowerCase();//兼容使用大写的表名
        return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
    &#125;

    private static String packageConvertPath(String packageName) &#123;
        return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
    &#125;
&#125;

其中Model和Mapper的生成均是采用的原MBG的功能,所以方法基本和MBG的XML配置一样.

而Service和Controller的生成是采用Freemarker模板生成的,所以必须在生前检查Freemarker模板中是否有与项目内容与不一致.

Freemarker模板

controller-restful.ftl

package $&#123;bussnessPackage&#125;.web;

import $&#123;frameworkPackage&#125;.core.Result;
import $&#123;frameworkPackage&#125;.core.ResultGenerator;
import $&#123;frameworkPackage&#125;.core.PageReq;
import $&#123;basePackage&#125;.annotation.Auth;
import $&#123;basePackage&#125;.annotation.SysLog;
import $&#123;basePackage&#125;.model.$&#123;modelNameUpperCamel&#125;;
import $&#123;bussnessPackage&#125;.service.$&#123;modelNameUpperCamel&#125;Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by $&#123;author&#125; on $&#123;date&#125;.
*/
@RestController
@RequestMapping("$&#123;baseRequestMapping&#125;s")
public class $&#123;modelNameUpperCamel&#125;Controller &#123;
    @Resource
    private $&#123;modelNameUpperCamel&#125;Service $&#123;modelNameLowerCamel&#125;Service;

    @PostMapping
    @ApiOperation(value = "新增$&#123;modelNameLowerCamel&#125;", notes = "新增$&#123;modelNameLowerCamel&#125;")
    @SysLog("新增$&#123;modelNameLowerCamel&#125;")
    @Auth
    public Result add(@RequestBody $&#123;modelNameUpperCamel&#125; $&#123;modelNameLowerCamel&#125;) &#123;
        $&#123;modelNameLowerCamel&#125;Service.save($&#123;modelNameLowerCamel&#125;);
        return ResultGenerator.genSuccessResult();
    &#125;

    @DeleteMapping
    @ApiOperation(value = "删除$&#123;modelNameLowerCamel&#125;", notes = "删除$&#123;modelNameLowerCamel&#125;")
    @SysLog("删除$&#123;modelNameLowerCamel&#125;")
    @Auth
    public Result delete(@RequestParam(value = "ids") List<Integer> ids) &#123;
        Condition con = new Condition($&#123;modelNameUpperCamel&#125;.class);
        con.createCriteria().andIn("id", ids);
        $&#123;modelNameLowerCamel&#125;Service.deleteByCondition(con);
        return ResultGenerator.genSuccessResult();
    &#125;

    @PutMapping
    @ApiOperation(value = "更新$&#123;modelNameLowerCamel&#125;", notes = "更新$&#123;modelNameLowerCamel&#125;")
    @SysLog("更新$&#123;modelNameLowerCamel&#125;")
    @Auth
    public Result update(@RequestBody $&#123;modelNameUpperCamel&#125; $&#123;modelNameLowerCamel&#125;) &#123;
        $&#123;modelNameLowerCamel&#125;Service.update($&#123;modelNameLowerCamel&#125;);
        return ResultGenerator.genSuccessResult();
    &#125;

    @GetMapping
    @ApiOperation(value = "获取$&#123;modelNameLowerCamel&#125;列表", notes = "获取$&#123;modelNameLowerCamel&#125;列表")
    @SysLog("获取$&#123;modelNameLowerCamel&#125;列表")
    @Auth
    public Result list(PageReq req) &#123;
        PageHelper.startPage(req.getPage(), req.getSize());
        
        Condition con = new Condition($&#123;modelNameUpperCamel&#125;.class);
        Criteria cri = con.createCriteria();
        
        List<$&#123;modelNameUpperCamel&#125;> list = $&#123;modelNameLowerCamel&#125;Service.findByCondition(con);

        return ResultGenerator.genSuccessResult(new PageInfo<$&#123;modelNameUpperCamel&#125;>(list));
    &#125;
&#125;

controller.ftl(普通非Restful)

package $&#123;bussnessPackage&#125;.web;
import $&#123;frameworkPackage&#125;.core.Result;
import $&#123;frameworkPackage&#125;.core.ResultGenerator;
import $&#123;basePackage&#125;.model.$&#123;modelNameUpperCamel&#125;;
import $&#123;bussnessPackage&#125;.service.$&#123;modelNameUpperCamel&#125;Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by $&#123;author&#125; on $&#123;date&#125;.
*/
@RestController
@RequestMapping("$&#123;baseRequestMapping&#125;")
public class $&#123;modelNameUpperCamel&#125;Controller &#123;
    @Resource
    private $&#123;modelNameUpperCamel&#125;Service $&#123;modelNameLowerCamel&#125;Service;

    @PostMapping("/add")
    @ApiOperation(value = "新增$&#123;modelNameUpperCamel&#125;", notes = "新增$&#123;modelNameUpperCamel&#125;")
    public Result add($&#123;modelNameUpperCamel&#125; $&#123;modelNameLowerCamel&#125;) &#123;
        $&#123;modelNameLowerCamel&#125;Service.save($&#123;modelNameLowerCamel&#125;);
        return ResultGenerator.genSuccessResult();
    &#125;

    @PostMapping("/delete")
    @ApiOperation(value = "删除$&#123;modelNameUpperCamel&#125;", notes = "删除$&#123;modelNameUpperCamel&#125;")
    public Result delete(@RequestParam Integer id) &#123;
        $&#123;modelNameLowerCamel&#125;Service.deleteById(id);
        return ResultGenerator.genSuccessResult();
    &#125;

    @PostMapping("/update")
    @ApiOperation(value = "更新$&#123;modelNameUpperCamel&#125;", notes = "更新$&#123;modelNameUpperCamel&#125;")
    public Result update($&#123;modelNameUpperCamel&#125; $&#123;modelNameLowerCamel&#125;) &#123;
        $&#123;modelNameLowerCamel&#125;Service.update($&#123;modelNameLowerCamel&#125;);
        return ResultGenerator.genSuccessResult();
    &#125;

    @PostMapping("/detail")
    @ApiOperation(value = "查询$&#123;modelNameUpperCamel&#125;详情", notes = "查询$&#123;modelNameUpperCamel&#125;详情")
    public Result detail(@RequestParam Integer id) &#123;
        $&#123;modelNameUpperCamel&#125; $&#123;modelNameLowerCamel&#125; = $&#123;modelNameLowerCamel&#125;Service.findById(id);
        return ResultGenerator.genSuccessResult($&#123;modelNameLowerCamel&#125;);
    &#125;

    @PostMapping("/list")
    @ApiOperation(value = "获取$&#123;modelNameUpperCamel&#125;列表", notes = "获取$&#123;modelNameUpperCamel&#125;列表")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) &#123;
        PageHelper.startPage(page, size);
        List<$&#123;modelNameUpperCamel&#125;> list = $&#123;modelNameLowerCamel&#125;Service.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    &#125;
&#125;

service.ftl

package $&#123;bussnessPackage&#125;.service;
import $&#123;basePackage&#125;.model.$&#123;modelNameUpperCamel&#125;;
import $&#123;frameworkPackage&#125;.core.Service;


/**
 * Created by $&#123;author&#125; on $&#123;date&#125;.
 */
public interface $&#123;modelNameUpperCamel&#125;Service extends Service<$&#123;modelNameUpperCamel&#125;> &#123;

&#125;

service-impl.ftl

package $&#123;bussnessPackage&#125;.service.impl;

import $&#123;basePackage&#125;.mapper.$&#123;modelNameUpperCamel&#125;Mapper;
import $&#123;basePackage&#125;.model.$&#123;modelNameUpperCamel&#125;;
import $&#123;bussnessPackage&#125;.service.$&#123;modelNameUpperCamel&#125;Service;
import $&#123;frameworkPackage&#125;.core.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * Created by $&#123;author&#125; on $&#123;date&#125;.
 */
@Service
@Transactional
public class $&#123;modelNameUpperCamel&#125;ServiceImpl extends AbstractService<$&#123;modelNameUpperCamel&#125;> implements $&#123;modelNameUpperCamel&#125;Service &#123;
    @Resource
    private $&#123;modelNameUpperCamel&#125;Mapper $&#123;modelNameLowerCamel&#125;Mapper;

&#125;

总结

内容有些多,但生成出来就爽.

注意:上面的内容需在生成通用Mapper文件和通用AbstractService文件以及通用Service文件前生成

上面的生成目录均为单个包项目,如果为多包项目的话,可以依据注解来进行修改包.