Automatic generation of DAO, Model, mapping related files using Mybatis-generator

Source: Internet
Author: User
Tags manual writing

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

1, a new folder in the G-disk, named: Generator (or other name of the other disk can also, the reason used this, is to copy the following code, do not have to modify the path)

2, prepare the required jar package: Mybatis-generator-core-1.3.2.jar, Mysql-connector-java-5.1.34.jar (Ignore the version number, this is just the jar version I use)

3, create a new file, named: Generatorconfig.xml

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.org/dtd/mybatis-generator-config_1_0.dtd "><generatorConfiguration> <!--database-driven < Classpathentry location= "Mysql-connector-java-5.0.4-bin.jar"/> <context id= "DB2Tables" TargetRuntime= "MyBat Is3 "> <commentGenerator> <property name=" Suppressdate "value=" true "/> <prop        Erty name= "Suppressallcomments" value= "true"/> </commentGenerator> <!--database link Address account password-- <jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "Jdbc:mysql://localhost/test" userid= "root" password= "123456" > </jdbcConnection> <javaTypeResolver> <property name= "Forcebi Gdecimals "value=" false "/> </javaTypeResolver> <!--generate model class storage location--<javamodelgenera Tor TArgetpackage= "model" targetproject= "Test" > <property name= "enablesubpackages" value= "true"/>        <property name= "Trimstrings" value= "true"/> </javaModelGenerator> <!--generate Map file storage location-- <sqlmapgenerator targetpackage= "Mapping" targetproject= "Test" > <property name= "enablesubpackages" V Alue= "true"/> </sqlMapGenerator> <!--generated DAO class storage location-<javaclientgenerator type= "Xmlma        Pper "targetpackage=" DAO "targetproject=" Test "> <property name=" enablesubpackages "value=" true "/> </javaClientGenerator> <!--generate the corresponding table and class name---<table tablename= "user" domainobjectname= "user" Ena Blecountbyexample= "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 the generated entity class name, and the rest can be customized to select (in general, 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:

First of all, this is my database table.

Generate the relevant code:

User.java

Package Model;public class User {private Integer uid;    Private String username;    private String password;    private String name;    Private String Email;    Private String phone;    Private String addr;    Private Integer State;    Private String Code;    Public Integer Getuid () {return UID;    } public void SetUid (Integer uid) {this.uid = UID;    } public String GetUserName () {return username;    } public void Setusername (String username) {This.username = Username = = null? Null:username.trim ();    } public String GetPassword () {return password;    } public void SetPassword (String password) {this.password = password = = null? Null:password.trim ();    } public String GetName () {return name;    } public void SetName (String name) {this.name = name = = null? Null:name.trim ();    } public String Getemail () {return email; } public void Setemail (String email) {This.emaIl = email = = NULL?    Null:email.trim ();    } public String Getphone () {return phone;    } public void Setphone (String phone) {This.phone = phone = = null? Null:phone.trim ();    } public String getaddr () {return addr;    } public void Setaddr (String addr) {this.addr = addr = = null? Null:addr.trim ();    } public Integer GetState () {return state;    The public void SetState (Integer state) {this.state = state;    } public String GetCode () {return code;    } public void Setcode (String code) {This.code = code = = null? Null:code.trim (); }}

Usermapper.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= "DAO. Usermapper "> <resultmap id=" baseresultmap "type=" model. User "> <id column=" UID "property=" UID "jdbctype=" INTEGER "/> <result column=" username "property=" Userna Me "jdbctype=" varchar "/> <result column=" password "property=" password "jdbctype=" varchar "/> <result Co lumn= "name" property= "name" jdbctype= "varchar"/> <result column= "email" property= "email" jdbctype= "varchar"/&G    T <result column= "Phone" property= "Phone" jdbctype= "VARCHAR"/> <result column= "addr" property= "addr" jdbctype= "VARCHAR"/> <result column= "state" property= "state" jdbctype= "INTEGER"/> <result column= "code" Propert y= "code" jdbctype= "VARCHAR"/> </resultMap> <sql id= "base_column_list" > uid, username, password, name , email, phone,addr, State, code </sql> <select id= "Selectbyprimarykey" resultmap= "Baseresultmap" parametertype= " Java.lang.Integer "> select <include refid=" Base_column_list "/> from user where uid = #{uid,jdbctyp     E=integer} </select> <delete id= "Deletebyprimarykey" parametertype= "Java.lang.Integer" > Delete from user where uid = #{uid,jdbctype=integer} </delete> <insert id= "Insert" parametertype= "model. User "> INSERT into User (UID, username, password, name, e-mail, phone, addr, State, code) VALUES (#{u Id,jdbctype=integer}, #{username,jdbctype=varchar}, #{password,jdbctype=varchar}, #{name,jdbctype=varchar}, #{ Email,jdbctype=varchar}, #{phone,jdbctype=varchar}, #{addr,jdbctype=varchar}, #{state,jdbctype=integer}, #{code, Jdbctype=varchar}) </insert> <insert id= "insertselective" parametertype= "model. User "> INSERT INTO User <trim prefix=" ("suffix=") "suffixoverrides=", "> <If test= "UID! = null" > UID, </if> <if test= "Username! = NULL" > Username, &lt        ;/if> <if test= "Password! = null" > Password, </if> <if test= "Name! = NULL" > Name, </if> <if test= "Email! = NULL" > Email </if> <if test= "phone ! = NULL "> Phone, </if> <if test=" Addr! = null "> addr, </if> <i F test= "state! = NULL" > state, </if> <if test= "Code! = NULL" > Code, </if&    Gt        </trim> <trim prefix= "VALUES (" suffix= ")" suffixoverrides= "," > <if test= "UID! = null" >      #{uid,jdbctype=integer}, </if> <if test= "Username! = null" > #{username,jdbctype=varchar},  </if> <if test= "Password! = null" > #{password,jdbctype=varchar}, </if> <if test= "Name! = Null "> #{name,jdbctype=varchar}, </if> <if test=" Email! = NULL "> #{email,jdbctype=v Archar}, </if> <if test= "Phone! = null" > #{phone,jdbctype=varchar}, </if> &L        T;if test= "Addr! = null" > #{addr,jdbctype=varchar}, </if> <if test= "state! = NULL" >      #{state,jdbctype=integer}, </if> <if test= "Code! = NULL" > #{code,jdbctype=varchar}, </if> </trim> </insert> <update id= "updatebyprimarykeyselective" parametertype= "model. User "> Update user <set > <if test=" username! = NULL "> username = #{username,jdbctype=v Archar}, </if> <if test= "Password! = null" > Password = #{password,jdbctype=varchar}, &L t;/if> <if test= "Name! = NULL" > name = #{name,jdbctype=varchar}, </if> <if test= " Email! = NULL "> Email = #{email,jdbctype=varchar}, </if> <if test= "Phone! = NULL" > Phone = #{phone,jdbctype=varchar }, </if> <if test= "Addr! = null" > addr = #{addr,jdbctype=varchar}, </if> &lt If test= "state! = NULL" > state = #{state,jdbctype=integer}, </if> <if test= "Code! = NULL" &        Gt  Code = #{code,jdbctype=varchar}, </if> </set> where uid = #{uid,jdbctype=integer} </update> <update id= "Updatebyprimarykey" parametertype= "model.      User "> Update user set username = #{username,jdbctype=varchar}, password = #{password,jdbctype=varchar}, name = #{name,jdbctype=varchar}, email = #{email,jdbctype=varchar}, phone = #{phone,jdbctype=varchar}, a DDR = #{addr,jdbctype=varchar}, state = #{state,jdbctype=integer}, code = #{code,jdbctype=varchar} where UID = #{uid,jdbctype=integer} </update></mapper>

Usermapper.java

Package Dao;import model. User;public interface Usermapper {    int deletebyprimarykey (Integer uid);    int Insert (User record);    int insertselective (User record);    User Selectbyprimarykey (Integer uid);    int updatebyprimarykeyselective (User record);    int Updatebyprimarykey (User record);}

  

Automatic generation of DAO, Model, mapping related files using Mybatis-generator

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.