mybatis generator逆向工程生成一对多映射

来源:互联网 发布:vb开源游戏引擎 编辑:程序博客网 时间:2024/06/05 10:46

最近需要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 尽量按照驼峰命名法标准命名。

最后还是感谢下插件作者的无私奉献~ 撒花~

原创粉丝点击