MyBatisGenerator使用_第1頁
MyBatisGenerator使用_第2頁
MyBatisGenerator使用_第3頁
MyBatisGenerator使用_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、MyBatis Generator使用1、相關(guān)文件關(guān)于Mybatis-Generator的下載由于我使用的是Mysql數(shù)據(jù)庫,這里需要在準(zhǔn)備一個(gè)連接mysql數(shù)據(jù)庫的驅(qū)動(dòng)jar包以下是相關(guān)文件截圖: 和Hibernate逆向生成一樣,這里也需要一個(gè)配置文件:generatorConfig.xml1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//DTD MyBatis G

2、enerator Configuration 1.0/EN" 4 "://dtd/mybatis-generator-config_1_0.dtd"> 5 <generatorConfiguration> 6 <!-數(shù)據(jù)庫驅(qū)動(dòng)-> 7 <classPathEntry location="mysql-connector-java-5.0.8-bin.jar"/> 8 <context id="DB2Tables" targetRuntime="My

3、Batis3"> 9 <commentGenerator>10 <property name="suppressDate" value="true"/>11 <property name="suppressAllComments" value="true"/>12 </commentGenerator>13 <!-數(shù)據(jù)庫鏈接地址賬號(hào)密碼->14 <jdbcConnection driverClass="" conn

4、ectionURL="jdbc:mysql:/localhost/mymessages" userId="root" password="root">15 </jdbcConnection>16 <javaTypeResolver>17 <property name="forceBigDecimals" value="false"/>18 </javaTypeResolver>19 <!-生成Model類存放位置->20 <

5、javaModelGenerator targetPackage="lcw.model" targetProject="src">21 <property name="enableSubPackages" value="true"/>22 <property name="trimStrings" value="true"/>23 </javaModelGenerator>24 <!-生成映射文件存放位置->25 <s

6、qlMapGenerator targetPackage="lcw.mapping" targetProject="src">26 <property name="enableSubPackages" value="true"/>27 </sqlMapGenerator>28 <!-生成Dao類存放位置->29 <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao&quo

7、t; targetProject="src">30 <property name="enableSubPackages" value="true"/>31 </javaClientGenerator>32 <!-生成對(duì)應(yīng)表及類名->33 <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateBy

8、Example="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>34 </context>35 </generatorConfiguration>需要修改文件配置的地方我都已經(jīng)把注釋標(biāo)注出來了,這里的相關(guān)路徑(如數(shù)據(jù)庫驅(qū)動(dòng)包,生成對(duì)應(yīng)的相關(guān)文件位置可以自定義)不能帶有中文。上面配置文件中的:<table

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

10、ObjectName為必選項(xiàng),分別代表數(shù)據(jù)庫表名和生成的實(shí)力類名,其余的可以自定義去選擇(一般情況下均為false)。 生成語句文件:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite  2、使用方法在該目錄按住Shift鍵,右鍵鼠標(biāo)選擇"在此處打開命令窗口",復(fù)制粘貼生成語句的文件代碼即可。 看下效果圖:  生成相關(guān)代碼:Message.java1 package lcw.model; 2 3

11、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 setTit

12、le(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 pub

13、lic void setContent(String content) 41 this.content = content = null ? null : content.trim();42 43 MessgaeMapper.xml1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//DTD Mapper 3.0/EN" "://dtd/mybatis-3-mapper.dt

14、d" > 3 <mapper namespace="" > 4 <resultMap id="BaseResultMap" type="" > 5 <id column="id" property="id" jdbcType="INTEGER" /> 6 <result column="title" property="title" jdbcType="VARCHAR&

15、quot; /> 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, titl

16、e, describe, content12 </sql>13 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="" >14 select 15 <include refid="Base_Column_List" />16 from message17 where id = #id,jdbcType=INTEGER18 </select>19 <delete id=&

17、quot;deleteByPrimaryKey" parameterType="" >20 delete from message21 where id = #id,jdbcType=INTEGER22 </delete>23 <insert id="insert" parameterType="" >24 insert into message (id, title, describe, 25 content)26 values (#id,jdbcType=INTEGER, #title,jd

18、bcType=VARCHAR, #describe,jdbcType=VARCHAR, 27 #content,jdbcType=VARCHAR)28 </insert>29 <insert id="insertSelective" parameterType="" >30 insert into message31 <trim prefix="(" suffix=")" suffixOverrides="," >32 <if test="id

19、 != 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

20、 (" 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=

21、VARCHAR,54 </if>55 <if test="content != null" >56 #content,jdbcType=VARCHAR,57 </if>58 </trim>59 </insert>60 <update id="updateByPrimaryKeySelective" parameterType="" >61 update message62 <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,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論