idea自動生成mybatis-xml文件


idea自動生成mybatis文件

1、在pom.xml中添加org.mybatis.generator插件

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.2</version>

        </plugin>
    </plugins>
</build>

這里寫圖片描述
2、添加generator運行選項

這里寫圖片描述

在Command line中添加如下代碼

mybatis-generator:generate -e

3、在resources下建立generatorConfig.xml 內容如下

<?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="D:\a_javaConfiig\maven\Maven_Repository\mysql\mysql-connector-java\5.1.29\mysql-connector-java-5.1.29.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--數據庫鏈接URL,用戶名、密碼 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/jazkwl?useUnicode=true"
                        userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.zzbeilei.dsly.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="com.zzbeilei.dsly.dao" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zzbeilei.dsly.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名-->
        <table tableName="gs_aboutu" domainObjectName="GsAboutu" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="gs_account" domainObjectName="GsAccount" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="gs_yundlb" domainObjectName="GsYundlb" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

4 自動生成的xml文件結構如下

<?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.zzbeilei.dsly.dao.GsAccountMapper" >
  <resultMap id="BaseResultMap" type="com.zzbeilei.dsly.pojo.GsAccount" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="desc" property="desc" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="INTEGER" />
    <result column="chakan" property="chakan" jdbcType="INTEGER" />
    <result column="add_time" property="addTime" jdbcType="VARCHAR" />
    <result column="add_id" property="addId" jdbcType="INTEGER" />
    <result column="yund_id" property="yundId" jdbcType="INTEGER" />
    <result column="xing" property="xing" jdbcType="INTEGER" />
    <result column="tit" property="tit" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, user_id, desc, status, chakan, add_time, add_id, yund_id, xing, tit
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from gs_account
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from gs_account
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zzbeilei.dsly.pojo.GsAccount" >
    insert into gs_account (id, user_id, desc, 
      status, chakan, add_time, 
      add_id, yund_id, xing, 
      tit)
    values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{desc,jdbcType=VARCHAR}, 
      #{status,jdbcType=INTEGER}, #{chakan,jdbcType=INTEGER}, #{addTime,jdbcType=VARCHAR}, 
      #{addId,jdbcType=INTEGER}, #{yundId,jdbcType=INTEGER}, #{xing,jdbcType=INTEGER}, 
      #{tit,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zzbeilei.dsly.pojo.GsAccount" >
    insert into gs_account
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="desc != null" >
        desc,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="chakan != null" >
        chakan,
      </if>
      <if test="addTime != null" >
        add_time,
      </if>
      <if test="addId != null" >
        add_id,
      </if>
      <if test="yundId != null" >
        yund_id,
      </if>
      <if test="xing != null" >
        xing,
      </if>
      <if test="tit != null" >
        tit,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="desc != null" >
        #{desc,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        #{status,jdbcType=INTEGER},
      </if>
      <if test="chakan != null" >
        #{chakan,jdbcType=INTEGER},
      </if>
      <if test="addTime != null" >
        #{addTime,jdbcType=VARCHAR},
      </if>
      <if test="addId != null" >
        #{addId,jdbcType=INTEGER},
      </if>
      <if test="yundId != null" >
        #{yundId,jdbcType=INTEGER},
      </if>
      <if test="xing != null" >
        #{xing,jdbcType=INTEGER},
      </if>
      <if test="tit != null" >
        #{tit,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zzbeilei.dsly.pojo.GsAccount" >
    update gs_account
    <set >
      <if test="userId != null" >
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="desc != null" >
        desc = #{desc,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="chakan != null" >
        chakan = #{chakan,jdbcType=INTEGER},
      </if>
      <if test="addTime != null" >
        add_time = #{addTime,jdbcType=VARCHAR},
      </if>
      <if test="addId != null" >
        add_id = #{addId,jdbcType=INTEGER},
      </if>
      <if test="yundId != null" >
        yund_id = #{yundId,jdbcType=INTEGER},
      </if>
      <if test="xing != null" >
        xing = #{xing,jdbcType=INTEGER},
      </if>
      <if test="tit != null" >
        tit = #{tit,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zzbeilei.dsly.pojo.GsAccount" >
    update gs_account
    set user_id = #{userId,jdbcType=INTEGER},
      desc = #{desc,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER},
      chakan = #{chakan,jdbcType=INTEGER},
      add_time = #{addTime,jdbcType=VARCHAR},
      add_id = #{addId,jdbcType=INTEGER},
      yund_id = #{yundId,jdbcType=INTEGER},
      xing = #{xing,jdbcType=INTEGER},
      tit = #{tit,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM