IntelijIdea 使用maven自动生成MyBatis代码
1. pom文件中引入依赖
a.在plugins加入以下代码
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
b.在dependencies加以下代码
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
2. 在resources目录下创建generatorConfig.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> <!--数据库驱动路径 -->
<classPathEntry
location="/Users/liting/workSpace/IntelijIDEAPto/maven/Repository/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 使用criteria代替Examples自动生成结尾 如这里会在dao下生成MyInfoCriteria 否则默认的为MyInfoExample-->
<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
<property name="searchString" value="Example$" />
<property name="replaceString" value="Criteria" />
</plugin>
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/bakery?serverTimezone=GMT%2B8" userId="root" password="960218lee">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--域模型层,生成的目标包,项目目标源文件 -->
<javaModelGenerator targetPackage="com.bakery.application.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--XML映射文件,生成的位置(目标包),源代码文件夹 -->
<sqlMapGenerator targetPackage="mapping"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--XML对应的Mapper类 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.bakery.application.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--下面是数据库表名和项目中需要生成类的名称,建议和数据库保持一致,如果有多个表,添加多个节点即可 -->
<table tableName="bky_count" domainObjectName="VisitCount"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true">
</table>
</context>
</generatorConfiguration>