使用Java的MyBatis生成器无法正常工作

时间:2015-02-04 19:42:28

标签: java mybatis mybatis-generator

MyBatis生成器不想为我生成代码。 我使用Eclipse IDE。 起初我怀疑是 targetProject 属性,但我为此指定了当前文件夹,实际上也没有结果。

我在命令行中使用相同的xml conf工作正常。

我认为我必须在我的Java代码中提供 outputFolder 一些方法,我尝试了 prop.setProperty(“generated.source.dir”,System.getProperty(“user”)。 dir“)); 但它不起作用。

因此,任何建议都将受到赞赏。

这是我生成的java代码

public class GeneratorMyBatis {

    public static void main(String args[]) {
        try {
            generate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void generate() throws Exception {
        System.out.println("Working Directory = " + System.getProperty("user.dir"));
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src//main//resources//mybatis//generator//config//generatorConfig.xml");
        Properties prop = new Properties();
        prop.setProperty("generated.source.dir", System.getProperty("user.dir"));
        ConfigurationParser cp = new ConfigurationParser(prop, warnings);
        Configuration config = cp.parseConfiguration(configFile);
        config.validate();
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        ProgressCallback progress = new VerboseProgressCallback();
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(progress);
    }
}

我的配置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="/Program Files/IBM/SQLLIB/java/db2java.zip" />
    -->

    <context id="MyTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/my_db"
                        userId="root"
                        password="root">
        </jdbcConnection>

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

        <javaModelGenerator targetPackage="model" targetProject="\">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="xml"  targetProject="\">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="dao"  targetProject="\">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table schema="my_db" tableName="assignment" domainObjectName="Assignment" >
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="id" sqlStatement="MySql" identity="true" />
        </table>
    </context>
</generatorConfiguration>

我收到了这条日志消息

Introspecting table my_db.assignment
Generating Example class for table assignment
Generating Record class for table assignment
Generating Mapper Interface for table assignment
Generating SQL Map for table assignment
Saving file AssignmentMapper.xml
Saving file AssignmentExample.java
Saving file Assignment.java
Saving file AssignmentMapper.java

1 个答案:

答案 0 :(得分:1)

总而言之,我找到了解决问题的方法。

MyBatis会生成所有必要的人员,但会将其放入磁盘根目录,例如D:\。

要在java targetProject targetPackage 上进行配置,您必须将它们设置为Configuration对象。像这样:

Configuration config = cp.parseConfiguration(configFile);
config.getContexts().get(0).getJavaModelGeneratorConfiguration().setTargetProject(TARGET_PROJECT_PATH);
config.getContexts().get(0).getJavaModelGeneratorConfiguration().setTargetPackage(MODEL_TARGET_PACKAGE);
config.getContexts().get(0).getSqlMapGeneratorConfiguration().setTargetProject(TARGET_PROJECT_PATH);
config.getContexts().get(0).getSqlMapGeneratorConfiguration().setTargetPackage(XML_MAPPER_TARGET_PACKAGE);