MyBatis學習總結(九)——使用Generator工具自動生成Mapper,Dao
阿新 • • 發佈:2018-12-30
出處:http://www.cnblogs.com/lichenwei/p/4145696.html
Mybatis屬於半自動ORM,在使用這個框架中,工作量最大的就是書寫Mapping的對映檔案,由於手動書寫很容易出錯,我們可以利用Mybatis-Generator來幫我們自動生成檔案。
1、相關檔案
關於Mybatis-Generator的下載可以到這個地址:https://github.com/mybatis/generator/releases
由於我使用的是Mysql資料庫,這裡需要在準備一個連線mysql資料庫的驅動jar包
以下是相關檔案截圖:
和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.8-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/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>
需要修改檔案配置的地方我都已經把註釋標註出來了,這裡的相關路徑(如資料庫驅動包,生成對應的相關檔案位置可以自定義)不能帶有中文。
上面配置檔案中的:
<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鍵,右鍵滑鼠選擇"在此處開啟命令視窗",複製貼上生成語句的檔案程式碼即可。
看下效果圖:
生成相關程式碼:
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);
}