Automatically generate DAO, Model, mapping related files using Mybatis-generator (GO)
Source: http://www.cnblogs.com/lichenwei/p/4145696.html
MyBatis is a semi-automatic ORM, in the use of this framework, the most effort is to write mapping mapping files, because manual writing is prone to error, we can use Mybatis-generator to help us automatically generate files.
1. Related Documents
About Mybatis-generator download can go to this address: https://github.com/mybatis/generator/releases
Since I am using MySQL database, I need to prepare a driver jar package to connect MySQL database.
The following documents are relevant:
As with hibernate reverse generation, a configuration file is also needed here:
Generatorconfig.xml
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE generatorconfiguration 3 Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" 4 "Http://mybati S.org/dtd/mybatis-generator-config_1_0.dtd "> 5 <generatorConfiguration> 6 <!--database Driver--7 <CLASSP Athentry location= "Mysql-connector-java-5.0.8-bin.jar"/> 8 <context id= "Db2tables" TargetRuntime= "MyBatis3 "> 9 <commentgenerator>10 <property name=" Suppressdate "value=" true "/>11 &L T;property name= "Suppressallcomments" value= "true"/>12 </commentgenerator>13 <!--database link Address account password-- >14 <jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "Jdbc:mysql://localhost/mymessages" us Erid= "root" password= "root" >15 </jdbcconnection>16 <javatyperesolver>17 <pro Perty name= "Forcebigdecimals" value= "false"/>18 </javatyperesolver>19 <!--generate model class storage location-->20 <javamodelgenerator targetpackage= "Lcw.model" targetproject= "src" >21 <property name= "Enablesubpackages" value= "true"/>22 <property name= "trimstrings" value= "true"/&G T;23 </javamodelgenerator>24 <!--generate Map file storage location-->25 <sqlmapgenerator targetpackage= "LC W.mapping "targetproject=" src ">26 <property name=" Enablesubpackages "value=" true "/>27 </sq lmapgenerator>28 <!--generate the DAO class storage location-->29 <javaclientgenerator type= "Xmlmapper" targetpackage= "LCW. DAO "targetproject=" src ">30 <property name=" Enablesubpackages "value=" true "/>31 </javaclie ntgenerator>32 <!--generate corresponding table and class name-->33 <table tablename= "message" domainobjectname= "Messgae" Enablec Ountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid= "false" ></table>34 </context>35 </generatorConfiguration>
Need to modify the file configuration where I have already annotated the note, where the relevant path (such as the database driver package, generated corresponding file location can be customized) cannot be in Chinese.
In the configuration file above:
<table tablename= "message" domainobjectname= "Messgae" enablecountbyexample= "false" enableupdatebyexample= "false "Enabledeletebyexample=" false "enableselectbyexample=" false "Selectbyexamplequeryid=" false "></table>
TableName and Domainobjectname are required, respectively, to represent the database table name and generated strength class name, the rest can be customized to choose (generally false).
Generate Statement file:
Java-jar Mybatis-generator-core-1.3.2.jar-configfile Generatorconfig.xml-overwrite
2. How to use
Hold down the SHIFT key in the directory and right-click the "Open command Window Here" to copy and paste the file code of the generated statement.
Look under:
Generate the relevant code:
Message.java
1 package Lcw.model; 2 3 public class Messgae {4 private Integer ID; 5 6 private string title; 7 8 private String desc Ribe 9 Private String content;11 public Integer getId () { return id;14}15-public void SetId (I Nteger ID) { this.id = id;18 }19 public String getTitle () { return title;22 }23 public void Settitle (String title) { This.title = title = = null? Null:title.trim (); }27 to Public St Ring Getdescribe () { describe return describe;30}31-public void Setdescribe (String This.describe = describe = = null? Null:describe.trim (); }35-Public String getcontent () {Notoginseng return content;38 }39 public void SetContent (String content) { This.content = content = = null? Null:content.trim (); }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" p roperty= "Content" jdbctype= "VARCHAR"/> 9 </resultmap>10 <sql id= "base_column_list" >11 ID, title, Describe, content12 </sql>13 <select id= "Selectbyprimarykey" resultmap= "Baseresultmap" parametertype= " Java.lang.Integer ">14 Select <include refid=" Base_column_list "/>16 from Message17 where id = #{id,jdbctype=integer}18 </select>19 <delete id= "Deletebyprimarykey" Parametertype= "java.Lang. Integer ">20 Delete from message21 where id = #{id,jdbctype=integer}22 </delete>23 <insert id=" ins ert "parametertype=" Lcw.model.Messgae ">24 INSERT into message (ID, title, describe, content) values (#{id,jdbctype=integer}, #{title,jdbctype=varchar}, #{describe,jdbctype=varchar}, #{content,jdbctype=varchar}) 2 8 </insert>29 <insert id= "insertselective" parametertype= "Lcw.model.Messgae" >30 insert INTO Message31 <trim prefix= "(" suffix= ")" suffixoverrides= "," >32 <if test= "id! = NULL" >33 id,34 & lt;/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 & lt;/if>44 </trim>45 <trim prefix= "VALUES (" suffix= ")" suffixoverrides= "," >46 <if test= "I D! = null ">47 #{id,jdbctype=integer},48 </if>49 <if test= "Title! = NULL" >50 #{title,jdbctype=varc har},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 </t rim>59 </insert>60 <update id= "updatebyprimarykeyselective" parametertype= "Lcw.model.Messgae" >61 u Pdate 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 &L t;/if>69 <if test= "Content! = NULL" >70 content = #{content,jdbctype=varchar},71 </if>7 2 </set>73 where id = #{id,jdbctype=integer}74 </update>75 <update id= "Updatebyprimarykey" para Metertype= "Lcw.model.MesSgae ">76 Update message77 Set title = #{title,jdbctype=varchar},78 Describe = #{describe,jdbctype=varcha r},79 content = #{content,jdbctype=varchar}80 where id = #{id,jdbctype=integer}81 </update>82 </mappe R>
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 int insertselective (messgae record); Messgae Selectbyprimarykey (Integer ID); int updatebyprimarykeyselective (messgae record); Updatebyprimarykey (Messgae record); 17}
Mybatis-generator How to use the concrete