Prerequisite: you have SpringBoot engineering, and in the implementation of the above application of MyBatis, but the whole is to create mapper, xml mapping file, pojo etc. manually.
- Add MyBatis.generator dependence in the POM
<dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <executions> <execution> <id>mybatis-generator</id> <phase>deploy</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <!-- Mybatis-Generator 工具配置文件的位置 --> <configurationFile>${basedir}/src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
- Creating src / main / resources to create mybatis-generator folder, create a configuration file in the folder generatorConfig.xml
<? Xml Version = "1.0" encoding = "UTF-8" ?> <! DOCTYPE generatorConfiguration the PUBLIC "- // mybatis.org//DTD the Configuration MyBatis Generator 1.0 // EN" "http://mybatis.org/dtd /mybatis-generator-config_1_0.dtd " > ! <- Profile generator -> < generatorConfiguration > <-! command generator plug-in generates a file: Call mvn the mybatis-generator: the generate -e -> <-! - the introduction of the configuration file -> < the Properties Resource = "the Application-dev.yml" /> <-! classpathentry: JDBC database driver, into your own driving position optional -> <! -<classpathentry LOCATION = "$ {the classPath}" /> -> <-! a database context -> <-! defaultModelType = "Flat" large data field, regardless of the table -> < context ID = " MysqlTables " targetRuntime =" MyBatis3Simple " defaultModelType =" Flat " > <-! automatic identification database key, default false, if set to true, according to the keyword list SqlReservedWords defined; generally leave the default value, the keyword database encountered (Java keyword), covering the use of columnOverride -> < Property name = "autoDelimitKeywords" value = "to true" /> <-! generate Java code files -> <property name= "javaFileEncoding" value = "UTF-. 8" /> <-! beginningDelimiter and endingDelimiter: symbols used to mark the database object name specified in the database, such that double quotes ORACLE, MYSQL Based default is `backquote; -> < Property name = "beginningDelimiter" value = "` " /> < Property name =" endingDelimiter " value =" `" /> <-! format java code -> < Property name = "javaFormatter" value = "ORG. mybatis.generator.api.dom.DefaultJavaFormatter " /> <-! format XML code -> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 注释 --> <commentGenerator > <property name="suppressAllComments" value="false"/><!-- 是否取消注释 --> <property name="suppressDate" value="true" /> <!--Note Generation whether to generate timestamp -> </ commentGenerator > <! - JDBC connection -> <! - used as a configuration using the following * .properties link string -> <! - <JdbcConnection driverClass = "$ spring.datasource.driver-class-name {} " the connectionURL =" $ {} spring.datasource.url " the userId =" $ {} spring.datasource.username " password =" $ {spring.datasource.password} "/> -> <! - used as a configuration using the following * .yml link string -> < JdbcConnection driverClass = "$ {Driver}-class-name" connectionURL= "URL $ {}" the userId = "$ {} username" password = "{$ password}" /> <-! cast -> < javaTypeResolver > <-! whether bigDecimal, false or less can be automatically converted type (Long, Integer, Short, etc.) -> < Property name = "forceBigDecimals" value = "to false" /> </ javaTypeResolver > <-! generating entity classes address -> < javaModelGenerator targetPackage = "$ { } aliases-Package-type " targetProject ="${project}" > <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="mapping" targetProject="${resources}" > <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!- javaClientGenerator<->generating a corresponding mapxml client, i.e. the interface DAO targetPackage = "com.cs.mapper" targetProject = "$ {} Project" type = "XMLMAPPER" > < Property name = "enableSubPackages" value = "to false" /> </ javaClientGenerator > <-! Table may have a plurality of each table in the database can write a table, tableName pledged to match the database table can also be used to match the tableName property by using wildcards% of all database tables, only the matching table will automatically generate a file -> < Table tableName = "Employee" enableCountByExample = "to true" enableUpdateByExample = "to true" enableDeleteByExample = "to true" enableSelectByExample="true" selectByExampleQueryId="true"> <property name="useActualColumnNames" value="false" /> <!-- 数据库表主键 --> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> </context> </generatorConfiguration>
Note: The path where the $ Application Note, specify the path targetProject to start the project name, for example: MyProject / src / main / java
- Create generating formulation package, DAO interface generation at src / main / java / main package / mapper bag, map the xml generated under src / main / resources / mapping, the generated entity classes in src / main / java / main package / under pojo
- In generatorConfig.xml right click, Run As -> Run MyBatis Generator, generating template code
- Create a controller class and services in their own class, you can achieve
Note: One path generatorConfig.xml need to call attention to match the application configuration file