Mybatis代码生成+分页+Mapper继承扩展

× 文章目录
  1. 一. mybatis-gennerator
    1. 1.1 mybatis-gennerator自动生成代码
      1. 1.1.1 mybatis-gennerator的安装
  2. 二.Mybatis分页插件
    1. 2.1 引入maven依赖
      1. 2.2 使用pagehelper
  3. 三.Mybatis的Mapper继承
    1. 3.1 为什么使用Mapper继承
    2. 3.2 代码示例处理

摘要 本篇文章主要介绍使用mybatis-gennerator快速生成代码,但是每次生成的Mapper或接口都会覆盖自定义的Mapper,因此介绍了Mapper接口的继承方式解决,还介绍了如何使用分页工具使用pagehelper结合Mybatis快速实现分页。

一. mybatis-gennerator

1.1 mybatis-gennerator自动生成代码

1.1.1 mybatis-gennerator的安装

Mybatis-Generator的下载地址:https://github.com/mybatis/generator/releases

  1. 在Eclipse安装mybatis-gennerator插件。

https://marketplace.eclipse.org/content/mybatis-generator

2.在项目中安装Maven插件

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
<build>
<finalName>janus-admin</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<version>3.3</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
<configuration>
<!--配置文件的路径 -->
<configurationFile>${basedir}/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

使用命令:mybatis-generator:generate生成

推荐安装IDE插件的方式或者Maven插件的方式,生成的代码直接刷新工程即可

generatorConfig.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
38
39
40
41
<?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:\develop\apache-maven-3.5.0\res\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar" />
<context id="context1">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://主机:3306/数据库" userId="用户名" password="密码" />
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="org.xujin.janus.admin.entity" targetProject="janus-admin/src/main/java" />
<!-- 生成的映射文件报名和位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="janus-admin/src/main/resources" />
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator targetPackage="org.xujin.janus.admin.mapper" targetProject="janus-admin/src/main/java" type="XMLMAPPER" />
<!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) -->
<table schema="janus_admin" tableName="cluster" domainObjectName="Cluster" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table schema="janus_admin" tableName="route_info" domainObjectName="RouteInfo" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<!--
或者使用 tableName="%"通配全部生成
<table tableName="%" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--mysql 配置-->
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
<!--oracle 配置-->
<!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
</table>
-->
</context>
</generatorConfiguration>

二.Mybatis分页插件

2.1 引入maven依赖

1
2
3
4
5
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.3</version>
</dependency>

2.2 使用pagehelper

1
2
3
4
5
6
7
8
9
10
11
public interface ExRouteInfoMapper extends RouteInfoMapper {
public List<RouteInfo> selectAll();
/**
* 分页查询数据
* @return
*/
Page<RouteInfo> findByPage();
}

业务层使用代码示li

1
2
3
4
5
6
7
@Override
public PageInfo<RouteInfo> findByPage(int pageNo, int pageSize) {
PageHelper.startPage(pageNo, pageSize);
Page<RouteInfo> pagelist = routeMapper.findByPage();
PageInfo<RouteInfo> pageInfo = new PageInfo<>(pagelist);
return pageInfo;
}

三.Mybatis的Mapper继承

3.1 为什么使用Mapper继承

使用mybatis-gennerator自动生成代码,每次自动生成代码都会覆盖之前的Mapper文件,为了将自定义的Mapper和接口与生成的Mapper与接口分开,使用继承方式处理。

3.2 代码示例处理

1.mybatis-gennerator自动生成的Mapper接口

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface RouteInfoMapper {
int deleteByPrimaryKey(Long id);
int insert(RouteInfo record);
int insertSelective(RouteInfo record);
RouteInfo selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(RouteInfo record);
int updateByPrimaryKey(RouteInfo record);
}

2.自动生成的Mapper文件

1
2
3
4
5
6
7
8
9
10
11
12
<?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="org.xujin.janus.admin.mapper.RouteInfoMapper">
<resultMap id="BaseResultMap" type="org.xujin.janus.admin.entity.RouteInfo">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="domain_id" jdbcType="INTEGER" property="domainId" />
<result column="type" jdbcType="VARCHAR" property="type" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<!-- 其余省略 -->
</mapper>

3.继承RouteInfoMapper扩展的接口

1
2
3
4
5
6
7
8
9
10
11
public interface ExRouteInfoMapper extends RouteInfoMapper {
public List<RouteInfo> selectAll();
/**
* 分页查询数据
* @return
*/
Page<RouteInfo> findByPage();
}

4.扩展的Mapper文件

1
2
3
4
5
6
7
8
9
10
11
12
<?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="org.xujin.janus.admin.mapper.ex.ExRouteInfoMapper">
<resultMap id="BaseResultMap" type="org.xujin.janus.admin.entity.RouteInfo">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="domain_id" jdbcType="INTEGER" property="domainId" />
<result column="type" jdbcType="VARCHAR" property="type" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<!-- 其余省略 -->
</mapper>

5.Service层依赖注入ExRouteInfoMapper使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Service
@Transactional(readOnly = true)
public class RouteInfoServiceImpl implements RouteInfoService {
@Autowired
private ExRouteInfoMapper routeMapper;
@Override
@Transactional
public void insert(RouteInfoModel routeInfoModel) {
RouteInfo RouteInfo = BeanMapper.map(routeInfoModel, RouteInfo.class);
routeMapper.insert(RouteInfo);
}
}
如果您觉得文章不错,可以打赏我喝一杯咖啡!