• IDEA 配置mybatis生成代码


    1、引入jar

    2、配置mybaits.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

    <generatorConfiguration>
    <context id="DB2Tables" targetRuntime="MyBatis3">
    <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
    <property name="searchString" value="Example$" />
    <property name="replaceString" value="Criteria" />
    </plugin>
    <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
    <commentGenerator>
    <property name="suppressDate" value="true" />
    <property name="suppressAllComments" value="true" />
    </commentGenerator>

    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    connectionURL="jdbc:mysql://172.16.8.61:3306/tbcmsdb"
    userId="tbcmsowner" password="ce#%^MZY45uauoMh">
    </jdbcConnection>

    <javaModelGenerator targetPackage="com.bonc.cms.dao.mapper.bo"
    targetProject="unicom-cms-coresrcmainjava">
    <property name="enableSubPackages" value="true" />
    <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <sqlMapGenerator targetPackage="mybatis.mapper.com"
    targetProject="unicom-cms-coresrcmain esources">
    <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER"
    targetPackage="com.bonc.cms.dao.mapper.interfaces"
    targetProject="unicom-cms-coresrcmainjava">
    <property name="enableSubPackages" value="true" />
    </javaClientGenerator>


    <table tableName="test" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="false" />
    </context>
    </generatorConfiguration>

    3、java主函数
    package com.bonc.cms.code;

    import org.mybatis.generator.api.MyBatisGenerator;
    import org.mybatis.generator.config.Configuration;
    import org.mybatis.generator.config.xml.ConfigurationParser;
    import org.mybatis.generator.internal.DefaultShellCallback;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;

    /**
    * 生成mybatis文件
    *
    * @author mayt
    */
    public class CodeGeneratorMain {

    private static final Logger LOGGER = LoggerFactory.getLogger(CodeGeneratorMain.class);

    /** 配置文件路径 */
    public static final String CFG_FILE_PATH = CodeGeneratorMain.class
    .getResource("/mybatis/generator/generatorConfig.xml").getFile();

    public static void main(String[] args) throws Exception {
    List<String> warnings = new ArrayList<>();
    boolean overwrite = true;
    File configFile = new File(CFG_FILE_PATH);
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = null;
    config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = null;
    myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
    for (String warning : warnings) {
    System.out.println(warning);
    }
    LOGGER.info("gen end");
    }
    }

  • 相关阅读:
    一个简单的投票功能
    快速删除.svn文件夹
    屏蔽关键字
    批量过滤POST GET数据
    JS识别ios & 安卓
    HashMap
    GC
    类与类之间的关系
    玩转大数据之Apache Pig如何与Apache Lucene集成
    大话设计模式-简单工厂模式
  • 原文地址:https://www.cnblogs.com/guanhao0114/p/14071952.html
Copyright © 2020-2023  润新知