詳解Mybatis Generator的具體使用教程
Mybatis屬于半自動ORM,在使用這個框架中,工作量最大的就是書寫Mapping的映射文件,由于手動書寫很容易出錯,我們可以利用Mybatis-Generator來幫我們自動生成文件。
1、相關(guān)文件
關(guān)于Mybatis-Generator的下載可以到這個地址:https://github.com/mybatis/generator/releases
由于我使用的是Mysql數(shù)據(jù)庫,這里需要在準備一個連接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>
<!--生成對應表及類名-->
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>需要修改文件配置的地方我都已經(jīng)把注釋標注出來了,這里的相關(guān)路徑(如數(shù)據(jù)庫驅(qū)動包,生成對應的相關(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鍵,右鍵鼠標選擇"在此處打開命令窗口",復制粘貼生成語句的文件代碼即可。
看下效果圖:


生成相關(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)文章希望大家以后多多支持腳本之家!
- mybatis-plus使用generator實現(xiàn)逆向工程
- mybatis-generator生成文件覆蓋問題的解決
- mybatis plus generator 根據(jù)數(shù)據(jù)庫自動生成實體類的實現(xiàn)示例
- mybatis mybatis-plus-generator+clickhouse自動生成代碼案例詳解
- SpringBoot整合mybatis-generator-maven-plugin的方法
- 基于Mybatis Plus實現(xiàn)代碼生成器CodeGenerator
- IDEA集成MyBatis Generator插件的使用
- 使用mybatis-plus-generator進行代碼自動生成的方法
- 詳解在springboot中使用Mybatis Generator的兩種方式
- MyBatis Generator介紹及使用方法
相關(guān)文章
Mybatis空值關(guān)聯(lián)的具體實現(xiàn)
在復雜的數(shù)據(jù)庫查詢中,處理空值關(guān)聯(lián)是一項常見的需求,本文就來介紹一下Mybatis空值關(guān)聯(lián)的具體實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07
超詳細講解SpringCloud?Commons公共抽象的用法
這篇文章主要介紹了超詳細講解SpringCloud?Commons公共抽象的用法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
SpringBoot之logback-spring.xml不生效的解決方法
這篇文章主要介紹了SpringBoot之logback-spring.xml不生效的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
mall整合SpringTask實現(xiàn)定時任務的方法示例
這篇文章主要介紹了mall整合SpringTask實現(xiàn)定時任務的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
分頁技術(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,具有很好的參考價值,希望對大家有所幫助。2022-03-03
springboot集成springsession如何實現(xiàn)分布式session共享
這篇文章主要介紹了springboot集成springsession如何實現(xiàn)分布式session共享問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析
這篇文章主要介紹了SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12

