Reputation: 735
MyBatis generator doesn't want to generate the code for me. I use an Eclipse IDE for that. At first I suspect targetProject property, but I specify the current folder for that and actually there is no result as well.
I work OK with the same xml conf from the command-line.
I think that I have to provide into my Java code the outputFolder some how, I tried prop.setProperty("generated.source.dir", System.getProperty("user.dir")); but it doesn't work.
So, any suggestions will be appreciated.
Here is my java code for generation
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);
}
}
My configuration xml file
<?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>
I got this log messages
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
Upvotes: 0
Views: 4174
Reputation: 735
All in all I found out the how to fix the issue.
MyBatis generates the all necessary staff, but puts it into the disk root, like D:\ for example.
To configure on java targetProject and targetPackage, you have to set them into the Configuration object. Something like this:
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);
Upvotes: 1