Automatically generate DAO, Model, mapping related files using Mybatis-generator (GO)

Source: Internet
Author: User
Tags manual writing

https://github.com/astarring/mybatis-generator-guI with Interface version: Requires JDK 1.8 or more

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

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "Http://mybatis.or     G/dtd/mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <!--database driver-<classpathentry location= "D:/officeapp/autocreatemapper/mysql-connector-java-5.1.21.jar"/> <context id= "DB2Tables" Targe             Truntime= "MyBatis3" > <commentGenerator> <property name= "Suppressdate" value= "true"/> <property name= "Suppressallcomments" value= "true"/> </commentGenerator> <!--database chain Address account password--<jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "Jdbc:mysql://172.16.2.37/uc_w SP "userid=" root "password=" hanming123 "> </jdbcConnection> <javaTypeResolver> &         Lt;property name= "Forcebigdecimals" value= "false"/> </javaTypeResolver><!--generate model class storage location-<javamodelgenerator targetpackage= "Com.chinaunicom.wsp.facade.entity" targetproject= "d:/officeapp/autocreatemapper/" > <property name= "enablesubpackages" value= "true"/> <pr Operty name= "Trimstrings" value= "true"/> </javaModelGenerator> <!--build Map file storage location-&L T;sqlmapgenerator targetpackage= "lcw.mapping" targetproject= "d:/officeapp/autocreatemapper/" > <property Name= "Enablesubpackages" value= "true"/> </sqlMapGenerator> <!--generated DAO class storage location--&LT;JAV Aclientgenerator type= "Xmlmapper" targetpackage= "Lcw.dao" targetproject= "d:/officeapp/autocreatemapper/" > &        Lt;property name= "Enablesubpackages" value= "true"/> </javaClientGenerator> <!--generate corresponding table and class name-- <table tablename= "WSP_SMS_MT" domainobjectname= "Smsmt" enablecountbyexample= "false" enableupdatebyexample= " False "enabledeletebyexample=" FAlse "enableselectbyexample=" false "Selectbyexamplequeryid=" false "></table> </context> </ 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.5.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 describe; 9 privat E String content;11 public Integer getId () {id;14}15-public void setId (Integer id) {1 7 This.id = id;18}19 public String getTitle () {+ return title;22}23 public void S Ettitle (string title) {This.title = title = = null? Null:title.trim ();}27 to public String GetDesc Ribe () {Describe return describe;30}31-public void Setdescribe (String this.describe) {= describe = = null? Null:describe.trim ();}35-Public String getcontent () {PNS return content;38}39-Public VO ID 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 &lt  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
Ten int insertselective (Messgae record);
One
messgae Selectbyprimarykey (Integer ID);
-
updatebyprimarykeyselective Int (messgae record);
the
Updatebyprimarykey Int (messgae record);
+ }

Automatically generate DAO, Model, mapping related files using Mybatis-generator (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.