mybatis:mybatis-generator插件使用
1 idea配置
idea,点击File->Settings->Plugins->设置,点击:Manage Plugin Repositories:
配置如下:
http://plugins.jetbrains.com/
下载插件并重启idea:
2 使用
参考官方文档mybatis-generator插件使用方式:
http://mybatis.org/generator/configreference/xmlconfig.html
pom.xml,增加mybatis-generator插件配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.4</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/mybatis-generator/mybatisGenerator.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
mybatis-generator插件配置文件:mybatisGenerator.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>
<!-- 连接数据库jar包的路径-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- When the property is true, no timestamp will be added to the generated comments.-->
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<!-- <property name="suppressAllComments" value="true"/>-->
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!--数据库连接信息:驱动类、链接地址、用户名、密码 -->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/fruitmall?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC"
userId="root"
password="pwd">
<!--高版本的 mysql-connector-java 需要设置 nullCatalogMeansCurrent=true-->
<!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver>
<!--类型解析器-->
<!-- 默认false,把jdbc decimal 和 numeric 类型解析为integer -->
<!-- true,把jdbc decimal 和 numeric 类型解析为java.math.bigdecimal-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体类的包名和存放路径 -->
<javaModelGenerator targetPackage="com.xiaoxu.repo.dao" targetProject="src/main/java">
<!-- 是否让schema作为包后缀 默认是false会在 po 目录下在创建一个 “数据库名” 的文件夹,生成 的 po 会放在该文件夹下,也就是说会多一层目录
-->
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格-->
<!-- <property name="trimStrings" value="true"/>-->
<property name="trimStrings" value="false"/>
</javaModelGenerator>
<!-- 生成映射文件*.xml的位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成Mapper接口的包名和位置
type="XMLMAPPER" 会将接口的实现放在 mapper.xml中,也推荐这样配置。
type="ANNOTATEDMAPPER",接口的实现通过注解写在接口上面
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.xiaoxu.repo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="my_fruit"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample = "false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
最后,双击执行如下的mybatis-generator:generate即可