1. Core file generator.xml
Specify the database jar package location, database connection information, the location of the generated package, table name and other key information. The file is placed anywhere.
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!-- JDBC driver jar package address of the database--><classPathEntry location="F:/xy/jars/mysql-connector-java-5.0.7-bin.jar" /><context id="DB2Tables" targetRuntime="MyBatis3"><!-- Whether to remove the automatically generated comments--><commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator><!-- Information about database connection--><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/db_MybatisTest" userId="root" password="mysqlttest"></jdbcConnection><!-- false: JDBC DECIMAL, NUMERIC types resolve to Integer, default method--><!-- true: JDBC DECIMAL and NUMERIC types resolve to java.math.BigDecimal --><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- The package name and location of the generated model --><javaModelGenerator targetPackage="com.xy.model" targetProject="F:/xy/mybatis-generator/src"><!-- Whether to let schema be the suffix of the package --><property name="enableSubPackages" value="true" /><!-- The space before and after the value returned from the database is cleaned --><property name="trimStrings" value="true" /></javaModelGenerator><!-- The package name and location of the map file --><sqlMapGenerator targetPackage="com.xy.mapping" targetProject="F:/xy/mybatis-generator/src"><property name="enableSubPackages" value="false" /></sqlMapGenerator><!-- The package name and location of the map file --><javaClientGenerator type="XMLMAPPER" targetPackage="com.xy.dao" targetProject="F:/xy/mybatis-generator/src"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- tableName: Database table--><!-- domainObjectName: javaBean class name corresponding to database table--><table tableName="t_student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"><!-- Ignore this field (can be omitted) --><ignoreColumn column="name" /></table></context></generatorConfiguration>
2. Table tag analysis
①Properties
The schema is the database name, the tableName is the corresponding database table, and the domainObjectName is the entity class to be generated.
To generate an example, set enableCountByExample, etc. to true, a Example class corresponding to domainObjectName will be generated, and false will not be generated. The default policy is true.
Similarly, enableUpdateByExample, enableDeleteByExample, enableSelectByExample, selectByExampleQueryId attributes.
②Sub-label
To operate on certain database fields, you can add the following tag to the table tag
1. Ignore a field
<ignoreColumn column="name" />
2. Regardless of the type of the database field, the generated class attribute is varchar
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
3. Generate
mybatis-generator-core-1.3.2.jar is a core jar package that can be downloaded online. The command window executes the statement. After the execution is successful, the code will be found in the specified location in the generator.xml file.
java -jar F:/xy/jars/mybatis-generator-core-1.3.2.jar -configfile F:/xy/generator.xml -overwrite
4. Summary
Using Mybatis Generator requires
①Two jar packages - mybatis-generator-core-1.3.2.jar and database jar package
②A configuration file generator.xml
③Execution statement
5. Things to note
① Generator.xml format: must be encoded in UTF-8 BOM format and converted with notepad++.
② Pay attention to the availability of database packages. Invalid database package conversion will report an error.
The above is a comprehensive analysis of the use of Mybatis generator introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!