一、加入插件
pom.xml中加入mybatis 代码自动生成插件
<!--Mybatis逆向工程插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
二、GeneratorMapper.xml配置
重要的步骤:
- 指定JDBC驱动的jar包位置
- 配置数据库四要素:驱动,url,用户名,密码
- 指定生成实体类的位置(工程名与包名)
- 指定生成的Mapper.xml文件的位置
- 指定生成的Mapper接口的位置
- 指定数据库表名及对应的 Java 模型类名(需要生成哪些表)
<?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="H:\PowerNode\jar\MySql\mysql-connector-java-5.1.47.jar"/>
<!-- 配置 table 表信息内容体, targetRuntime 指定采用 MyBatis3 的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/springboot"
userId="root"
password="123">
</jdbcConnection>
<!-- 生成 model 类, targetPackage 指定 model 类的包名,
targetProject 指定生成的 model 放在 eclipse 的哪个工程下面-->
<javaModelGenerator targetPackage="com.wkcto.springboot.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成 MyBatis 的 Mapper.xml 文件,
targetPackage 指定 mapper.xml 文件的包名,
targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 -->
<sqlMapGenerator targetPackage="com.wkcto.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包名,
targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.wkcto.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名及对应的 Java 模型类名 -->
<table tableName="t_student" domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
注意:如果使用高版本, 驱动类变为: com.mysql.cj.jdbc.Driver,url 后面应该加属性 nullCatalogMeansCurrent=true,否则生成有问题
当前版本 MySQL 数据库为 5.7.18
三、运行插件
在IDEA右侧maven中对应的模块运行插件
四、生成的文件说明
1. mapper接口
MyBatis 只能自动生成单表中的sql语句,所以生成了六个单表操作的基本方法:
package com.wkcto.springboot.mapper;
import com.wkcto.springboot.model.Student;
public interface StudentMapper {
//根据主键进行删除
int deleteByPrimaryKey(Integer id);
//新增所有属性
int insert(Student record);
//新增不为null的属性
int insertSelective(Student record);
//要把主键查询
Student selectByPrimaryKey(Integer id);
//根据主键更新不为null的属性
int updateByPrimaryKeySelective(Student record);
//根据主键更新所有属性
int updateByPrimaryKey(Student record);
}
2. mapper.xml
除了生成mapper接口中对应的方法,还有sql片段和resultMap
不懂的请戳这里:MyBatis学习记录(3)之动态SQL
<mapper>
标签是将数据库表中的字段与实体类中的字段一对应起来,MyBatis 推荐数据库中字段多个单词使用下划线"_
"隔开,而java中的字段使用的是用驼峰命名法,所以逆向工程会生成<mapper>
标签来管理表字段与实体类之间的映射关系
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wkcto.springboot.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.wkcto.springboot.model.Student">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="age" jdbcType="INTEGER" property="age"/>
</resultMap>
<sql id="Base_Column_List">
id, name, age
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_student
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from t_student
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.wkcto.springboot.model.Student">
insert into t_student (id, name, age
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.wkcto.springboot.model.Student">
insert into t_student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="age != null">
age,
</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="age != null">
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.wkcto.springboot.model.Student">
update t_student
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.wkcto.springboot.model.Student">
update t_student
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
3. 实体bean
生成的主要是表中字段对应的实体类属性及对应的Getter和Setter
package com.wkcto.springboot.model;
public class Student {
private Integer id;
private String name;
private Integer age;
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
五、细节
1. 进行逆向工程时文件已经存在
进行逆向工程,如果mapper.xml文件已经存在,则会往后面继续追加,如果mapper.xml文件中已追加的多次,则在启动服务时会报 BuilderException 异常和 IllegalArgumentException 异常,所以在运行逆向工程的时候,要先删掉mapper.xml文件再执行逆向工程。
mapper 接口和实体bean 在逆向工程时如果存在会覆盖之前的
2. 逆向工程时引入工程中其它模块
进行逆向工程时,如果引用了本包中的模块,需要先将被引用模块打包并发布到本地仓库,否则会报下面的错:
Failed to execute goal on project 021-springboot-provider: Could not resolve dependencies for project com.wkcto.springboot:021-springboot-provider:jar:1.0.0: Failure to find com.wkcto.springboot:020-springboot-dubbo-interface:jar:1.0.0 in https://maven.aliyun.com/repository/public was cached in the local repository, resolution will not be reattempted until the update interval of aliyunmaven has elapsed or updates are forced -> [Help 1]