MyBatis官方生成器
1. 依赖插件
-
创建generator模块
-
在父模块中进行版本管理
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency>
-
在common模块中添加依赖:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency>
- 在generator引入依赖和插件,插件根据
src/main/resources/generator-config-member.xml
等配置文件生成xml文件和Mapper接口,多个模块可以写多个配置文件生成多个xml文件和接口。<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <configurationFile>src/main/resources/generator-config-member.xml</configurationFile> <configurationFile>src/main/resources/generator-config-business.xml</configurationFile> <configurationFile>src/main/resources/generator-config-batch.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies> </plugin> </plugins> </build>
2. 配置文件
generator-config-member.xml
生成train_member数据库下对应表的xml文件和Mapper接口,注意连接数据库的用户名、密码是否正确。配置文件指定了XML、Mapper接口生成的位置。
<?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>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 自动检查关键字,为关键字增加反引号 -->
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--覆盖生成XML文件-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<!-- 生成的实体类添加toString()方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 不生成注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 配置数据源,需要根据自己的项目修改 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/member?serverTimezone=Asia/Shanghai"
userId="root"
password="root">
</jdbcConnection>
<!-- domain类的位置 targetProject是相对pom.xml的路径-->
<javaModelGenerator targetProject="../member/src/main/java"
targetPackage="com.lzk.member.domain"/>
<!-- mapper xml的位置 targetProject是相对pom.xml的路径 -->
<sqlMapGenerator targetProject="../member/src/main/resources"
targetPackage="mapper"/>
<!-- mapper类的位置 targetProject是相对pom.xml的路径 -->
<javaClientGenerator targetProject="../member/src/main/java"
targetPackage="com.lzk.member.mapper"
type="XMLMAPPER"/>
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<table tableName="member" domainObjectName="Member"/>
<table tableName="passenger" domainObjectName="Passenger"/>
</context>
</generatorConfiguration>
3. 启动插件
启动完插件后会在配置文件指定的位置下生成Mapper接口和XML文件以及实体类、条件构造实体类Example:
4. 配置文件
通过插件生成的Mapper接口和XML文件是MyBatis中的,因此需要在各个对应模块(如果有多个模块)下导入MaBatis依赖和MySQL依赖。因为需要连接数据库,所以需要配置,配置XML路径:
server.port=8001
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/member?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# mybatis xml路径
mybatis.mapper-locations=classpath:/mapper/**/*.xml
logging.level.com.jiawa.train.member.mapper=trace
5. 包扫描
在启动类上进行包扫描@MapperScan("com.lzk.member.mapper")
,配置接口:
@SpringBootApplication
@ComponentScan("com.lzk")
@MapperScan("com.lzk.train.member.mapper")
public class MemberApplication {
public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
}
}