Mybatis代碼產生器配置(mybatis逆向工程)_mybatis

來源:互聯網
上載者:User

1.pom.xml,添加mybatis外掛程式

<plugin>                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.3.2</version>                <configuration>                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>                    <verbose>true</verbose>                    <overwrite>true</overwrite>                </configuration>            </plugin>

2.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:/.m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar"/>    <context id="DB2Tables"    targetRuntime="MyBatis3">        <commentGenerator>            <property name="suppressDate" value="true"/>            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <!--資料庫連結地址帳號密碼-->        <jdbcConnection driverClass="com.mysql.jdbc.Driver"         connectionURL="jdbc:mysql://127.0.0.1:3306/crm" userId="root" password="123456">        </jdbcConnection>        <javaTypeResolver>            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <!--產生Model類存放位置-->        <javaModelGenerator targetPackage="com.keving.vo" targetProject="E:/EclipseWorkspace/mybatis_code_generator/src/main/java">            <property name="enableSubPackages" value="true"/>            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!--產生對應檔存放位置-->        <sqlMapGenerator targetPackage="com.keving.mapper" targetProject="E:/EclipseWorkspace/mybatis_code_generator/src/main/java">            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <!--產生Dao類存放位置-->        <javaClientGenerator type="XMLMAPPER" targetPackage="com.keving.dao" targetProject="E:/EclipseWorkspace/mybatis_code_generator/src/main/java">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <table tableName="t_user" domainObjectName="CustomerOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>    </context></generatorConfiguration>


3.配置運行命令參數
window—>preferences–>java–>installed jres—>edit 在彈出的對話方塊中 修改
jre 運行參數 -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME

MAVEN_HOME 為 環境變數名 和JAVA_HOME一樣配置

run as –>maven build
命令 mybatis-generator:generate

運行命令,顯示代碼產生成功

選中項目,右鍵重新整理

===============================
代碼自動產生後的效果:

======================
CustomerOrder.java

package com.keving.vo;import java.util.Date;public class CustomerOrder {    private Integer id;    private String userName;    private String userPwd;    private String trueName;    private String email;    private String phone;    private Integer isValid;    private Date createDate;    private Date updateDate;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName == null ? null : userName.trim();    }    public String getUserPwd() {        return userPwd;    }    public void setUserPwd(String userPwd) {        this.userPwd = userPwd == null ? null : userPwd.trim();    }    public String getTrueName() {        return trueName;    }    public void setTrueName(String trueName) {        this.trueName = trueName == null ? null : trueName.trim();    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email == null ? null : email.trim();    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone == null ? null : phone.trim();    }    public Integer getIsValid() {        return isValid;    }    public void setIsValid(Integer isValid) {        this.isValid = isValid;    }    public Date getCreateDate() {        return createDate;    }    public void setCreateDate(Date createDate) {        this.createDate = createDate;    }    public Date getUpdateDate() {        return updateDate;    }    public void setUpdateDate(Date updateDate) {        this.updateDate = updateDate;    }}

=====================================
CustomerOrderMapper.java

package com.keving.dao;import com.keving.vo.CustomerOrder;public interface CustomerOrderMapper {    int deleteByPrimaryKey(Integer id);    int insert(CustomerOrder record);    int insertSelective(CustomerOrder record);    CustomerOrder selectByPrimaryKey(Integer id);    int updateByPrimaryKeySelective(CustomerOrder record);    int updateByPrimaryKey(CustomerOrder record);}

==================================

CustomerOrderMapper.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.keving.dao.CustomerOrderMapper" >  <resultMap id="BaseResultMap" type="com.keving.vo.CustomerOrder" >    <id column="id" property="id" jdbcType="INTEGER" />    <result column="user_name" property="userName" jdbcType="VARCHAR" />    <result column="user_pwd" property="userPwd" jdbcType="VARCHAR" />    <result column="true_name" property="trueName" jdbcType="VARCHAR" />    <result column="email" property="email" jdbcType="VARCHAR" />    <result column="phone" property="phone" jdbcType="VARCHAR" />    <result column="is_valid" property="isValid" jdbcType="INTEGER" />    <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />    <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />  </resultMap>  <sql id="Base_Column_List" >    id, user_name, user_pwd, true_name, email, phone, is_valid, create_date, update_date  </sql>  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >    select     <include refid="Base_Column_List" />    from t_user    where id = #{id,jdbcType=INTEGER}  </select>  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >    delete from t_user    where id = #{id,jdbcType=INTEGER}  </delete>  <insert id="insert" parameterType="com.keving.vo.CustomerOrder" >    insert into t_user (id, user_name, user_pwd,       true_name, email, phone,       is_valid, create_date, update_date      )    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR},       #{trueName,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},       #{isValid,jdbcType=INTEGER}, #{createDate,jdbcType=TIMESTAMP}, #{updateDate,jdbcType=TIMESTAMP}      )  </insert>  <insert id="insertSelective" parameterType="com.keving.vo.CustomerOrder" >    insert into t_user    <trim prefix="(" suffix=")" suffixOverrides="," >      <if test="id != null" >        id,      </if>      <if test="userName != null" >        user_name,      </if>      <if test="userPwd != null" >        user_pwd,      </if>      <if test="trueName != null" >        true_name,      </if>      <if test="email != null" >        email,      </if>      <if test="phone != null" >        phone,      </if>      <if test="isValid != null" >        is_valid,      </if>      <if test="createDate != null" >        create_date,      </if>      <if test="updateDate != null" >        update_date,      </if>    </trim>    <trim prefix="values (" suffix=")" suffixOverrides="," >      <if test="id != null" >        #{id,jdbcType=INTEGER},      </if>      <if test="userName != null" >        #{userName,jdbcType=VARCHAR},      </if>      <if test="userPwd != null" >        #{userPwd,jdbcType=VARCHAR},      </if>      <if test="trueName != null" >        #{trueName,jdbcType=VARCHAR},      </if>      <if test="email != null" >        #{email,jdbcType=VARCHAR},      </if>      <if test="phone != null" >        #{phone,jdbcType=VARCHAR},      </if>      <if test="isValid != null" >        #{isValid,jdbcType=INTEGER},      </if>      <if test="createDate != null" >        #{createDate,jdbcType=TIMESTAMP},      </if>      <if test="updateDate != null" >        #{updateDate,jdbcType=TIMESTAMP},      </if>    </trim>  </insert>  <update id="updateByPrimaryKeySelective" parameterType="com.keving.vo.CustomerOrder" >    update t_user    <set >      <if test="userName != null" >        user_name = #{userName,jdbcType=VARCHAR},      </if>      <if test="userPwd != null" >        user_pwd = #{userPwd,jdbcType=VARCHAR},      </if>      <if test="trueName != null" >        true_name = #{trueName,jdbcType=VARCHAR},      </if>      <if test="email != null" >        email = #{email,jdbcType=VARCHAR},      </if>      <if test="phone != null" >        phone = #{phone,jdbcType=VARCHAR},      </if>      <if test="isValid != null" >        is_valid = #{isValid,jdbcType=INTEGER},      </if>      <if test="createDate != null" >        create_date = #{createDate,jdbcType=TIMESTAMP},      </if>      <if test="updateDate != null" >        update_date = #{updateDate,jdbcType=TIMESTAMP},      </if>    </set>    where id = #{id,jdbcType=INTEGER}  </update>  <update id="updateByPrimaryKey" parameterType="com.keving.vo.CustomerOrder" >    update t_user    set user_name = #{userName,jdbcType=VARCHAR},      user_pwd = #{userPwd,jdbcType=VARCHAR},      true_name = #{trueName,jdbcType=VARCHAR},      email = #{email,jdbcType=VARCHAR},      phone = #{phone,jdbcType=VARCHAR},      is_valid = #{isValid,jdbcType=INTEGER},      create_date = #{createDate,jdbcType=TIMESTAMP},      update_date = #{updateDate,jdbcType=TIMESTAMP}    where id = #{id,jdbcType=INTEGER}  </update></mapper>

相關內容串連:
使用Mybatis-Generator自動產生Dao、Model、Mapping相關檔案
http://www.cnblogs.com/lichenwei/p/4145696.html

利用mybatis-generator自動產生代碼
http://www.cnblogs.com/yjmyzz/p/4210554.html

【MyBatis架構】mybatis逆向工程自動產生代碼
http://blog.csdn.net/acmman/article/details/46906871

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.