詳解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)文章希望大家以后多多支持腳本之家!
- mybatis-plus使用generator實(shí)現(xiàn)逆向工程
- mybatis-generator生成文件覆蓋問(wèn)題的解決
- mybatis plus generator 根據(jù)數(shù)據(jù)庫(kù)自動(dòng)生成實(shí)體類(lèi)的實(shí)現(xiàn)示例
- mybatis mybatis-plus-generator+clickhouse自動(dòng)生成代碼案例詳解
- SpringBoot整合mybatis-generator-maven-plugin的方法
- 基于Mybatis Plus實(shí)現(xiàn)代碼生成器CodeGenerator
- IDEA集成MyBatis Generator插件的使用
- 使用mybatis-plus-generator進(jìn)行代碼自動(dòng)生成的方法
- 詳解在springboot中使用Mybatis Generator的兩種方式
- MyBatis Generator介紹及使用方法
相關(guā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-08Mybatis-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-08Java學(xué)習(xí)之如何進(jìn)行JSON解析
JSON(JavaScript?Object?Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,它算是JavaScript語(yǔ)言的一部分,與XML一樣都可以用于數(shù)據(jù)的存儲(chǔ)和傳輸,本文講給大家介紹如何進(jìn)行JSON解析,需要的朋友可以參考下2023-12-12SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空的問(wèn)題
這篇文章主要介紹了SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10