mybatis generator配置

前言

​ 每次做完一些配置都会忘,都要回去看博客,mybatis-generator的配置算是比较麻烦的了,但是配好之后用起来很方便,什么mapper,xml、model一键生成,几乎也不用自己写sql语句,爽的飞起,所以必须记录一下。

官方文档

步骤

​ 首先确保你已经在pom文件中添加了mybatis的相关依赖。

1.pom文件中添加plugin,在里面添加数据库相关驱动

1
2
3
4
5
6
7
8
9
10
11
12
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
</dependencies>
</plugin>

​ 我使用的是myssql数据库,所以添加的mysql的驱动

2. 在resources下,创建generatorConfig.xml文件

3.配置generatorCofig.xml文件

官网示例文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/community?serverTimezone=GMT"
userId="root"
password="******">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>

<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<javaModelGenerator targetPackage="cpsky.community.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>

<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>

<javaClientGenerator type="XMLMAPPER" targetPackage="cpsky.community.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

<table tableName="user" domainObjectName="User" ></table>
<table tableName="question" domainObjectName="Question" ></table>
<table tableName="comment" domainObjectName="Comment"></table>
</context>
</generatorConfiguration>

<jdbcConnection>填写你的数据库有关信息 <javaModelGenerator>生成java模型(配好路径)

<sqlMapperGenerator>生成mapper.xml文件(可以修改生成方式 如注释等)

详见

<javaClientGenerator>生成mapper层

<table>对应数据库的表

4.配置跟springboot相关联

官网说明

启动类添加@MapperScan(mapper层路径如com.***.mapper)

application.properties添加mybatis.mapper-locations=classpath:mapper/*.xml

application.properties添加mybatis.type-aliases-package=com.example.domain.model

5.在终端运行命令

mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

​ overwrite默认覆盖原先已经存在的文件

6.自定义查询语句

​ 因为overwirte=true 会覆盖原文件,所以要新建.xml文件和对应的mapper接口。举个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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="cpsky.community.mapper.CommentExtMapper">
<resultMap id="BaseResultMap" type="cpsky.community.model.Comment">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Jun 08 11:39:52 CST 2019.
-->
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="parent_id" jdbcType="BIGINT" property="parentId"/>
<result column="type" jdbcType="INTEGER" property="type"/>
<result column="commentator" jdbcType="BIGINT" property="commentator"/>
<result column="gmt_create" jdbcType="BIGINT" property="gmtCreate"/>
<result column="gmt_modified" jdbcType="BIGINT" property="gmtModified"/>
<result column="like_count" jdbcType="BIGINT" property="likeCount"/>
<result column="content" jdbcType="VARCHAR" property="content"/>
<result column="comment_count" jdbcType="INTEGER" property="commentCount"/>
</resultMap>
<update id="incCommentCount" parameterType="cpsky.community.model.Comment">
update COMMENT
set comment_count = comment_count + #{commentCount,jdbcType=INTEGER}
where id = #{id}
</update>
</mapper>

​ 要修改的有namespace ,以及curd语句,语法就是mybatis的语法辣,对应的Mapper接口长这个样子:

1
2
3
public interface CommentExtMapper {
int incCommentCount(Comment comment);
}
---------------- The End ----------------

本文基于 知识共享署名-相同方式共享 4.0 国际许可协议发布
本文地址: https://cpsky.github.io/2019/06/20/mybatis-generator配置/
转载请注明出处, 谢谢!

分享到: