The Automatic Generation of mybatis code is required in the past two days. Because mybatis is a semi-automatic ORM framework, the main task is to configure the mapping ing file, however, because handwritten ing files are prone to errors, you can use the mybatis generator to automatically generate object classes, Dao interfaces, and mapping ing files. This saves a lot of effort to copy the generated code to the project.
There are many ways to use automatic generation. you can install plug-ins in eclipse. However, I think this method is easy and simple, and you do not need to install plug-ins, you only need to put the following JAR packages in a directory.
Generate the files and jar packages required by the Code:
(File: http://download.csdn.net/detail/u012909091/7206091)
The jar package of the mybatis framework, the jar package of the database driver, and the jar package of the mybatis generator. The generatorconfig. xml file must be configured as follows:
[HTML]View plaincopyprint?
- <? 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.1.25-bin.jar"/>
- <Context ID = "db2tables" targetruntime = "mybatis3">
- <Commentgenerator>
- <Property name = "suppressdate" value = "true"/>
- <! -- Whether to remove automatically generated comments true: Yes: false: No -->
- <Property name = "suppressallcomments" value = "true"/>
- </Commentgenerator>
- <! -- Database link URL, user name, password -->
- <Jdbcconnection driverclass = "com. MySQL. JDBC. Driver" connectionurl = "JDBC: mysql: // 125.221.225.113/db_demao" userid = "Demao" Password = "Demao">
- </Jdbcconnection>
- <Javatyperesolver>
- <Property name = "forcebigdecimals" value = "false"/>
- </Javatyperesolver>
- <! -- Generate the model package name and location -->
- <Javamodelgenerator targetpackage = "test. Domain" targetproject = "src">
- <Property name = "enablesubpackages" value = "true"/>
- <Property name = "trimstrings" value = "true"/>
- </Javamodelgenerator>
- <! -- Generate the package name and location of the ing file -->
- <Sqlmapgenerator targetpackage = "test. Mapping" targetproject = "src">
- <Property name = "enablesubpackages" value = "true"/>
- </Sqlmapgenerator>
- <! -- Generate the package name and location of Dao -->
- <Javaclientgenerator type = "xmlmapper" targetpackage = "test. idao" targetproject = "src">
- <Property name = "enablesubpackages" value = "true"/>
- </Javaclientgenerator>
- <! -- The tablename of the table to be generated is the table name or view name in the database. domainobjectname is the object class name. -->
- <Table tablename = "user_info_t" domainobjectname = "user" metadata = "false" metadata = "false" enabledeletebyexample = "false" enableselectbyexample = "false" selectbyexamplequeryid = "false"> </ table>
- </Context>
- </Generatorconfiguration>
After the preceding steps are completed, you only need to open the console, enter the lib directory, and execute the script:
Java-jar mybatis-generator-core-1.3.2.jar-configfile generatorconfig. XML-overwrite
You can.
In this way, you can find the corresponding folder under the src directory. Each table corresponds to three files (entity classes, interfaces, and configuration files ).
Use mybatis generator to automatically create code