使用Mybatis-Generator自動產生Dao、Model、Mapping相關檔案

來源:互聯網
上載者:User

標籤:his   pat   idt   ref   img   相關   lan   example   gen   

Mybatis屬於半自動ORM,在使用這個架構中,工作量最大的就是書寫Mapping的對應檔,由於手動書寫很容易出錯,我們可以利用Mybatis-Generator來幫我們自動組建檔案。

 

1、相關檔案

1、在G盤建立一個檔案夾,命名:generator(或者其他盤其他名字也可以,之所以用這個,是為了copy下面代碼後,不用再做修改路徑)

2、準備需要的jar包:mybatis-generator-core-1.3.2.jar、MySQL-connector-Java-5.1.34.jar(忽略版本號碼,這隻是我用的jar 版本)

3、建立一個檔案,命名:generatorConfig.xml

以下是相關檔案:

 

 

和Hibernate逆向產生一樣,這裡也需要一個設定檔:

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="mysql-connector-java-5.0.4-bin.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://localhost/test" userId="root" password="123456">        </jdbcConnection>        <javaTypeResolver>            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <!--產生Model類存放位置-->        <javaModelGenerator targetPackage="model" targetProject="test">            <property name="enableSubPackages" value="true"/>            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!--產生對應檔存放位置-->        <sqlMapGenerator targetPackage="mapping" targetProject="test">            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <!--產生Dao類存放位置-->        <javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="test">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <!--產生對應表及類名-->        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>    </context></generatorConfiguration>

  

需要修改檔案配置的地方我都已經把注釋標註出來了,這裡的相關路徑(如資料庫驅動包,產生對應的相關檔案位置可以自訂)不能帶有中文。

上面設定檔中的:

<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

tableName和domainObjectName為必選項,分別代表資料庫表名和產生的實體類名,其餘的可以自訂去選擇(一般情況下均為false)。

 

產生語句檔案:

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

 

 

2、使用方法

在該目錄按住Shift鍵,右鍵滑鼠選擇"在此處開啟命令視窗",複製粘貼產生語句的檔案代碼即可。

 

看下:

首先這個是我的資料庫表

 

 

 

 

 

產生相關代碼:

User.java

package model;public class User {    private Integer uid;    private String username;    private String password;    private String name;    private String email;    private String phone;    private String addr;    private Integer state;    private String code;    public Integer getUid() {        return uid;    }    public void setUid(Integer uid) {        this.uid = uid;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username == null ? null : username.trim();    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password == null ? null : password.trim();    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name == null ? null : name.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 String getAddr() {        return addr;    }    public void setAddr(String addr) {        this.addr = addr == null ? null : addr.trim();    }    public Integer getState() {        return state;    }    public void setState(Integer state) {        this.state = state;    }    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code == null ? null : code.trim();    }}

UserMapper.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="dao.UserMapper" >  <resultMap id="BaseResultMap" type="model.User" >    <id column="uid" property="uid" jdbcType="INTEGER" />    <result column="username" property="username" jdbcType="VARCHAR" />    <result column="password" property="password" jdbcType="VARCHAR" />    <result column="name" property="name" jdbcType="VARCHAR" />    <result column="email" property="email" jdbcType="VARCHAR" />    <result column="phone" property="phone" jdbcType="VARCHAR" />    <result column="addr" property="addr" jdbcType="VARCHAR" />    <result column="state" property="state" jdbcType="INTEGER" />    <result column="code" property="code" jdbcType="VARCHAR" />  </resultMap>  <sql id="Base_Column_List" >    uid, username, password, name, email, phone, addr, state, code  </sql>  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >    select     <include refid="Base_Column_List" />    from user    where uid = #{uid,jdbcType=INTEGER}  </select>  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >    delete from user    where uid = #{uid,jdbcType=INTEGER}  </delete>  <insert id="insert" parameterType="model.User" >    insert into user (uid, username, password,       name, email, phone,       addr, state, code)    values (#{uid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},       #{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},       #{addr,jdbcType=VARCHAR}, #{state,jdbcType=INTEGER}, #{code,jdbcType=VARCHAR})  </insert>  <insert id="insertSelective" parameterType="model.User" >    insert into user    <trim prefix="(" suffix=")" suffixOverrides="," >      <if test="uid != null" >        uid,      </if>      <if test="username != null" >        username,      </if>      <if test="password != null" >        password,      </if>      <if test="name != null" >        name,      </if>      <if test="email != null" >        email,      </if>      <if test="phone != null" >        phone,      </if>      <if test="addr != null" >        addr,      </if>      <if test="state != null" >        state,      </if>      <if test="code != null" >        code,      </if>    </trim>    <trim prefix="values (" suffix=")" suffixOverrides="," >      <if test="uid != null" >        #{uid,jdbcType=INTEGER},      </if>      <if test="username != null" >        #{username,jdbcType=VARCHAR},      </if>      <if test="password != null" >        #{password,jdbcType=VARCHAR},      </if>      <if test="name != null" >        #{name,jdbcType=VARCHAR},      </if>      <if test="email != null" >        #{email,jdbcType=VARCHAR},      </if>      <if test="phone != null" >        #{phone,jdbcType=VARCHAR},      </if>      <if test="addr != null" >        #{addr,jdbcType=VARCHAR},      </if>      <if test="state != null" >        #{state,jdbcType=INTEGER},      </if>      <if test="code != null" >        #{code,jdbcType=VARCHAR},      </if>    </trim>  </insert>  <update id="updateByPrimaryKeySelective" parameterType="model.User" >    update user    <set >      <if test="username != null" >        username = #{username,jdbcType=VARCHAR},      </if>      <if test="password != null" >        password = #{password,jdbcType=VARCHAR},      </if>      <if test="name != null" >        name = #{name,jdbcType=VARCHAR},      </if>      <if test="email != null" >        email = #{email,jdbcType=VARCHAR},      </if>      <if test="phone != null" >        phone = #{phone,jdbcType=VARCHAR},      </if>      <if test="addr != null" >        addr = #{addr,jdbcType=VARCHAR},      </if>      <if test="state != null" >        state = #{state,jdbcType=INTEGER},      </if>      <if test="code != null" >        code = #{code,jdbcType=VARCHAR},      </if>    </set>    where uid = #{uid,jdbcType=INTEGER}  </update>  <update id="updateByPrimaryKey" parameterType="model.User" >    update user    set username = #{username,jdbcType=VARCHAR},      password = #{password,jdbcType=VARCHAR},      name = #{name,jdbcType=VARCHAR},      email = #{email,jdbcType=VARCHAR},      phone = #{phone,jdbcType=VARCHAR},      addr = #{addr,jdbcType=VARCHAR},      state = #{state,jdbcType=INTEGER},      code = #{code,jdbcType=VARCHAR}    where uid = #{uid,jdbcType=INTEGER}  </update></mapper>

UserMapper.java

package dao;import model.User;public interface UserMapper {    int deleteByPrimaryKey(Integer uid);    int insert(User record);    int insertSelective(User record);    User selectByPrimaryKey(Integer uid);    int updateByPrimaryKeySelective(User record);    int updateByPrimaryKey(User record);}

  

 

使用Mybatis-Generator自動產生Dao、Model、Mapping相關檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.