java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatisplus自动生成代码tinyint(1)自动转为Boolean

mybatis plus自动生成代码tinyint(1)自动转换为Boolean的问题及解决

作者:hank009

这篇文章主要介绍了mybatis plus自动生成代码tinyint(1)自动转换为Boolean的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

mybatis plus自动生成代码tinyint(1)自动转换为Boolean

说明下为什么会自动转换为Boolean,是因为mybtisplus提供的默认的mysql类型转换器MySqlTypeConvert上特别写了这一段:

对症下药的解决办法是重写一个

在注入到配置里:

/**
 * 自定义类型转换
 */
class MySqlTypeConvertCustom extends MySqlTypeConvert implements ITypeConvert{
    @Override
    public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
        String t = fieldType.toLowerCase();
        if (t.contains("tinyint(1)")) {
            return DbColumnType.INTEGER;
        }
        return super.processTypeConvert(globalConfig, fieldType);
    }
}

代码生成器完整代码

package com.wjj.application;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.Arrays;
/**
 * @author hank
 */
public class TestGen {
    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir("D://");
        gc.setFileOverride(true);
        //不需要ActiveRecord特性的请改为false
        gc.setActiveRecord(true);
        // XML 二级缓存;
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columnList
        gc.setBaseColumnList(true);
        gc.setAuthor("mybatisPlus");
        gc.setDateType(DateType.ONLY_DATE);
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://xxxx/xxxxxx?tinyInt1isBit=false&useUnicode=true&characterEncoding=UTF-8&generateSimpleParameterMetadata=true");
        dsc.setUsername("xxxx");
        dsc.setPassword("xxxx");
        dsc.setTypeConvert(new MySqlTypeConvertCustom());
        mpg.setDataSource(dsc);
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix("saas_");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude("saas_medical_history", "saas_medical_history_describe", "saas_medical_history_attachment",
                "saas_medical_history_diagnose", "saas_medical_history_prescription", "saas_medical_history_prescription_subjoin",
                "saas_medical_history_prescription_chinese_list", "saas_medical_history_prescription_generic_list", "saas_medical_history_feature");
        // 指定逻辑删除字段
        strategy.setLogicDeleteFieldName("is_deleted");
        mpg.setStrategy(strategy);
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.wjj.application");
        String packageModule = ".medicalhistory";
        pc.setController("controller"+packageModule);
        pc.setService("service"+packageModule);
        pc.setServiceImpl("service" + packageModule + ".impl");
        pc.setMapper("mapper" + packageModule);
        pc.setEntity("entity" + packageModule);
        mpg.setPackageInfo(pc);
        mpg.execute();
    }
}
/**
 * 自定义类型转换
 */
class MySqlTypeConvertCustom extends MySqlTypeConvert implements ITypeConvert{
    @Override
    public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
        String t = fieldType.toLowerCase();
        if (t.contains("tinyint(1)")) {
            return DbColumnType.INTEGER;
        }
        return super.processTypeConvert(globalConfig, fieldType);
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文