使用场景
当我们使用Springboot整合Mybatis时,我们就需要为数据库中的每一个表分别写出:
如果数据库中有很多表,这个过程就会非常的繁琐。而MyBatis官方为我们提供了MyBatis Generator插件,我们只需要以下介绍的三个步骤就可以快速地生成以上三个文件,极大的简化了数据层的开发。
官网:MyBatis Generator
项目准备
以下用一个例子来演示如何在项目中使用MyBatis Generator,项目使用的环境为:
- ubuntu 18.04
- maven 3.6.3
- JDK 11
项目中使用的表如图所示
使用方法
1. 引入mybatis的依赖和MyBatis Generator插件
在pom.xml文件中加入以下两个部分
mybatis的依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
MyBatis Generator插件:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<overwrite>true</overwrite>
</configuration>
</plugin>
2. MyBatis Generator 的配置文件
MyBatis Generator配置文件默认的路径为
${basedir}/src/main/resources/generatorConfig.xml
所以我们在 recources 文件夹下新建 generatorConfig.xml 文件(如果希望保存在其他路径可以在导入插件时使用 configurationFile 标签来更改),配置文件的内容如下:
<?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>
<!-- JDBC 驱动的路径,需要自己配置 -->
<classPathEntry location="/home/XXX/.m2/repository/mysql/mysql-connector-java/8.0.21/mysql-connector-java-8.0.21.jar" />
<context id="table" targetRuntime="MyBatis3">
<!-- 不加注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"
userId="root"
password="1">
<!-- mysql8.x需要这一行 -->
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<!-- 实体类 -->
<javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- Mapper.xml文件 -->
<sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Mapper接口文件 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="db" tableName="student" domainObjectName="Student" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
配置文件需要注意的地方有以下几点:
- 通过 targetPackage 和 targetProject 配置生产成的实体类、Mapper.xml文件、Mapper接口的位置。
- 通过table标签可以用来指定数据库、表以及表对应的实体类名称
3. 运行 Mybatis Generator
在 Maven 标签下双击 generate 就可以生成对应的文件
可以看到相应的目录之下出现了生成的文件
文件细节
打开 StudentMapper 接口,里面一共自动生成了六种方法
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
insert 方法和 insertSelective 方法有什么区别呢?
打开 StudentMapper.xml 文件可以看到 insertSelective 方法下用了动态SQL
(如果不记得动态SQL可以看一下这篇博客 MyBatis:使用动态SQL简化代码)
<insert id="insert" parameterType="com.example.model.Student">
insert into student (id, name, score,
birthday)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{score,jdbcType=REAL},
#{birthday,jdbcType=DATE})
</insert>
<insert id="insertSelective" parameterType="com.example.model.Student">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="score != null">
score,
</if>
<if test="birthday != null">
birthday,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="score != null">
#{score,jdbcType=REAL},
</if>
<if test="birthday != null">
#{birthday,jdbcType=DATE},
</if>
</trim>
</insert>
从以上代码中可以看出 insertSelective 方法只添加传入了参数的字段,而 insert 添加所有的字段,如果字段有默认值,使用insert 方法就会出错。
这种使用动态SQL的方式我们平时在写代码的时候也可以使用起来。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)