我想在MyBatis Generator中使用Java8DateTime和Lombok!
序言
在使用Spring Boot时,经常会与MyBatis同时使用,同时也经常使用MyBatisGenerator。
然而,关于将Lombok应用于MyBatisGenerator的Getter/Setter类的示例并不多,因此我总结了一些。
环境
-
- Mac 10.15.1 (Catalina)
-
- Java 8
-
- MyBatis Generator 1.3.7
-
- Maven 3系
- MySQL 8.0
我使用了之前在 Mac 上用 Docker 构建的日本语环境 MySQL 8.0 的文章中创建的 MySQL。
操作系统可以是 Windows 或 Linux,都没有问题。
事先准备
在适当的数据库实例中创建表。
在这里,我们按照前面提到的环境,在MySQL上创建了表。
请参考以下链接
- MyBatis Generator Lombok plugin
下面的步骤基本上与上述插件的GitHub手动操作步骤几乎一致。
设定文件
以下是pom.xml和用于MyBatisGenerator的Xml文件的设置示例。
虽然包含了SpringBoot的Web设置,但并没有特别的意义。
对于pom,请参考注释行附近。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- Mybatis Generator用 -->
<mybatis.generator.configurationFile>${basedir}/src/main/resources/generatorConfig.xml</mybatis.generator.configurationFile>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Mybatis Generator用 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>com.softwareloop</groupId>
<artifactId>mybatis-generator-lombok-plugin</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
<?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="プロジェクトディレクトリ配下に配置したDB接続用のjarファイルのパスを記載する(ex: lib/mysql-connector-java-8.0.18.jar)" />
<context id="MySQLTables" targetRuntime="MyBatis3">
<plugin type="com.softwareloop.mybatis.generator.plugins.LombokPlugin">
</plugin>
<commentGenerator>
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:13306/testDb"
userId="test"
password="password" />
<javaTypeResolver>
<property name="useJSR310Types" value="true" />
</javaTypeResolver>
<javaModelGenerator
targetPackage="Model(java)の配置先パッケージを記載する"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaModelGenerator>
<javaClientGenerator
targetPackage="Mapper(java)の配置先パッケージを記載する"
targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<sqlMapGenerator
targetPackage="Mapper(xml)の配置先パッケージを記載する"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<table tableName="%"
enableInsert="true"
enableSelectByPrimaryKey="true"
enableSelectByExample="true"
enableUpdateByPrimaryKey="true"
enableUpdateByExample="true"
enableDeleteByPrimaryKey="true"
enableDeleteByExample="true"
enableCountByExample="true"
selectByExampleQueryId="true"
modelType="flat">
</table>
</context>
</generatorConfiguration>
请注意
我在Maven中确认了generatorConfig.xml。在Eclipse插件MyBatis Generator中,我注意到generatorConfig.xml中的targetProject路径与在Maven执行时不同。请注意。