Java 的 MyBatis 生成器不工作
MyBatis generator with Java is not working
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 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);
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 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);