最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Mybatis Generator的具體使用教程

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

Mybatis屬于半自動ORM,在使用這個框架中,工作量最大的就是書寫Mapping的映射文件,由于手動書寫很容易出錯,我們可以利用Mybatis-Generator來幫我們自動生成文件。

1、相關(guān)文件

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

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

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

和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>
    <!--數(shù)據(jù)庫驅(qū)動-->
    <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ù)庫鏈接地址賬號密碼-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model類存放位置-->
        <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類存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成對應(yīng)表及類名-->
        <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

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

上面配置文件中的:

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

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

生成語句文件:

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

2、使用方法

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

看下效果圖:

生成相關(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis空值關(guān)聯(lián)的具體實現(xiàn)

    Mybatis空值關(guān)聯(lián)的具體實現(xiàn)

    在復(fù)雜的數(shù)據(jù)庫查詢中,處理空值關(guān)聯(lián)是一項常見的需求,本文就來介紹一下Mybatis空值關(guān)聯(lián)的具體實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • 超詳細(xì)講解SpringCloud?Commons公共抽象的用法

    超詳細(xì)講解SpringCloud?Commons公共抽象的用法

    這篇文章主要介紹了超詳細(xì)講解SpringCloud?Commons公共抽象的用法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • SpringBoot之logback-spring.xml不生效的解決方法

    SpringBoot之logback-spring.xml不生效的解決方法

    這篇文章主要介紹了SpringBoot之logback-spring.xml不生效的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • mall整合SpringTask實現(xiàn)定時任務(wù)的方法示例

    mall整合SpringTask實現(xiàn)定時任務(wù)的方法示例

    這篇文章主要介紹了mall整合SpringTask實現(xiàn)定時任務(wù)的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • 分頁技術(shù)原理與實現(xiàn)之Java+Oracle代碼實現(xiàn)分頁(二)

    分頁技術(shù)原理與實現(xiàn)之Java+Oracle代碼實現(xiàn)分頁(二)

    這篇文章主要介紹了分頁技術(shù)原理與實現(xiàn)的第二篇:Java+Oracle代碼實現(xiàn)分頁,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 關(guān)于maven全局配置文件settings.xml解析

    關(guān)于maven全局配置文件settings.xml解析

    這篇文章主要介紹了關(guān)于maven全局配置文件settings.xml,具有很好的參考價值,希望對大家有所幫助。
    2022-03-03
  • springboot集成springsession如何實現(xiàn)分布式session共享

    springboot集成springsession如何實現(xiàn)分布式session共享

    這篇文章主要介紹了springboot集成springsession如何實現(xiàn)分布式session共享問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Spring事務(wù)處理原理步驟詳解

    Spring事務(wù)處理原理步驟詳解

    這篇文章主要介紹了Spring事務(wù)處理原理步驟詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • 開發(fā)工具EesyCode使用方法解析

    開發(fā)工具EesyCode使用方法解析

    這篇文章主要介紹了開發(fā)工具EesyCode使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析

    SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析

    這篇文章主要介紹了SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12

最新評論

兖州市| 邵阳市| 铁力市| 万源市| 上饶市| 北海市| 东阿县| 永安市| 当涂县| 江源县| 凌海市| 通渭县| 措美县| 玉林市| 集安市| 乾安县| 兴海县| 河北区| 叙永县| 蚌埠市| 凤山市| 桂东县| 科技| 米脂县| 宜章县| 张家港市| 台州市| 洛南县| 逊克县| 禄丰县| 汝阳县| 甘谷县| 莒南县| 敦煌市| 惠州市| 九龙城区| 东方市| 崇信县| 佳木斯市| 浪卡子县| 文成县|