博客
关于我
每日总结[5 20191005]MyBatis之输入输出映射
阅读量:324 次
发布时间:2019-03-04

本文共 1213 字,大约阅读时间需要 4 分钟。

输入输出映射部分

为了实现Mybatis的动态代理,我们可以按照以下步骤进行配置。首先,创建一个新的Mapper包,其下包含接口UserMapper和对应的UserMapper.xml文件。在SqlMapConfig.xml中添加如下配置:

这会使Mybatis扫描指定包中的接口,并根据接口定义生成相应的SQL映射文件。在接口UserMapper中定义需要与UserMapper.xml中的statement一致的方法。

为了支持动态查询需求,我们可以创建一个扩展类UserEx和一个包装类UserPackingUserPacking类中需要定义一个UserEx类型的属性,以便在接口中通过参数传递必要的条件。

以下是实现需求1的具体配置:

需求1:支持条件查询

UserMapper.xml中配置如下SQL:

为了提取重复的判断逻辑,可以将条件判断部分单独定义为一个SQL片段:

usersex=#{userEx.sex}
and username like '%${userEx.name}%'

在主查询中引用该片段:

需求2:批量查询

为了支持批量查询,我们可以在UserPacking类中添加一个ids属性,并在接口中使用foreach循环来处理多个ID的查询。以下是实现示例:

需求3:结果字段映射

为了将数据库中的字段映射到对象属性上,我们可以使用resultmap来定义字段别名映射。以下是实现示例:

在主查询中引用结果映射:

通过以上配置,我们可以实现动态代理、条件查询、批量查询以及结果字段映射等功能。在实际应用中,可以根据具体需求调整SQL片段和结果映射逻辑。

转载地址:http://qbjh.baihongyu.com/

你可能感兴趣的文章
NoSQL介绍
查看>>
Notadd —— 基于 nest.js 的微服务开发框架
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>