Search in sources :

Example 1 with TableField

use of com.baomidou.mybatisplus.generator.config.po.TableField in project heifer by galaxy-sea.

the class GeneratorCode method injectionConfig.

private static InjectionConfig injectionConfig(PackageConfig packageInfo, DataSourceConfig dataSourceConfig) {
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
            this.setMap(map);
        // to do nothing
        }

        @Override
        public void initTableMap(TableInfo tableInfo) {
            super.initTableMap(tableInfo);
            tableInfo.setComment(pattern.matcher(tableInfo.getComment()).replaceAll(""));
            for (TableField field : tableInfo.getFields()) {
                if (StringUtils.isNotEmpty(field.getComment())) {
                    field.setComment(pattern.matcher(field.getComment()).replaceAll(""));
                }
            }
        }
    };
    String projectPath = System.getProperty("user.dir");
    // 如果模板引擎是 freemarker
    String templatePath = "generator/templates/mapper.xml.ftl";
    // 如果模板引擎是 velocity
    // String templatePath = "/templates/mapper.xml.vm";
    List<FileOutConfig> focList = new ArrayList<>();
    focList.add(new FileOutConfig(templatePath) {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            if (moduleName == null) {
                return projectPath + "/src/main/resources/mapper/" + "" + tableInfo.getXmlName() + "Mapper" + ConstVal.XML_SUFFIX;
            }
            moduleName = moduleName.replace(".", "/");
            return projectPath + "/src/main/resources/mapper/" + moduleName + "/" + tableInfo.getXmlName() + ConstVal.XML_SUFFIX;
        }
    });
    cfg.setFileOutConfigList(focList);
    return cfg;
}
Also used : FileOutConfig(com.baomidou.mybatisplus.generator.config.FileOutConfig) ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) TableField(com.baomidou.mybatisplus.generator.config.po.TableField)

Example 2 with TableField

use of com.baomidou.mybatisplus.generator.config.po.TableField in project chao-cloud by chaojunzi.

the class ZipVelocityTemplateEngine method appendTableInfo.

/**
 * 添加
 * @param tableInfo 表信息
 * @param objectMap 模板对象
 */
private void appendTableInfo(TableInfo tableInfo, Map<String, Object> objectMap) {
    // 请求路径前缀
    objectMap.put("controllerMappingPrefix", tableInfo.getName().replaceFirst("_", "/"));
    // shiroPermissionsPrefix
    objectMap.put("shiroPermissionsPrefix", tableInfo.getName().replaceFirst("_", ":"));
    // 是否存在date 数据类型
    boolean hasDate = tableInfo.getFields().stream().anyMatch(f -> DbColumnType.DATE.getType().equals(f.getPropertyType()));
    if (hasDate) {
        tableInfo.getImportPackages().add("org.springframework.format.annotation.DateTimeFormat");
        tableInfo.getImportPackages().add("cn.hutool.core.date.DatePattern");
    }
    // 版本
    objectMap.put("version", EnableMybatisGenerator.VERSION);
    // 主键名称类型
    TableField pk = tableInfo.getFields().stream().filter(f -> f.isKeyFlag()).findFirst().orElse(null);
    objectMap.put("pk", pk);
    // 弹窗大小
    long i = tableInfo.getFields().stream().filter(f -> !(f.isKeyFlag() || f.getName().equals(getConfigBuilder().getStrategyConfig().getVersionFieldName()))).count();
    // 比率
    long rate = 30 + (i - 1) * 8;
    objectMap.put("openHeight", rate > 100 ? 100 : rate);
    // 模糊查询 tableInfo
    String comment = tableInfo.getComment();
    Map<String, String> map = this.commentToMap(comment);
    TableCommentParse parse = BeanUtil.mapToBean(map, TableCommentParse.class, true);
    objectMap.put("likeFields", parse.parseLike(tableInfo.getFields()));
    // 标题
    objectMap.put("menuTitle", parse.getTitle());
    // 关键字-保留字处理
    Map<String, String> kws = tableInfo.getFields().stream().collect(Collectors.toMap(TableField::getName, t -> KeyWordConstant.contains(t.getName()) ? "`" : ""));
    objectMap.put("kws", kws);
    if (kws.values().contains("`")) {
        tableInfo.getImportPackages().add("com.baomidou.mybatisplus.annotation.TableField");
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) HtmlTemplateConfig(com.chao.cloud.common.extra.mybatis.generator.template.HtmlTemplateConfig) MapUtil(cn.hutool.core.map.MapUtil) TableCommentParse(com.chao.cloud.common.extra.mybatis.generator.parse.TableCommentParse) BeanUtil(cn.hutool.core.bean.BeanUtil) Template(org.apache.velocity.Template) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) ConstVal(com.baomidou.mybatisplus.generator.config.ConstVal) VelocityEngine(org.apache.velocity.app.VelocityEngine) Map(java.util.Map) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) BusinessException(com.chao.cloud.common.exception.BusinessException) CollectionUtils(com.baomidou.mybatisplus.core.toolkit.CollectionUtils) FileType(com.baomidou.mybatisplus.generator.config.rules.FileType) ZipEntry(java.util.zip.ZipEntry) KeyWordConstant(com.chao.cloud.common.extra.mybatis.constant.KeyWordConstant) FileOutConfig(com.baomidou.mybatisplus.generator.config.FileOutConfig) DbColumnType(com.baomidou.mybatisplus.generator.config.rules.DbColumnType) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) List(java.util.List) StrUtil(cn.hutool.core.util.StrUtil) Stream(java.util.stream.Stream) EnableMybatisGenerator(com.chao.cloud.common.extra.mybatis.annotation.EnableMybatisGenerator) VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) ReflectUtil(cn.hutool.core.util.ReflectUtil) AbstractTemplateEngine(com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine) FileUtil(cn.hutool.core.io.FileUtil) TableField(com.baomidou.mybatisplus.generator.config.po.TableField) TableField(com.baomidou.mybatisplus.generator.config.po.TableField) TableCommentParse(com.chao.cloud.common.extra.mybatis.generator.parse.TableCommentParse)

Example 3 with TableField

use of com.baomidou.mybatisplus.generator.config.po.TableField in project code-generator-maven-plugin by mengweijin.

the class DefaultAutoGenerator method getIdField.

private static IdField getIdField(TableInfo tableInfo) {
    TableField tableField = tableInfo.getFields().stream().filter(TableField::isKeyFlag).findFirst().orElse(null);
    IdField idField = new IdField();
    if (tableField != null) {
        idField.setColumnName(tableField.getName());
        idField.setPropertyName(tableField.getPropertyName());
        idField.setPropertyType(tableField.getColumnType().getType());
    }
    return idField;
}
Also used : IdField(com.github.mengweijin.generator.entity.IdField) TableField(com.baomidou.mybatisplus.generator.config.po.TableField)

Aggregations

TableField (com.baomidou.mybatisplus.generator.config.po.TableField)3 InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)2 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)2 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)2 BeanUtil (cn.hutool.core.bean.BeanUtil)1 FileUtil (cn.hutool.core.io.FileUtil)1 MapUtil (cn.hutool.core.map.MapUtil)1 ReflectUtil (cn.hutool.core.util.ReflectUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 CollectionUtils (com.baomidou.mybatisplus.core.toolkit.CollectionUtils)1 ConstVal (com.baomidou.mybatisplus.generator.config.ConstVal)1 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)1 DbColumnType (com.baomidou.mybatisplus.generator.config.rules.DbColumnType)1 FileType (com.baomidou.mybatisplus.generator.config.rules.FileType)1 AbstractTemplateEngine (com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine)1 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)1 BusinessException (com.chao.cloud.common.exception.BusinessException)1 EnableMybatisGenerator (com.chao.cloud.common.extra.mybatis.annotation.EnableMybatisGenerator)1 KeyWordConstant (com.chao.cloud.common.extra.mybatis.constant.KeyWordConstant)1 TableCommentParse (com.chao.cloud.common.extra.mybatis.generator.parse.TableCommentParse)1