Students who have used MyBatis know that the database tables used in each project need to establish their corresponding database additions and deletions to xxxmapper.xml files, The entity class Xxx.java file and other classes are used to invoke the Xxxmapper.java file for database operations. When I started learning MyBatis, I believed that many people created these files by hand. Without a doubt, if the project is relatively large, it is very inefficient to build these files manually by hand, and we can generate these files by Mybatis-generator . However, this tool generates the relevant files by default in the form of a command line, so we can write an ant script that will be automatically generated each time we need to create these files in eclipse by executing this ant script. The complete steps are as follows:
import a related jar package
To use "Mybatis-generator", you need to import the corresponding Mybatis-generator-1.3.x.jar file in the Lib of the Web project. On GitHub: Mybatis-generator's jar pack download
Two configuring the Mybatis-generator configuration file
(1) First create a few packages in the project to hold the corresponding files:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/80/1A/wKioL1c4GwWT7LbbAABGJG6JwXc356.png "title=" 20160427150740611.png "alt=" Wkiol1c4gwwt7lbbaabgjg6jwxc356.png "/>
As can be seen, Src/main/java is used to store Java source code, Src/main/env/dev for the development environment of the configuration files (such as: JDBC, cache, log, etc.), src/main/resources for storing some common configuration files, Here our auto-generated mapper.xml files are stored in this path; Src/test/java represents the test code, no matter
Note: How do I add these source folders in eclipse?
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/80/1A/wKioL1c4GyaAKG7JAAEw43WIEzI818.png "title=" 20160427152103879.png "alt=" Wkiol1c4gyaakg7jaaew43wiezi818.png "/>
(2) Create a new generatorconfig.xml and Build_mybatis.xml under the project root directory:
These two files are "mybatis-generator" profiles and automated ant scripts, with the following paths in the project:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/1D/wKiom1c4GmKCFQa8AAAtgsRnYb8301.png "title=" 20160427153028863.png "alt=" Wkiom1c4gmkcfqa8aaatgsrnyb8301.png "/>
i) 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 driver --><classpathentrylocation= "webcontent/web-inf/lib/ Mysql-connector-java-5.1.26-bin.jar " /><context id=" Db2tables " targetRuntime=" MyBatis3 " ><commentgenerator><property name= "Suppressallcomments" value= "true" /><!-- Whether to uncomment --><property name= "Suppressdate" value= "true" /> <!-- Whether to generate comment generation timestamps --></commentGenerator><!-- database connection Information --><jdbcConnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "jdbc:mysql://127.0.0.1:3306/ehcache_db" userid= "root" password= "root" ></jdbcConnection><!-- only one belongs to Forcebigdecimals, default false. If the field is more accurate than 0, generate bigdecimal if the field is exactly 0, the total length 10-18 generates a long, if the field is exactly 0, total length 5-9 generates integer; if the field is exactly 0, Total length less than 5 build short; if Forcebigdecimals is true, unified build bigdecimal --><javatyperesolver><!-- Use Bigdecimal, false to automatically convert the following types (long, integer, short, etc.) --><property Name= "Forcebigdecimals" value= "false" /></javaTypeResolver><!--generate Model.java file -- ><javamodelgenerator targetpackage= "Cn.zifangsky.model" targetproject= "Src/main/java" ><!-- enablesubpackages: Do you want the schema to be the suffix of the package --><property name= "Enablesubpackages" value= " False /><!-- whether to trim call --><property name= "Trimstrings" for a String Type field at set time value= "true" /></javaModelGenerator><!-- generate mapper.xml files -->< Sqlmapgenerator targetpackage= "Sqlmaps" targetproject= "src/main/resources" ><!-- Enablesubpackages: Do you want the schema to be the suffix of the package --><property Name= "Enablesubpackages" value= "false" /></sqlMapGenerator><!-- Generate Mapper.java file, DAO layer --><javaclientgenerator type= "Xmlmapper" targetpackage= " Cn.zifangsky.mapper " targetproject=" Src/main/java "><property name=" Enablesubpackages " Value= "false" /></javaClientGenerator><!-- table names in the database to be generated, generating a table corresponding to the Java and XML files will need to configure a section of -- ><table tablename= "User" domainobjectname= "user" enablecountbyexample= "false" Enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid= "false" ></table></context></generatorConfiguration>
Note: Some of the areas that need to be modified can be modified by referring to my comments above, and don't forget the data-driven jar package
II) Build_mybatis.xml:
<project default= "Genfiles" basedir= "." ><property name= "Generated.source.dir" value= "${basedir}" /> <path id= "Ant.run.lib.path" > < pathelement location= "${basedir}/webcontent/web-inf/lib/mybatis-generator-core-1.3.2.jar"/> </path><target name= "Genfiles" description= "Generate the files "><taskdef name=" Mbgenerator " classname=" Org.mybatis.generator.ant.GeneratorAntTask " classpathref=" Ant.run.lib.path "/><mbgenerator overwrite= "true" configfile= "Generatorconfig.xml" verbose= "false" ><propertyset>< Propertyref name= "Generated.source.dir" /></propertyset></mbgenerator></target> </project>
The above code is two places to note: one is "Mybatis-generator" the jar package, the second is the need for corresponding "Generatorconfig.xml" file
Note: If you are not familiar with the ant script, you can refer to the following article I wrote:http://www.zifangsky.cn/444.html
three-Test
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/1D/wKiom1c4Gq2z4AHJAABkKWMktKM529.png "title=" 20160427153347941.png "alt=" Wkiom1c4gq2z4ahjaabkkwmktkm529.png "/>
When performing an effect test, simply drag the "Build_mybatis.xml" file into the ant view, then click Execute this script to automatically generate the files we need, and finally refresh the project structure to see the file, the effect is as follows:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/1A/wKioL1c4G7yCHFnZAAA-sWYpPxc552.png "title=" 20160427153722569.png "alt=" Wkiol1c4g7ychfnzaaa-swyppxc552.png "/>
> Note: I tested the database data used:
set foreign_key_checks=0;-- ------------------------------ Table structure for user-- ----------------------------drop table if exists ' user '; create table ' user ' ( ' id ' int (one) not null auto_increment, ' name ' varchar (+) DEFAULT NULL, ' password ' varchar (+) default null, ' email ' varchar DEFAULT NULL, ' birthday ' date DEFAULT NULL, PRIMARY KEY (' id ')) engine=innodb auto_increment=4 DEFAULT CHARSET=utf8;-- ------------------------------ records of user-- - ---------------------------insert into ' user ' VALUES (' 1 ', ' admin ', ' 123456 ', ' [email protected] ', ' 2000-01-02 ');insert into ' user ' VALUES (' 2 ', ') Test ', ' 1234 ', ' [email protected] ', &NBSP; ' 1990-12-12 ');insert into ' user ' VALUES (' 3 ', ' xxxx ', ' xx ', ' [email Protected] ', ' 1723-06-21 ');
This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1773597
Fast automatic generation of model, mapper and other files using mybatis-generator combined with ant script in MyBatis