欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Mybatis Generator的具體使用教程

 更新時(shí)間:2022年02月10日 09:28:35   作者:zorro的菜鳥(niǎo)筆記  
Mybatis Generator可以幫助我們自動(dòng)生成很多結(jié)構(gòu)化的代碼,比如每張表對(duì)應(yīng)的Entity、Mapper接口和Xml文件,可以省去很多繁瑣的工作,今天通過(guò)本文給大家介紹Mybatis Generator的具體使用教程,感興趣的朋友一起看看吧

Mybatis屬于半自動(dòng)ORM,在使用這個(gè)框架中,工作量最大的就是書(shū)寫(xiě)Mapping的映射文件,由于手動(dòng)書(shū)寫(xiě)很容易出錯(cuò),我們可以利用Mybatis-Generator來(lái)幫我們自動(dòng)生成文件。

1、相關(guān)文件

關(guān)于Mybatis-Generator的下載可以到這個(gè)地址:https://github.com/mybatis/generator/releases

由于我使用的是Mysql數(shù)據(jù)庫(kù),這里需要在準(zhǔn)備一個(gè)連接mysql數(shù)據(jù)庫(kù)的驅(qū)動(dòng)jar包

以下是相關(guān)文件截圖:

和Hibernate逆向生成一樣,這里也需要一個(gè)配置文件:

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>
    <!--數(shù)據(jù)庫(kù)驅(qū)動(dòng)-->
    <classPathEntry    location="mysql-connector-java-5.0.8-bin.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--數(shù)據(jù)庫(kù)鏈接地址賬號(hào)密碼-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model類(lèi)存放位置-->
        <javaModelGenerator targetPackage="lcw.model" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao類(lèi)存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成對(duì)應(yīng)表及類(lèi)名-->
        <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

需要修改文件配置的地方我都已經(jīng)把注釋標(biāo)注出來(lái)了,這里的相關(guān)路徑(如數(shù)據(jù)庫(kù)驅(qū)動(dòng)包,生成對(duì)應(yīng)的相關(guān)文件位置可以自定義)不能帶有中文。

上面配置文件中的:

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

tableName和domainObjectName為必選項(xiàng),分別代表數(shù)據(jù)庫(kù)表名和生成的實(shí)力類(lèi)名,其余的可以自定義去選擇(一般情況下均為false)。

生成語(yǔ)句文件:

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

2、使用方法

在該目錄按住Shift鍵,右鍵鼠標(biāo)選擇"在此處打開(kāi)命令窗口",復(fù)制粘貼生成語(yǔ)句的文件代碼即可。

看下效果圖:

生成相關(guān)代碼:

Message.java

package lcw.model;
public class Messgae {
    private Integer id;
    private String title;
    private String describe;
    private String content;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title == null ? null : title.trim();
    }
    public String getDescribe() {
        return describe;
    }
    public void setDescribe(String describe) {
        this.describe = describe == null ? null : describe.trim();
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content == null ? null : content.trim();
    }
}

MessgaeMapper.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="lcw.dao.MessgaeMapper" >
  <resultMap id="BaseResultMap" type="lcw.model.Messgae" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="describe" property="describe" jdbcType="VARCHAR" />
    <result column="content" property="content" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, title, describe, content
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from message
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from message
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="lcw.model.Messgae" >
    insert into message (id, title, describe,
      content)
    values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR},
      #{content,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="lcw.model.Messgae" >
    insert into message
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="title != null" >
        title,
      </if>
      <if test="describe != null" >
        describe,
      </if>
      <if test="content != null" >
        content,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="title != null" >
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="describe != null" >
        #{describe,jdbcType=VARCHAR},
      </if>
      <if test="content != null" >
        #{content,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="lcw.model.Messgae" >
    update message
    <set >
      <if test="title != null" >
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="describe != null" >
        describe = #{describe,jdbcType=VARCHAR},
      </if>
      <if test="content != null" >
        content = #{content,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="lcw.model.Messgae" >
    update message
    set title = #{title,jdbcType=VARCHAR},
      describe = #{describe,jdbcType=VARCHAR},
      content = #{content,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

MessgaeMapper.java

package lcw.dao;
import lcw.model.Messgae;
public interface MessgaeMapper {
    int deleteByPrimaryKey(Integer id);
    int insert(Messgae record);
    int insertSelective(Messgae record);
    Messgae selectByPrimaryKey(Integer id);
    int updateByPrimaryKeySelective(Messgae record);
    int updateByPrimaryKey(Messgae record);
}

到此這篇關(guān)于詳解Mybatis Generator的具體使用教程的文章就介紹到這了,更多相關(guān)Mybatis Generator使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Async的線程池使用選擇解析

    Async的線程池使用選擇解析

    這篇文章主要為大家介紹了Async的線程池使用選擇解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Java中Spring的Security使用詳解

    Java中Spring的Security使用詳解

    這篇文章主要介紹了Java中Spring的Security使用詳解,在web應(yīng)用開(kāi)發(fā)中,安全無(wú)疑是十分重要的,選擇Spring Security來(lái)保護(hù)web應(yīng)用是一個(gè)非常好的選擇,需要的朋友可以參考下
    2023-07-07
  • Spring Security角色繼承分析

    Spring Security角色繼承分析

    這篇文章主要介紹了Spring Security角色繼承分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java中常用的6種排序算法詳細(xì)分解

    Java中常用的6種排序算法詳細(xì)分解

    這篇文章主要介紹了Java中常用的6種排序算法詳細(xì)分解,著重說(shuō)明每個(gè)算法的計(jì)算過(guò)程分解,是探究實(shí)現(xiàn)原理級(jí)的文章,對(duì)于深入理解這些算法有很大幫助,需要的朋友可以參考下
    2014-07-07
  • Idea中maven無(wú)法下載依賴(lài)包問(wèn)題解決

    Idea中maven無(wú)法下載依賴(lài)包問(wèn)題解決

    用過(guò)idea開(kāi)發(fā)過(guò)項(xiàng)目的同學(xué),偶爾會(huì)遇到項(xiàng)目中有一些依賴(lài)沒(méi)法下載,或者依賴(lài)包已經(jīng)有項(xiàng)目卻無(wú)法掃到的問(wèn)題,本文就詳細(xì)的介紹了解決方法,感興趣的可以了解一下
    2020-08-08
  • Mybatis-Plus通過(guò)SQL注入器實(shí)現(xiàn)批量插入的實(shí)踐

    Mybatis-Plus通過(guò)SQL注入器實(shí)現(xiàn)批量插入的實(shí)踐

    本文主要介紹了Mybatis-Plus通過(guò)SQL注入器實(shí)現(xiàn)批量插入的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 5個(gè)主流的Java開(kāi)源IDE工具詳解

    5個(gè)主流的Java開(kāi)源IDE工具詳解

    這篇文章主要介紹了5個(gè)主流的Java開(kāi)源IDE工具,無(wú)論如何,Java在當(dāng)今使用的編程語(yǔ)言中始終排在前三名,在TIOBE索引中涉及700萬(wàn)到1000萬(wàn)的程序員和開(kāi)發(fā)者
    2020-07-07
  • Java學(xué)習(xí)之如何進(jìn)行JSON解析

    Java學(xué)習(xí)之如何進(jìn)行JSON解析

    JSON(JavaScript?Object?Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,它算是JavaScript語(yǔ)言的一部分,與XML一樣都可以用于數(shù)據(jù)的存儲(chǔ)和傳輸,本文講給大家介紹如何進(jìn)行JSON解析,需要的朋友可以參考下
    2023-12-12
  • SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空的問(wèn)題

    SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空的問(wèn)題

    這篇文章主要介紹了SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • springboot打包如何忽略Test單元測(cè)試

    springboot打包如何忽略Test單元測(cè)試

    這篇文章主要介紹了springboot打包如何忽略Test單元測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評(píng)論