1、問題java
基於最新版的mybatis-generator-core:1.3.6生成的代碼中,model和example在一個目錄下,以下圖,其實他們並非一類bean。git
dto和example生成在一個目錄的緣由是它們的生成目錄都使用了github
<javaModelGenerator targetPackage="com.iwill.model" targetProject="src/main/java"> <!-- 實體類 --> <property name="enableSubPackages" value="true" /> <property name="" value=""/> </javaModelGenerator>
這裏的targetPackage。能夠依照源碼來找到答案。sql
2、實現mybatis
咱們要實現能夠靈活指定example生成目錄的目標,須要擴展mybatis-generator-core:1.3.6maven
一、擴展dtd文件spa
<!ELEMENT context (property*, plugin*, commentGenerator?, (connectionFactory | jdbcConnection), javaTypeResolver?,javaModelGenerator,javaExampleGenerator?, sqlMapGenerator?, javaClientGenerator?, table+)> <!ATTLIST context id ID #REQUIRED defaultModelType CDATA #IMPLIED targetRuntime CDATA #IMPLIED introspectedColumnImpl CDATA #IMPLIED>
增長在context節點下增長javaExampleGenerator子節點,而且javaExampleGenerator的定義以下:code
<!ELEMENT javaExampleGenerator (property*)> <!ATTLIST javaExampleGenerator targetPackage CDATA #REQUIRED targetProject CDATA #REQUIRED>
二、修改源代碼xml
具體不詳細描述,主要有如下修改點:blog
a、解析javaExampleGenerator
b、設置JavaExampleGeneratorConfiguration的值
c、設置example的生成目錄
d、配置全部用到該example的地方對應的import package
三、上傳修改好後的jar
修改pom,指定版本爲1.3.6.1。
<artifactId>mybatis-generator-core</artifactId> <name>MyBatis Generator</name> <version>1.3.6.1</version>
生成並上傳對應的jar到本地:mvn package install -Dmaven.test.skip=true
四、引入jar包
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6.1</version> <scope>system</scope> <systemPath>/Users/iwill/.m2/repository/org/mybatis/generator/mybatis-generator-core/1.3.6.1/mybatis-generator-core-1.3.6.1.jar</systemPath> </dependency>
五、修改generatorConfig.xml
在1的基礎上,這樣就能夠在generatorConfig.xml中添加javaExampleGenerator節點。
<javaExampleGenerator targetPackage="com.iwill.example" targetProject="src/main/java"> <!-- example --> <property name="enableSubPackages" value="true" /> </javaExampleGenerator>
注:
在提交給mybatis-generator官方的代碼中,通過author的建議,實現方式採用properties。配置以下:
<javaModelGenerator targetPackage="com.iwill.model" targetProject="src/main/java"> <!-- 實體類 --> <property name="enableSubPackages" value="true" /> <property name="exampleTargetPackage" value="com.iwill.example"/> </javaModelGenerator>
六、運行命令生成代碼
運行mvn mybatis-generator:generate -e生成代碼
這樣就能夠了。
3、擴展
若是不配置javaExampleGenerator節點的話,那麼model和example仍是生成在一個目錄。
但願mybatis-generator-core的官方能夠早日支持上述功能,能夠給開發者更多選項來支持更多配置。
附:對應的樣例項目的地址:https://github.com/yangjianzhou/mybatis-generator.git
對應的generator項目的地址:https://github.com/yangjianzhou/generator
對應分支爲:mybatis-generator-1.3.6.1