本片文章是基于之前写的springboot集成mybatis,主要是用来自动生成mapper,mapping以及实体类entity的
- 从pom.xml文件中导入 generator 插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--自动生成mapper插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
- 在项目resource目录下创建 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>
<!--这里的loacation是你连接数据库的jar包,可以从项目依赖里面找-->
<classPathEntry location="C:\Users\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar"/>
<context id="context1">
<!-- 是否生成注释 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库连接,数据库版本高的话需要添加时区serverTimezone=GMT%2B8 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/shop_mall?serverTimezone=GMT%2B8"
userId="root" password="root" />
<!-- 生成的包名和工程名 -->
<javaModelGenerator targetPackage="com.lover.mybatistest.entity"
targetProject="src/main/java/"/>
<!-- xml映射文件 -->
<sqlMapGenerator targetPackage="mapping"
targetProject="src/main/resources/" />
<!-- mapper接口 -->
<javaClientGenerator targetPackage="com.lover.mybatistest.mapper"
targetProject="src/main/java/" type="XMLMAPPER" />
<!-- 数据库表 以及是否生成example,可以同时生成多个-->
<table tableName="tb_item_desc" domainObjectName="TbItemDesc"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="tb_user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
-
点击idea右侧 Maven Projects,双击下面的就可以自动生成了
最后在生成的mapper中,增加一个注解 @Repository ,就可以正常使用了