1. 程式人生 > >使用Mybatis-Generator自動生成Dao、Model、Mapping相關文件(轉)

使用Mybatis-Generator自動生成Dao、Model、Mapping相關文件(轉)

rop root github mini -c back fig override creat

https://github.com/astarring/mybatis-generator-gui 帶界面版:需要jdk 1.8以上 技術分享

技術分享

出處: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="D:/OfficeAPP/AutoCreateMapper/mysql-connector-java-5.1.21.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://172.16.2.37/uc_wsp" userId="root" password="hanming123">
         </jdbcConnection>
         <javaTypeResolver>
             <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
         <!--生成Model類存放位置-->
         <javaModelGenerator targetPackage="com.chinaunicom.wsp.facade.entity" targetProject="D:/OfficeAPP/AutoCreateMapper/">
            <property name="enableSubPackages" value="true"/>
             <property name="trimStrings" value="true"/>
         </javaModelGenerator>
         <!--生成映射文件存放位置-->
         <sqlMapGenerator targetPackage="lcw.mapping" targetProject="D:/OfficeAPP/AutoCreateMapper/">
             <property name="enableSubPackages" value="true"/>
         </sqlMapGenerator>
         <!--生成Dao類存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="D:/OfficeAPP/AutoCreateMapper/">
             <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
         <!--生成對應表及類名-->
        <table tableName="wsp_sms_mt" domainObjectName="SmsMt" 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.5.jar -configfile generatorConfig.xml -overwrite

2、使用方法

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

看下效果圖:

技術分享

技術分享

生成相關代碼:

Message.java

技術分享 技術分享
 1 package lcw.model;
 2 
 3 public class Messgae {
 4     private Integer id;
 5 
 6     private String title;
 7 
 8     private String describe;
 9 
10     private String content;
11 
12     public Integer getId() {
13         return id;
14     }
15 
16     public void setId(Integer id) {
17         this.id = id;
18     }
19 
20     public String getTitle() {
21         return title;
22     }
23 
24     public void setTitle(String title) {
25         this.title = title == null ? null : title.trim();
26     }
27 
28     public String getDescribe() {
29         return describe;
30     }
31 
32     public void setDescribe(String describe) {
33         this.describe = describe == null ? null : describe.trim();
34     }
35 
36     public String getContent() {
37         return content;
38     }
39 
40     public void setContent(String content) {
41         this.content = content == null ? null : content.trim();
42     }
43 }
技術分享 技術分享

MessgaeMapper.xml

技術分享 技術分享
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="lcw.dao.MessgaeMapper" >
 4   <resultMap id="BaseResultMap" type="lcw.model.Messgae" >
 5     <id column="id" property="id" jdbcType="INTEGER" />
 6     <result column="title" property="title" jdbcType="VARCHAR" />
 7     <result column="describe" property="describe" jdbcType="VARCHAR" />
 8     <result column="content" property="content" jdbcType="VARCHAR" />
 9   </resultMap>
10   <sql id="Base_Column_List" >
11     id, title, describe, content
12   </sql>
13   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
14     select 
15     <include refid="Base_Column_List" />
16     from message
17     where id = #{id,jdbcType=INTEGER}
18   </select>
19   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
20     delete from message
21     where id = #{id,jdbcType=INTEGER}
22   </delete>
23   <insert id="insert" parameterType="lcw.model.Messgae" >
24     insert into message (id, title, describe, 
25       content)
26     values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR}, 
27       #{content,jdbcType=VARCHAR})
28   </insert>
29   <insert id="insertSelective" parameterType="lcw.model.Messgae" >
30     insert into message
31     <trim prefix="(" suffix=")" suffixOverrides="," >
32       <if test="id != null" >
33         id,
34       </if>
35       <if test="title != null" >
36         title,
37       </if>
38       <if test="describe != null" >
39         describe,
40       </if>
41       <if test="content != null" >
42         content,
43       </if>
44     </trim>
45     <trim prefix="values (" suffix=")" suffixOverrides="," >
46       <if test="id != null" >
47         #{id,jdbcType=INTEGER},
48       </if>
49       <if test="title != null" >
50         #{title,jdbcType=VARCHAR},
51       </if>
52       <if test="describe != null" >
53         #{describe,jdbcType=VARCHAR},
54       </if>
55       <if test="content != null" >
56         #{content,jdbcType=VARCHAR},
57       </if>
58     </trim>
59   </insert>
60   <update id="updateByPrimaryKeySelective" parameterType="lcw.model.Messgae" >
61     update message
62     <set >
63       <if test="title != null" >
64         title = #{title,jdbcType=VARCHAR},
65       </if>
66       <if test="describe != null" >
67         describe = #{describe,jdbcType=VARCHAR},
68       </if>
69       <if test="content != null" >
70         content = #{content,jdbcType=VARCHAR},
71       </if>
72     </set>
73     where id = #{id,jdbcType=INTEGER}
74   </update>
75   <update id="updateByPrimaryKey" parameterType="lcw.model.Messgae" >
76     update message
77     set title = #{title,jdbcType=VARCHAR},
78       describe = #{describe,jdbcType=VARCHAR},
79       content = #{content,jdbcType=VARCHAR}
80     where id = #{id,jdbcType=INTEGER}
81   </update>
82 </mapper>
技術分享 技術分享

MessgaeMapper.java

技術分享 技術分享 1 package lcw.dao;
2
3 import lcw.model.Messgae;
4
5 public interface MessgaeMapper {
6 int deleteByPrimaryKey(Integer id);
7
8 int insert(Messgae record);
9
10 int insertSelective(Messgae record);
11
12 Messgae selectByPrimaryKey(Integer id);
13
14 int updateByPrimaryKeySelective(Messgae record);
15
16 int updateByPrimaryKey(Messgae record);
17 }

使用Mybatis-Generator自動生成Dao、Model、Mapping相關文件(轉)