0

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
4

1 回答 1

1

总而言之,我发现了如何解决这个问题。

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

要在 java targetProjecttargetPackage上进行配置,您必须将它们设置到 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);
于 2015-02-05T10:06:54.860 回答