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

最近需要mybatis做映射一对多表关系,之前遇到这种情况我都是手动写一个新的。但是最近发现有个大神写了个可以用逆向工程生成一对多、一对一等复杂表关系的插件。在此感谢下大神~~
附上插件原文:http://blog.csdn.net/bandaotixiruiqiang/article/details/72478361

环境:mybatis+springboot+maven+jdk1.8

maven配置:

我直接下载了原博主的jar包,引入maven依赖。

dependency依赖:

    <dependency>
        <groupId>org.mybatis.generatorgroupId>
        <artifactId>mybatis-generator-coreartifactId>
        <version>1.3.6version>
        <scope>systemscope>
        <systemPath>${project.basedir}/lib/mybatis.jarsystemPath>
    dependency>
    <dependency>
        <groupId>org.mybatis.generatorgroupId>
        <artifactId>mybatis-generator-maven-pluginartifactId>
        <version>1.3.5version>
    dependency>

plugin:

<plugin>
    <groupId>org.mybatis.generatorgroupId>
    <artifactId>mybatis-generator-maven-pluginartifactId>
    <version>1.3.5version>
    <dependencies>
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.6version>
            <scope>systemscope>
            <systemPath>${project.basedir}/lib/mybatis.jarsystemPath>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.34version>
        dependency>
        <dependency>
            <groupId>com.xxggroupId>
            <artifactId>mybatis-generator-pluginartifactId>
            <version>1.0.0version>
        dependency>
    dependencies>
    <configuration>
        <verbose>falseverbose>
        <overwrite>trueoverwrite>
    configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifactsid>
            <goals>
                <goal>generategoal>
            goals>
        execution>
    executions>
plugin>

重点在 ${project.basedir}/lib/mybatis.jar 这句的配置。
mybatis.jar为插件jar包。下图为在项目中的位置。
mybatis generator逆向工程生成一对多映射_第1张图片

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>

        
        <sqlMapGenerator targetPackage="com.test.mapper" targetProject="src/main/resources" />

        
        <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:

  id="BaseResultMap" type="com.test.model.aTest">
    <id column="a_id" jdbcType="INTEGER" property="aId" />
    <result column="a_name" jdbcType="VARCHAR" property="aName" />
    "a_id" property="bTests" select="getbTests" />
  

获取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

 /**
     * 自增主键
     * 表字段 : t_a.a_id
     */
    private Integer aId;

    /**
     * 不知道反正测试用的
     * 表字段 : t_a.a_name
     */
    private String aName;

    private List bTests;

蓝后就可以开心的使用了~~

敲黑板!注意两点:

1.子表和父表必须要同时生成,这样才能获取到子表的配置,进行表关系的映射。
2.domainObjectName、mapperName 尽量按照驼峰命名法标准命名。

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

你可能感兴趣的:(java)