MyBatis

使用MyBatisGenerator

使用MyBatis Generator可以自动生成Mapper接口,Mapper xml, Entity类。一般来说,我们要创建2个文件,一个xml,一个java, xml是核心,指定了数据源,插件,生成文件的路径。这两个文件在项目中的位置如下:

generate_renshui.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>
    <!-- 驱动程序 -->
    <classPathEntry
        location="/Users/renshui/.gradle/caches/modules-2/files-2.1/mysql/mysql-connector-java/5.1.24/a7d599d73b86a928303909b9be7573a4ea0ed28e/mysql-connector-java-5.1.24.jar" />

    <context id="context2" targetRuntime="MyBatis3">


        <!-- 生成entity类中的equals(),hashcode()方法 -->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
        <!-- entity类自动实现Serializable接口 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <!-- 生成的Mapper接口支持分页 -->
        <plugin type="org.mybatis.generator.plugins.PaginationPlugin4MySQL"></plugin>
        <!-- 用新生成的mapper覆盖原来的 -->
        <plugin type="org.mybatis.generator.plugins.GeneratedXmlFileOverwritePlugin"></plugin>
        <!-- 自动给生成的Entity类加注释 -->
        <plugin type="org.mybatis.generator.plugins.GenerateDBCommentPlugin"></plugin>
        <!-- 生成的Mapper接口支持selct for update加锁 -->
        <plugin type="org.mybatis.generator.plugins.GenerateMySQLPessimisticLockPlugin"></plugin>

        <commentGenerator >
            <!-- 是否取消注释 -->
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>




        <!-- 数据库链接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://192.168.1.13:3306/vbam" userId="root"
            password="xxxxxxx" />

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- java entity生成位置 -->
        <javaModelGenerator targetPackage="com.cana.earlywarning.dao.po"
            targetProject="/Users/renshui/cana/early-warning/early-warning-dao/src/main/java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- mapper xml配置文件生成位置 -->
        <sqlMapGenerator targetPackage="mybatis.mapper.gen"
            targetProject="/Users/renshui/cana/early-warning/early-warning-dao/src/main/resources">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- mapper 接口生成位置 -->
        <javaClientGenerator targetPackage="com.cana.earlywarning.dao.mapper.gen"
            targetProject="/Users/renshui/cana/early-warning/early-warning-dao/src/main/java"
            type="XMLMAPPER">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 表以及关系配置 -->

        <table tableName="early_warning_customer" domainObjectName="EarlywarningCustomer"></table>  
        <table tableName="early_warning_event" domainObjectName="EarlywarningEvent"></table>  
        <table tableName="early_warning_event_review" domainObjectName="EarlywarningEventReview"></table>  
        <table tableName="early_warning_level_change_history" domainObjectName="EarlywarningLevelChangeHistory"></table>  
        <table tableName="early_warning_system_event_generate_record" domainObjectName="EarlywarningSystemEventGenerateRecord"></table>  

    </context>
</generatorConfiguration>

Generate_renshui.java 的内容如下:


   public class Generate_renshui {

    public static void main(String[] args) {
        List<String> warnings = new ArrayList<String>();
        try {
            boolean overwrite = true;
            File configFile = new File("./src/test/java/mybatis/generator/generate_renshui.xml");
            if (configFile.exists()) {
                ConfigurationParser cp = new ConfigurationParser(warnings);
                Configuration config = cp.parseConfiguration(configFile);
                for (Context context : config.getContexts()) {
                    context.addProperty("overwrite", String.valueOf(overwrite));
                }
                DefaultShellCallback callback = new DefaultShellCallback(overwrite);
                MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
                myBatisGenerator.generate(null);
                System.out.println("Success");
            } else
                System.out.println("沒有找到配置文件");// 配置文件中的项目地址和包引用地址要用绝对地址,否则不能生成
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

results matching ""

    No results matching ""