SpringBoot使用MyBatis-Generator详解-copy

SpringBoot使用MyBatis-Generator详解
MyBatis-Generator简介
MyBatis-Generator使用
添加maven依赖
创建MBG配置文件
运行MBG,生成底层类
数据库查询

前几天工作中接触到MyBatis-Generator,发现其方便之处,MyBatis-Generator可以帮助我们实现数据库繁复的增删改查操作,当数据库中表结构发生变化时,只需要重新生成一次即可,而不用每一条语句上去添加新增或者删除字段,这样可以大大的提高我们的开发效率,并且,不需要写大量的重复性的工作,可以让我们把精力更多的专注于业务逻辑。
​ MyBatis-Generator官网

MyBatis-Generator简介
​ MyBatis Generator(MBG,缩写名)是MyBatis的代码生成器。 它将为所有版本的MyBatis以及版本2.2.0之后的iBATIS版本生成数据库的底层实现代码。 它可以根据数据库中的表文件,生成可用于访问数据表的Pojo类,dao类和*Mapper.xml的所有文件。MBG为大部分数据库操作即相对简单CRUD(增删改查)提供支持,不过相对复杂的操作比如多表查询还需自己动手完成sql。

​ MBG生成的SQL配置文包含一下操作的语句:

​ -[ ] insert

​ -[ ] update by primary key

​ -[ ] update by example (using a dynamic where clause)

​ -[ ] delete by primary key

​ -[ ] delete by example (using a dynamic where clause)

​ -[ ] select by primary key

​ -[ ] select by example (using a dynamic where clause)

​ -[ ] count by example

​ MBG默认生成所有的语句,我么同样可以在配置文件中通过配置去除自己不需要的语句。

MyBatis-Generator使用
添加maven依赖
<!--数据库连接依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--MBG插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
</plugin>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
本文使用的SpringBoot版本是2.0.3.RELEASE。

创建MBG配置文件
​ MBG最重要的就是配置文件,可以参考以为大神的博客,我也从中受益很多,因为MBG配置文件可选参数实在是很多,本文只介绍些重要的参数的属性,足够正常开发使用,如有需要,还是需要看MBG的官方文档的。

​ 在resources文件目录下创建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>
<classPathEntry
location="D:Office SoftWaremaven epositorymysqlmysql-connector-java8.0.12mysql-connector-java-8.0.12.jar"/>

<context id="MysqlTables" targetRuntime="MyBatis3" defaultModelType="flat">

<property name="javaFileEncoding" value="UTF-8"/>

<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="addRemarkComments" value="true"/>
<property name="suppressAllComments" value="false"/>
</commentGenerator>

<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/testMBG?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"
userId="root"
password="123456">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>

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

<javaModelGenerator targetPackage="com.doo.mybatis.domain.entity"
targetProject="srcmainjava">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<sqlMapGenerator targetPackage="mappers" targetProject="srcmain esources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<javaClientGenerator type="XMLMAPPER" targetPackage="com.doo.mybatis.dao"
targetProject="srcmainjava">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>


<table schema="ins_personal_claim" tableName="claim_apply_record" enableCountByExample="true" domainObjectName="ClaimApplyRecord">
<columnOverride column="apply_for" javaType="com.jd.ins.personal.claim.domain.enums.ApplyForType" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
<ignoreColumn column="create_time"/>
<ignoreColumn column="update_time"/>
</table>

<table schema="ins_personal_claim" tableName="claim_image_info" enableCountByExample="false" domainObjectName="ClaimImageInfo">
<columnOverride column="image_type" javaType="com.jd.ins.personal.claim.domain.enums.ImageType" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
<ignoreColumn column="create_time"/>
<ignoreColumn column="update_time"/>
</table>

</context>
</generatorConfiguration>
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
​ 配置文件中的<classPathEntry>是配置驱动的一个简单的方式,可以通过location制定路径。

​ <context>元素用于指定生成一组对象的环境。例如指定要连接的数据库,要生成对象的类型和要处理的数据库中的表。其中的defaultModelType属性很重要,这个属性定义了MBG如何生成实体类,推荐使用flat模式,为每一张表只生成一个包含表所有字段的实体类。

​ <javaModelGenerator>指定生成实体类的生成路径,trimStrings属性会对查询条件进行trim操作,默认值是false。

​ <sqlMapGenerator>指定生成xml文件的路径。

​ <javaClientGenerator>指定生成dao接口。

​ <table>可以配置多个,用于指定生成数据库中的那个表的底层类,可以指定生成的实体类的name,enableCountXXX属性可以去除不需要的sql方法,其中columnOverride可以指定表中使用的枚举类,ignoreColumn可以忽略表中的字段,columnOverride和ignoreColumn可以指定0个或多个。

运行MBG,生成底层类
​ 执行MBG的方式有很多,本文使用Maven的方式执行,也是较为简单的方式。

点击执行就可以自动生成代码,会生成实体类,查询使用的example文件,dao,xml文件,如下图所示:

数据库查询
​ 生成了数据表操的所有底层类后,我们简单的使用它来进行一次查询操作,示例代码如下:

@Test
public void queryByPrimaryKey() {
ClaimImageInfo imageInfo = imageInfoMapper.selectByPrimaryKey(112L);
log.info("查询结果为:{}", imageInfo);
}

@Test
public void queryByExample(){
ClaimImageInfoExample example = new ClaimImageInfoExample();
example.createCriteria().andIdGreaterThan(112L);
example.setOrderByClause(" id limit " + 10);
List<ClaimImageInfo> imageInfoList = imageInfoMapper.selectByExample(example);
log.info("查询结果为:{}", imageInfoList);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
如果需要对查询结果进行排序,可以使用setOrderByClause来添加排序语句,并且如果有分页的需求,可以通过id或者其他的条件,和limit配合使用,达到分页的效果。

希望本文对你有所帮助,如有疑问,可进行留言。
————————————————
版权声明:本文为CSDN博主「李子树_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34666857/article/details/90142700

原文地址:https://www.cnblogs.com/hanease/p/14492546.html