gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:57:26 25 4
gpt4 key购买 nike

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

最佳答案

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

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);

关于java - 使用 Java 的 MyBatis 生成器无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28330015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com