These two days need to use the MyBatis code automatically generated functionality, because MyBatis belongs to a semi-automatic ORM framework, so the main task is to configure mapping mapping file, but because the handwriting map file is very error-prone, you can use the MyBatis generator to automatically generate entity classes, DAO interface and mapping mapping file. This can save a lot of effort, copy the generated code to project engineering.
There are many ways to use auto-generation, and you can install plug-ins in eclipse, but the following is the way I think it is easy, the simplest, no plug-ins, just the next few jar packs, put them under a directory.
Generate the required files and jar packages for your code:
(File: http://download.csdn.net/detail/u012909091/7206091)
There are jar packages for the MyBatis framework, database driver jar packages, and MyBatis generator jar packages. The Generatorconfig.xml is a file that needs to be configured by us, configured as follows:
[HTML]View Plaincopy print?
- <? 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.1.1/db_124 " userid=" dem " password=" dem ">
- </jdbcconnection>
- <javatyperesolver>
- <property name="forcebigdecimals" value= "false"/>
- </javatyperesolver>
- <!--generate the package name and location of the model--
- <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 mapping file--
- <sqlmapgenerator targetpackage="test.mapping" targetproject="src">
- <property name="enablesubpackages" value="true"/>
- </sqlmapgenerator>
- <!--Generate DAO's package name and location--
- <javaclientgenerator type="Xmlmapper" targetpackage= "test. Idao " targetproject="src ">
- <property name="enablesubpackages" value="true"/>
- </javaclientgenerator>
- <!--the table TableName to be generated is the table name or view name in the database Domainobjectname is the entity class name--
- <table < span class= "attribute" >tablename= "user_info_t" Domainobjectname= "User" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" ></table >
- </Context>
- </generatorconfiguration>
When these are completed, simply open the console and enter the Lib directory to execute the script:
Java-jar Mybatis-generator-core-1.3.2.jar-configfile Generatorconfig.xml-overwrite
Can.
So after the build, you can find the corresponding folder in the SRC directory, each table will correspond to three files (entity class, interface, configuration file).
Automatically create code using MyBatis generator