mybatis自动生成mapper.xml的方法

来源:互联网 发布:源代码如何变成软件 编辑:程序博客网 时间:2024/05/05 11:14

1. 首先添加maven插件

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/test/resources/config/generatorConfig.xml</configurationFile>  //这是与数据库的配置地址
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

2.其次填写与数据库连接配置文件<?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="/Users/seabook.liu/.m2/repository/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar" />  
  <context id="context1" >
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="" />(数据库连接)
    <javaModelGenerator targetPackage="com.dianping.poi.updateflow.biz.model" targetProject="src/main/java">
        <property name="enableSubPackages" value="true" />
    </javaModelGenerator>
    <sqlMapGenerator targetPackage="config.sqlmap.updateflow"  targetProject="src/main/resources" >(生成的mapper文件)
        <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <javaClientGenerator targetPackage="com.dianping.poi.updateflow.biz.dao" targetProject="src/main/java" type="XMLMAPPER" >(生成的dao层接口)
        <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <table schema="" tableName="POI_Shop"(数据库表名) domainObjectName="PoiShop"(生产的实体对象)
        enableCountByExample="true"
        enableUpdateByExample="true"  
        enableDeleteByExample="true"
        enableSelectByExample="true"
        selectByExampleQueryId="true"
        
        >
    </table>
  </context>
</generatorConfiguration>


3.利用maven命令自动生成

mybatis-generator:generate


4.最后能生成如下文件

只是实例一个

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dianping.poi.updateflow.biz.dao.ProcInfoDao" >
  <resultMap id="BaseResultMap" type="com.dianping.poi.updateflow.biz.model.ProcInfo" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    <id column="proc_id" property="procId" jdbcType="INTEGER" />
    <result column="flow_id" property="flowId" jdbcType="INTEGER" />
    <result column="entry_id" property="entryId" jdbcType="INTEGER" />
    <result column="proc_type" property="procType" jdbcType="VARCHAR" />
    <result column="proc_status" property="procStatus" jdbcType="TINYINT" />
    <result column="editer_id" property="editerId" jdbcType="VARCHAR" />
    <result column="result" property="result" jdbcType="VARCHAR" />
    <result column="explain" property="explain" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="ext" property="ext" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    proc_id, flow_id, entry_id, proc_type, proc_status, editer_id, result, explain, create_time,
    update_time, ext
  </sql>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.dianping.poi.updateflow.biz.model.ProcInfoExample" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    select
    <if test="distinct" >
      distinct
    </if>
    'true' as QUERYID,
    <include refid="Base_Column_List" />
    from POI_ProcInfo
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    select
    <include refid="Base_Column_List" />
    from POI_ProcInfo
    where proc_id = #{procId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    delete from POI_ProcInfo
    where proc_id = #{procId,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.dianping.poi.updateflow.biz.model.ProcInfoExample" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    delete from POI_ProcInfo
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.dianping.poi.updateflow.biz.model.ProcInfo" useGeneratedKeys="true" keyProperty="procId">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    insert into POI_ProcInfo (proc_id, flow_id, entry_id,
      proc_type, proc_status, editer_id,
      result, explain, create_time,
      update_time, ext)
    values (#{procId,jdbcType=INTEGER}, #{flowId,jdbcType=INTEGER}, #{entryId,jdbcType=INTEGER},
      #{procType,jdbcType=VARCHAR}, #{procStatus,jdbcType=TINYINT}, #{editerId,jdbcType=VARCHAR},
      #{result,jdbcType=VARCHAR}, #{explain,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
      #{updateTime,jdbcType=TIMESTAMP}, #{ext,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.dianping.poi.updateflow.biz.model.ProcInfo" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    insert into POI_ProcInfo
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="procId != null" >
        proc_id,
      </if>
      <if test="flowId != null" >
        flow_id,
      </if>
      <if test="entryId != null" >
        entry_id,
      </if>
      <if test="procType != null" >
        proc_type,
      </if>
      <if test="procStatus != null" >
        proc_status,
      </if>
      <if test="editerId != null" >
        editer_id,
      </if>
      <if test="result != null" >
        result,
      </if>
      <if test="explain != null" >
        explain,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
      <if test="ext != null" >
        ext,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="procId != null" >
        #{procId,jdbcType=INTEGER},
      </if>
      <if test="flowId != null" >
        #{flowId,jdbcType=INTEGER},
      </if>
      <if test="entryId != null" >
        #{entryId,jdbcType=INTEGER},
      </if>
      <if test="procType != null" >
        #{procType,jdbcType=VARCHAR},
      </if>
      <if test="procStatus != null" >
        #{procStatus,jdbcType=TINYINT},
      </if>
      <if test="editerId != null" >
        #{editerId,jdbcType=VARCHAR},
      </if>
      <if test="result != null" >
        #{result,jdbcType=VARCHAR},
      </if>
      <if test="explain != null" >
        #{explain,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="ext != null" >
        #{ext,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.dianping.poi.updateflow.biz.model.ProcInfoExample" resultType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    select count(*) from POI_ProcInfo
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    update POI_ProcInfo
    <set >
      <if test="record.procId != null" >
        proc_id = #{record.procId,jdbcType=INTEGER},
      </if>
      <if test="record.flowId != null" >
        flow_id = #{record.flowId,jdbcType=INTEGER},
      </if>
      <if test="record.entryId != null" >
        entry_id = #{record.entryId,jdbcType=INTEGER},
      </if>
      <if test="record.procType != null" >
        proc_type = #{record.procType,jdbcType=VARCHAR},
      </if>
      <if test="record.procStatus != null" >
        proc_status = #{record.procStatus,jdbcType=TINYINT},
      </if>
      <if test="record.editerId != null" >
        editer_id = #{record.editerId,jdbcType=VARCHAR},
      </if>
      <if test="record.result != null" >
        result = #{record.result,jdbcType=VARCHAR},
      </if>
      <if test="record.explain != null" >
        explain = #{record.explain,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null" >
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateTime != null" >
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.ext != null" >
        ext = #{record.ext,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    update POI_ProcInfo
    set proc_id = #{record.procId,jdbcType=INTEGER},
      flow_id = #{record.flowId,jdbcType=INTEGER},
      entry_id = #{record.entryId,jdbcType=INTEGER},
      proc_type = #{record.procType,jdbcType=VARCHAR},
      proc_status = #{record.procStatus,jdbcType=TINYINT},
      editer_id = #{record.editerId,jdbcType=VARCHAR},
      result = #{record.result,jdbcType=VARCHAR},
      explain = #{record.explain,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      ext = #{record.ext,jdbcType=VARCHAR}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.dianping.poi.updateflow.biz.model.ProcInfo" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    update POI_ProcInfo
    <set >
      <if test="flowId != null" >
        flow_id = #{flowId,jdbcType=INTEGER},
      </if>
      <if test="entryId != null" >
        entry_id = #{entryId,jdbcType=INTEGER},
      </if>
      <if test="procType != null" >
        proc_type = #{procType,jdbcType=VARCHAR},
      </if>
      <if test="procStatus != null" >
        proc_status = #{procStatus,jdbcType=TINYINT},
      </if>
      <if test="editerId != null" >
        editer_id = #{editerId,jdbcType=VARCHAR},
      </if>
      <if test="result != null" >
        result = #{result,jdbcType=VARCHAR},
      </if>
      <if test="explain != null" >
        explain = #{explain,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="ext != null" >
        ext = #{ext,jdbcType=VARCHAR},
      </if>
    </set>
    where proc_id = #{procId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.dianping.poi.updateflow.biz.model.ProcInfo" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 28 16:24:30 CST 2015.
    -->
    update POI_ProcInfo
    set flow_id = #{flowId,jdbcType=INTEGER},
      entry_id = #{entryId,jdbcType=INTEGER},
      proc_type = #{procType,jdbcType=VARCHAR},
      proc_status = #{procStatus,jdbcType=TINYINT},
      editer_id = #{editerId,jdbcType=VARCHAR},
      result = #{result,jdbcType=VARCHAR},
      explain = #{explain,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      ext = #{ext,jdbcType=VARCHAR}
    where proc_id = #{procId,jdbcType=INTEGER}
  </update>
</mapper>



0 0
原创粉丝点击