下面是SpringBoot整合mybatis-generator插件的详细攻略,我们将分为以下几个步骤进行操作:
- 添加mybatis-generator插件依赖
- 配置mybatis-generator插件
- 配置生成代码的输出路径和文件名
- 自动生成代码
- 示例展示
1. 添加mybatis-generator插件依赖
首先,我们需要在项目中添加mybatis-generator插件的依赖。在pom.xml文件中添加以下依赖:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
2. 配置mybatis-generator插件
在pom.xml文件中添加以下这段代码,用来配置mybatis-generator插件:
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>deploy</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 指定需要使用的数据库连接的配置文件 -->
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<!-- 如果需要在动态 SQL 中使用可替换的字段,可以在此处指定对应的 property -->
<properties>
<property>
<name>useExample</name>
<value>false</value>
</property>
</properties>
<!-- 输出日志 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
3. 配置生成代码的输出路径和文件名
在项目中添加一个generator文件夹,然后在其中创建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>
<context id="Mysql" targetRuntime="MyBatis3">
<!-- 配置数据源,可以配置多个数据源 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"
userId="root"
password="root">
</jdbcConnection>
<!-- 配置 domain model 类所在的包 -->
<javaModelGenerator targetPackage="com.example.demo.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 配置 mapper 接口所在的包 -->
<sqlMapGenerator targetPackage="com.example.demo.dao.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 配置 XML 文件所在的包 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 配置要生成的表,可以配置多个表 -->
<table tableName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"/>
<table tableName="product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
4. 自动生成代码
执行命令 mvn mybatis-generator:generate
即可生成代码。
5. 示例展示
以一个SpringBoot项目为例,我们使用上述步骤生成代码。以下是user表和product表生成的示例:
- 生成的User类
public class User {
private Integer id;
private String name;
private Integer age;
private String gender;
private String phone;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender == null ? null : gender.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address == null ? null : address.trim();
}
}
- 生成的Product类
public class Product {
private Integer id;
private String name;
private Double price;
private Integer stock;
private String description;
private String imageUrl;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description == null ? null : description.trim();
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl == null ? null : imageUrl.trim();
}
}
至此,我们成功的实现了SpringBoot整合mybatis-generator插件的自动代码生成过程。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot整合mybatis-generator插件流程详细讲解 - Python技术站