内容简介:使用MyBatis轻松实现递归查询与存储过程调用
vhr部门管理模块更新啦!为了让小伙伴们快速理解部门管理模块实现思路,我想通过3篇短文来给大家介绍下大致的实现思路和核心代码。
项目地址: https://github.com/lenve/vhr
建议小伙伴们先阅读前面的文章,会有助于你理解本文。
1. SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题(一)
2. SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题(二)
3. SpringSecurity中密码加盐与SpringBoot中异常统一处理
6. SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题(六)
本文主要介绍部门管理功能的后台程序,其实都是常规代码,我们重点关注两点:1.递归调用,2.存储过程调用
递归调用
由于部门的层级不可控,因此如果我想要获取所有部门的完整json的话,就要采用递归调用,使用 Java 代码处理递归有点low,刚好MyBatis的ResultMap中的collection可以很方便的解决这个问题,核心代码如下:
<resultMap id="BaseResultMap" type="org.sang.bean.Department">
<id property="id" column="id"/>
<result column="name" property="name"/>
<result column="parentId" property="parentId"/>
<result column="isParent" property="isParent"/>
<collection property="children" ofType="org.sang.bean.Department" select="org.sang.mapper.DepartmentMapper.getDepByPid" column="id">
</collection>
</resultMap>
<select id="getDepByPid" resultMap="BaseResultMap">
select d1.*from department d1 where d1.`parentId`=#{pid} AND d1.enabled=true;
</select>
每一个Department中都有一个children属性,getDepByPid方法的返回结果是一个BaseResultMap,BaseResultMap中的collection又将调用getDepByPid方法,通过这种方式我们可以快速实现一个递归调用。Mapper中只需要定义如下方法即可:
List<Department> getDepByPid(Long pid);
查询结果如下(部分):
[
{
"id": 1,
"name": "股东会",
"parentId": -1,
"enabled": true,
"children": [
{
"id": 4,
"name": "董事长",
"parentId": 1,
"enabled": true,
"children": [
{
"id": 5,
"name": "总经理",
"parentId": 4,
"enabled": true,
"children": [
{
"id": 8,
"name": "财务部",
"parentId": 5,
"enabled": true,
"children": [],
"parent": false
}],
"parent": true
}
],
"parent": true
}
],
"parent": true
}
]
存储过程调用
存储过程调用比较简单,以添加部门为例,如下:
1.Mapper中添加如下方法:
void addDep(@Param("dep") Department department);
2.xml中写法如下:
<select id="addDep" statementType="CALLABLE">
call addDep(#{dep.name,mode=IN,jdbcType=VARCHAR},#{dep.parentId,mode=IN,jdbcType=INTEGER},#{dep.enabled,mode=IN,jdbcType=BOOLEAN},#{dep.result,mode=OUT,jdbcType=INTEGER},#{dep.id,mode=OUT,jdbcType=BIGINT})
</select>
注意statementType调用表示这是一个存储过程,mode=IN表示这是输入参数,mode=OUT表示这是输出参数,调用成功之后,在service中获取department的id和result字段,就能拿到相应的调用结果了。
其他一些琐碎的技术就不值得介绍了,大家在源码中自行研究,有问题欢迎留言讨论。
关注公众号,可以及时接收到最新文章:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- MySQL存储过程语句及调用
- asp.net中如何调用sql存储过程实现分页
- 使用JPA和Hibernate调用存储过程的最佳方法 - Vlad Mihalcea
- Oracle报存储过程中调用DBLink同义词出现错误:PLS-00201: 必须声明标识符
- Kubernetes 1.14.1 使用 NFS-Client-Provisioner 调用 NFS Server 静态和动态配置 Mysql 持久化存储
- 直观讲解-RPC调用和HTTP调用的区别
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Music Recommendation and Discovery
Òscar Celma / Springer / 2010-9-7 / USD 49.95
With so much more music available these days, traditional ways of finding music have diminished. Today radio shows are often programmed by large corporations that create playlists drawn from a limited......一起来看看 《Music Recommendation and Discovery》 这本书的介绍吧!