In the process of learning mybatis, it is found that MyBatis is really a very useful framework, but the handwriting map file is very error-prone, so you can use the MyBatis generator to automatically generate entity classes, DAO interfaces, and mapping mapping files. This can save a lot of effort, copy the generated code to project engineering. Of course, there are similar plugins on eclipse, but I chose generator because it's convenient, just need to download a jar package, here's the build process:
My directory structure is as follows:
First: We need to download Generator's jar package download address: Http://pan.baidu.com/s/1gdm4rfX
Then, you need a database-driven jar package. I'm using MySQL here, give the download address:
http://pan.baidu.com/s/1gdIs00b
The next step is to create the config file, and here I am named Generator.xml,
The following is my configuration file
<?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 package Location-<! --<classpathentry location= "D:\software\lib\mysql-connector-java-5.1.21.jar"/>--<classpathentry
location= "D:\auto\mysql-connector-java-5.1.36.jar"/> <context id= "db2tables" targetruntime= "MyBatis3" > <commentGenerator> <property name= "suppressallcomments" value= "true"/> </commentgen erator> <!--database link URL, user name, password--<!--<jdbcconnection driverclass= "Com.mysql.jdbc.Driver" C Onnectionurl= "Jdbc:mysql://localhost:3306/sy" userid= "Sypro" password= "Sypro" >-<jdbcconnection Driver
class= "Com.mysql.jdbc.Driver" connectionurl= "Jdbc:mysql://localhost:3306/ssm1" userid= "root" password= "" > </jdbcConnection> <javaTypeResolver> <property name= "Forcebigdecimals" value= "false" /> </javaTypeResolver> <!--generate the package name and location of the model--<javamodelgenerator targetpackage= "Co M.yc.ssm.cinema.entity "targetproject=" D:\auto\src "> <property name=" enablesubpackages "value=" true "/&G
T
<property name= "Trimstrings" value= "true"/> </javaModelGenerator> <!--generated map file package name and location-- <sqlmapgenerator targetpackage= "com.yc.ssm.cinema.mapping" targetproject= "D:\AUTO\SRC" > <pro
Perty name= "Enablesubpackages" value= "true"/> </sqlMapGenerator> <!--generate DAO's package name and location--
<javaclientgenerator type= "Xmlmapper" targetpackage= "Com.yc.ssm.cinema.mapper" targetproject= "D:\AUTO\SRC" > <property name= "Enablesubpackages" value= "true"/> </javaClientGenerator> <!--to generate Those tables (change tablEname and Domainobjectname can)--<table tablename= "Filminfo" domainobjectname= "Filminfo" enablecountbyexample= "False" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid= "false"/> <table tablename= "Filmtype" domainobjectname= "Filmtype" EnableCountByExamp Le= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid= "false"/> </context> </generatorConfiguration>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Then intervene in the command line, switch to the current directory, run the command
Java-jar Mybatis-generator-core-1.3.2.jar-configfile Generator.xml-overwrite
1
As shown in figure
Results:
Next, you can copy the generated entity classes and mapping files to the project.