MyBatis Learn 4---Use mybatis_generator to generate DTO, Dao, Mapping

Source: Internet
Author: User
Tags generator

Because MyBatis belongs to a semi-automatic ORM framework, the main job will be to write the mapping mapping file, but because the handwriting mapping file is prone to error, the data discovery has off-the-shelf tools to automatically generate the underlying model classes, DAO interface classes, and even mapping mapping files.

I. Establishment of the table structure

CREATE TABLE ' user ' (
' ID ' varchar not NULL,
' username ' varchar CHARACTER SET UTF8 COLLATE utf8_bin DEFAULT NULL,
' Password ' varchar DEFAULT NULL,
' Email ' varchar DEFAULT NULL,
' Name ' varchar (#) DEFAULT NULL,
' Sex ' varchar (2) DEFAULT NULL,
' Birthday ' varchar DEFAULT NULL,
' Address ' varchar (+) DEFAULT NULL,
' Tel ' varchar (%) DEFAULT NULL,
' QQ ' varchar (%) DEFAULT NULL,
' Image ' varchar DEFAULT NULL,
' SFJH ' varchar (1) DEFAULT NULL,
' SFZX ' varchar (1) DEFAULT NULL,
' SFHF ' varchar (1) DEFAULT NULL,
' SFPL ' varchar (1) DEFAULT NULL,
' SFFX ' varchar (1) DEFAULT NULL,
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf-8;

Second, download Mybatis-generator-core

Enter: http://code.google.com/p/mybatis/

Select Downloads, then select MyBatis Generator Tool download.

Iii. Build configuration file

Create a new empty XML configuration file, the name can be arbitrarily taken, here in the name of Generatorconfig.xml. It is best to put this file in the downloaded Lib directory, as shown in figure:


One of the MySQL driver can be casually placed in a non-Chinese path, where in order to facilitate the Lib directory.

The most important thing about automatic generation is the writing of configuration files, and now we're going to start with the specifics of the Generatorconfig.xml file:

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis generator Configuration" "1.0//en	G/dtd/mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <!--database driver--> <classpathentry location= "Mysql-connector-java-5.0.6-bin.jar"/> <context id= "db2tables" targetruntime= "MyBatis3" > <
			commentgenerator> <property name= "Suppressdate" value= "true"/> <!--remove automatically generated comments true: false: No-->
		<property name= "Suppressallcomments" value= "true"/> </commentGenerator> <!--database link URL, username, password--> <jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "jdbc:mysql://localhost/test" userid= "test" password= "Test" > </jdbcConnection> <javaTypeResolver> <property name= "forcebigdecimals" value= "FA LSE "/> </javaTypeResolver> <!--build the package name and location of the model--> <javamodelgenerator targetpackage=" Test.model "targetproject= "src" > <property name= "enablesubpackages" value= "true"/> <property the Name= "Trimstrings" value=  ' True '/> </javaModelGenerator> <!--generate the package name and location of the mapping file--> <sqlmapgenerator targetpackage= "Test.mapping" targetproject= "src" > <property name= "enablesubpackages" value= "true"/> </sqlMapGenerator> <!-- The package name and location of the DAO--> <javaclientgenerator type= "Xmlmapper" targetpackage= "Test.dao" targetproject= "src" > < Property Name= "Enablesubpackages" value= "true"/> </javaClientGenerator> <!--which tables to generate--> <table tabl Ename= "About" domainobjectname= "Aboutdto" enablecountbyexample= "false" enableupdatebyexample= "false" Enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid= "false" ></table> < Table tablename= "User" domainobjectname= "Userdto" enablecountbyexample= "false" enableupdatebyexample= "false" Enabledeletebyexample= "false" enableselectbyexample= "false" SelectbyexamPlequeryid= "false" ></table> <table tablename= "syslogs" domainobjectname= "Syslogsdto" Enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "
 False "selectbyexamplequeryid= false" ></table> </context> </generatorConfiguration>

1, there are database drivers, database URL, user name, password, the package name and location of the generated model, the package name and location of the generated mapping file, the package name and location of the DAO, and the name of the table and the corresponding class name that you want to generate.


Four, the operation

Need to run through the cmd command-line, you can first prepare a running script, where the script is: Java-jar mybatis-generator-core-1.3.2.jar-configfile generatorconfig.xml -overwrite

Note that: Mybatis-generator-core-1.3.2.jar for the corresponding version of the download jar,generatorconfig.xml for the configuration file name, if not for this can be modified here.

Start cmd into the "F:\soft\mybatis-generator-core-1.3.2\lib" directory, as shown in the figure:


The build succeeds to the SRC directory, and you can see that the corresponding model, DAO, and mapping have been generated, as shown in the figure:


Below you can look at the generated 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= "Test.dao.UserDtoMapper" > <resultmap id= "baseresultmap" type= "Test.model.UserDto" > <i D column= "id" property= "id" jdbctype= "VARCHAR"/> <result column= "username" property= "username" jdbctype= "Varcha" R "/> <result column=" password "property=" password "jdbctype=" VARCHAR "/> <result" email column= " rty= "Email" jdbctype= "VARCHAR"/> <result column= "name" property= "name" jdbctype= "VARCHAR"/> <result column= "Sex" property= "Sex" jdbctype= "VARCHAR"/> <result column= "Birthday" property= "Birthday" jdbctype= "VARCH" AR "/> <result column=" Address "property=" Address "jdbctype=" VARCHAR "/> <result The" tel "property = "Tel" jdbctype= "VARCHAR"/> <result column= "QQ" property= "QQ" jdbctype= "VARCHAR"/> &LT;result column= "image" property= "image" Jdbctype= "VARCHAR"/> <result column= "Sfjh" property= "Sfjh" jdbctype= "V" Archar "/> <result column=" sfzx "property=" SFZX "jdbctype=" VARCHAR "/> <result" column= "SFHF" property= "SFHF" jdbctype= "VARCHAR"/> <result column= "SFPL" property= "SFPL" jdbctype= "VARCHAR"/> <result N= "SFFX" property= "SFFX" jdbctype= "VARCHAR"/> </resultMap> <sql id= "Base_column_list" > IDs, Userna Me, password, email, name, sex, Birthday, address, tel, QQ, image, Sfjh, SFZX, SFHF, SFPL, SFFX </sql> <  Select Id= "Selectbyprimarykey" resultmap= "Baseresultmap" parametertype= "java.lang.String" > select <include Refid= "Base_column_list"/> from user where id = #{id,jdbctype=varchar} </select> <delete id= "Del Etebyprimarykey "parametertype=" java.lang.String > Delete from user where id = #{id,jdbctype=varchar} </d Elete> <insert Id= "Insert" parametertype= "test.model.UserDto" > INSERT into User (ID, username, password, email, name, sex, Birthday, Address, tel, QQ, image, Sfjh, SFZX, SFHF, SFPL, SFFX) VALUES (#{id,jdbctype=varchar}, #{username,jdbctype=varchar}, #{password,jdbctype=varchar}, #{email,jdbctype=varchar}, #{name,jdbcType=VARCHAR}, # {Sex,jdbctype=varchar}, #{birthday,jdbctype=varchar}, #{address,jdbctype=varchar}, #{tel,jdbctype=varchar}, #{qq,j Dbctype=varchar}, #{image,jdbctype=varchar}, #{sfjh,jdbctype=varchar}, #{sfzx,jdbctype=varchar}, #{sfhf,jdbcType=V Archar}, #{sfpl,jdbctype=varchar}, #{sffx,jdbctype=varchar}) </insert> <insert id= "Insertselective" pa
      Rametertype= "Test.model.UserDto" > insert INTO User <trim prefix= "(" suffix= ")" suffixoverrides= "," > 
      <if test= "id!= null" > ID, </if> <if test= "username!= null" > Username, </if> &LT;if test= "password!= null" > Password, </if> <if test= "email!= null" > Email , </if> <if test= "name!= null" > Name, </if> <if test= "Sex!= null" > Sex </if> <if test= "birthday!= null" > Birthday, </if> &l
      T;if test= "address!= null" > Address, </if> <if test= "tel!= null" > Tel,
        </if> <if test= "qq!= NULL" > QQ, </if> <if test= "image!= Null" > Image, </if> <if test= "sfjh!= null" > Sfjh, </if> <if test= "Sfz
      x!= Null "> Sfzx, </if> <if test=" SFHF!= null "> SFHF, </if>
      <if test= "SFPL!= null" > SFPL, </if> <if test= "sffx!= null" > SFFX, </if> </trim> <trim prefix= "VALUES (" suffix= ")" suffixoverrides= "," > <if test= "id!= null" > #{i
      D,jdbctype=varchar}, </if> <if test= "username!= null" > #{username,jdbctype=varchar}, </if> <if test= "password!= null" > #{password,jdbctype=varchar}, </if>
        ; if test= "email!= null" > #{email,jdbctype=varchar}, </if> <if test= "name!= null" >
      #{name,jdbctype=varchar}, </if> <if test= "sex!= null" > #{sex,jdbctype=varchar}, </if> <if test= "birthday!= null" > #{birthday,jdbctype=varchar}, </if> & Lt;if test= "address!= null" > #{address,jdbctype=varchar}, </if> <if test= "tel!= null" &
        Gt #{tel,jdbctype=varchar}, </if> <if test= "qq!= null" > #{qq,jdbctype=varchar}, </
     If> <if test= "image!= null" > #{image,jdbctype=varchar}, </if> <if test= "sfjh!= null" &G
        T
      #{sfjh,jdbctype=varchar}, </if> <if test= "sfzx!= null" > #{sfzx,jdbctype=varchar}, </if> <if test= "SFHF!= null" > #{sfhf,jdbctype=varchar}, </if> <if test= "s FPL!= null "> #{sfpl,jdbctype=varchar}, </if> <if test=" sffx!= null "> #{sffx , Jdbctype=varchar}, </if> </trim> </insert> <update id= "Updatebyprimarykeyselective" pa Rametertype= "test.model.UserDto" > Update user <set > <if test= "username!= null" > U Sername = #{username,jdbctype=varchar}, </if> <if test= "password!= null" > Password = #{p Assword,jdbctype=varchar}, </if> <if test= "email!= null" > Email = #{email,jdbctype=varch AR}, &Lt;/if> <if test= "name!= null" > name = #{name,jdbctype=varchar}, </if> <if te
        st= "Sex!= null" > Sex = #{sex,jdbctype=varchar}, </if> <if test= "birthday!= null" >  Birthday = #{birthday,jdbctype=varchar}, </if> <if test= ' address!= null ' > Address = #{address,jdbctype=varchar}, </if> <if test= "tel!= null" > Tel = #{tel,jdbctype=varcha R}, </if> <if test= "qq!= null" > QQ = #{qq,jdbctype=varchar}, </if> ; if test= "image!= null" > Image = #{image,jdbctype=varchar}, </if> <if test= "Sfjh!= null "> Sfjh = #{sfjh,jdbctype=varchar}, </if> <if test=" sfzx!= null "> SFZX = #{s
      Fzx,jdbctype=varchar}, </if> <if test= "SFHF!= null" > SFHF = #{sfhf,jdbctype=varchar}, </if> &LT;if test= "SFPL!= null" > SFPL = #{sfpl,jdbctype=varchar}, </if> <if test= "sffx!= null" > SFFX = #{sffx,jdbctype=varchar}, </if> </set> where id = #{id,jdbctype=varchar} & lt;/update> <update id= "Updatebyprimarykey" parametertype= "test.model.UserDto" > Update user set Userna 
      me = #{username,jdbctype=varchar}, password = #{password,jdbctype=varchar}, email = #{email,jdbctype=varchar}, name = #{name,jdbctype=varchar}, sex = #{sex,jdbctype=varchar}, birthday = #{birthday,jdbctype=varchar}
      , address = #{address,jdbctype=varchar}, tel = #{tel,jdbctype=varchar}, QQ = #{qq,jdbctype=varchar},
      Image = #{image,jdbctype=varchar}, Sfjh = #{sfjh,jdbctype=varchar}, sfzx = #{sfzx,jdbctype=varchar},  SFHF = #{sfhf,jdbctype=varchar}, SFPL = #{sfpl,jdbctype=varchar}, sffx = #{sffx,jdbctype=varchar} where ID = #{id,jdbctype=varchar} </update> </mapper> 

You can then copy the three directories to the directory of the corresponding project, and modify the DAO class if you need to add your own method.

More articles See: http://www.16boke.com/article/detail/108


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.