Mybatis generator代码生成器的使用(Mybatis generator+springboot结合)
generator.xml配置文件
<?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>
<!-- mysql数据源配置文件路径 -->
<properties resource="application.properties"/>
<!--targetRuntime="MyBatis3":会生成example文件,targetRuntime="MyBatis3BySample":不会生成example文件-->
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<!--autoDelimitKeywords,当表名或者字段名为SQL关键字的时候,可以设置该属性为true,
MBG会自动给表名或字段名添加分隔符-->
<property name="autoDelimitKeywords" value="true"/>
<!--beginningDelimiter和endingDelimiter的默认值为双引号("),
在Mysql中不能这么写,所以还要将这两个默认值改为反单引号(`)-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<!--阻止生成的注释包含时间戳-->
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${db.url}"
userId="${db.username}"
password="${db.password}">
</jdbcConnection>
<!--
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
</javaTypeResolver>
<!-- 实体类生成路径 -->
<javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>
<!-- XML生成路径 -->
<sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
<!-- 接口生成路径 -->
<javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}"
type="XMLMAPPER"/>
<!--生成所有表-->
<!--<table tableName="%">
<property name="useActualColumnNames" value="true"/>
</table>-->
<!--生成单个表-->
<table tableName="t_geo_test" enableCountByExample="true">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ld-study</artifactId>
<groupId>com.ld</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ld-mybatis-generator</artifactId>
<properties>
<!-- Java接口和实体类生成路径
targetMapperPackage是生成的接口包名
targetModelPackage是生成的实体类包名
-->
<targetJavaProject>${basedir}/src/main/java</targetJavaProject>
<targetMapperPackage>com.ld.mapper</targetMapperPackage>
<targetModelPackage>com.ld.model</targetModelPackage>
<!-- XML生成路径 -->
<targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject>
<targetXMLPackage>mapper</targetXMLPackage>
</properties>
<dependencies>
<!-- 因为我的项目是spring boot项目,非spring boot项目改成mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- MBG插件 -->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<!-- MBG配置文件路径 -->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
application.properties
jdbc.driver=com.mysql.jdbc.Driver
#数据库连接字符串
db.url=jdbc:mysql://127.0.0.1:3306/qcx_test?useUnicode=true&characterEncoding=utf8&useSSL=false
db.username=root
db.password=root
注意
如果想同时生成example文件,修改context标签:
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
targetRuntime=“MyBatis3”:会生成example文件,targetRuntime=“MyBatis3BySample”:不会生成example文件
站在墙头上 发布了10 篇原创文章 · 获赞 0 · 访问量 97 私信 关注