最近需要mybatis做映射一對多表關系,之前遇到這種情況我都是手動寫一個新的。但是最近發現有個大神寫了個可以用逆向工程生成一對多、一對一等複雜表關系的插件。在此感謝下大神~~
附上插件原文:http://blog.csdn.net/bandaotixiruiqiang/article/details/72478361
環境:mybatis+springboot+maven+jdk1.8
maven配置:
我直接下載下傳了原部落客的jar包,引入maven依賴。
dependency依賴:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/mybatis.jar</systemPath>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
</dependency>
plugin:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/mybatis.jar</systemPath>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>com.xxg</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<configuration>
<verbose>false</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
重點在 ${project.basedir}/lib/mybatis.jar 這句的配置。
mybatis.jar為插件jar包。下圖為在項目中的位置。

generatorConfig.xml配置:
<generatorConfiguration>
<properties resource="application.properties" />
<classPathEntry location="${jdbc.driver-lib}" />
<context id="MysqlContext" targetRuntime="MyBatis3"
defaultModelType="flat">
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<!-- 生成一對一配置 -->
<plugin type="cc.bandaotixi.plugins.OneToOnePlugin" />
<!-- 生成一對多配置 -->
<plugin type="cc.bandaotixi.plugins.OneToManyPlugin" />
<!-- 生成批量配置 -->
<plugin type="cc.bandaotixi.plugins.BatchInsertPlugin" />
<plugin type="cc.bandaotixi.plugins.BatchUpdatePlugin" />
<!-- 生成注釋配置 -->
<commentGenerator type="cc.bandaotixi.plugins.BDTCommentGenerator">
<property name="javaFileEncoding" value="GB2312"/>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 資料庫連接配接配置 -->
<jdbcConnection driverClass="${generator.datasource.driver-class-name}"
connectionURL="jdbc:mysql://localhost:3306/office" userId="root" password="123">
</jdbcConnection>
<!-- 實體類配置 -->
<javaModelGenerator targetPackage="com.test.model"
targetProject="src/main/java">
<property name="trimStrings" value="true" />
<property name="enableSubPackages" value="true" />
</javaModelGenerator>
<!-- mapper.xml配置 -->
<sqlMapGenerator targetPackage="com.test.mapper" targetProject="src/main/resources" />
<!-- mapper.java配置 -->
<javaClientGenerator type="XMLMAPPER"
implementationPackage="com.test.mapper"
targetPackage="com.test.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaClientGenerator>
<!-- 父表配置-->
<table tableName="t_a" mapperName="aTestMapper" domainObjectName="aTest">
<generatedKey column="a_id" sqlStatement="MySql" identity="true"/>
<oneToMany mappingTable="t_b" column="a_id" joinColumn="b_parent_id"/>
</table>
<!-- 子表配置-->
<table tableName="t_b" domainObjectName="bTest">
<generatedKey column="b_id" sqlStatement="MySql" identity="true" />
</table>
</context>
</generatorConfiguration>
其中,t_a表為父表;t_b為子表,用b_parent_id字段關聯t_a表的a_id字段,關系為t_b(多)對t_a(一)。
生成結果
xml檔案:
resultMap 加入了子表的的collection 集合,調用a表mapper中的select方法時會傳回b表資料list:
<resultMap id="BaseResultMap" type="com.test.model.aTest">
<id column="a_id" jdbcType="INTEGER" property="aId" />
<result column="a_name" jdbcType="VARCHAR" property="aName" />
<collection column="a_id" property="bTests" select="getbTests" />
</resultMap>
擷取b表資料:
<select id="getbTests" resultMap="com.test.mapper.bTestMapper.BaseResultMap">
select b_id,b_parent_id from t_b where b_parent_id=#{aId}
</select>
批量修改、插入:
<insert id="insertBatchSelective" parameterType="java.util.List">
insert into t_a
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="list[0].aId!=null">
a_id,
</if>
<if test="list[0].aName!=null">
a_name,
</if>
</trim>
values
<foreach collection="list" index="index" item="item" separator=",">
<trim prefix=" (" suffix=")" suffixOverrides=",">
<if test="item.aId!=null">
#{item.aId,jdbcType=INTEGER},
</if>
<if test="item.aName!=null">
#{item.aName,jdbcType=VARCHAR},
</if>
</trim>
</foreach>
</insert>
<update id="updateBatchByPrimaryKeySelective" parameterType="java.util.List">
<foreach collection="list" index="index" item="item" separator=";">
update t_a
<set>
<if test="item.aName!=null">
a_name=#{item.aName,jdbcType=VARCHAR},
</if>
</set>
where
a_id = #{item.aId,jdbcType=INTEGER}
</foreach>
</update>
</mapper>
model檔案:
多了
List<bTest>
/**
* 自增主鍵
* 表字段 : t_a.a_id
*/
private Integer aId;
/**
* 不知道反正測試用的
* 表字段 : t_a.a_name
*/
private String aName;
private List<bTest> bTests;
藍後就可以開心的使用了~~
敲黑闆!注意兩點:
1.子表和父表必須要同時生成,這樣才能擷取到子表的配置,進行表關系的映射。
2.domainObjectName、mapperName 盡量按照駝峰命名法标準命名。
最後還是感謝下插件作者的無私奉獻~ 撒花~