Mybatis模糊查询及自动映射实现详解

作者:yaominghui 时间:2021-10-29 12:05:40 

这篇文章主要介绍了Mybatis模糊查询及自动映射实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Mybatis的模糊查询

1. 参数中直接加入%%

1
2
3
4
5
6
7
8
9
param.setUsername("%CD%");
param.setPassword("%11%");

<select id="selectPersons" resultType="person" parameterType="person">
select id,sex,age,username,password from person where true
<if test="username!=null"> AND username LIKE #{username}</if>
<if test="password!=null">AND password LIKE #{password}</if>

</select>
2. bind标签

1
2
3
4
5
6
<select id="selectPersons" resultType="person" parameterType="person">
<bind name="pattern" value="'%' + _parameter.username + '%'" />
select id,sex,age,username,password
from person
where username LIKE #{pattern}
</select>
3. CONCAT

1
where username LIKE concat(concat('%',#{username}),'%')
Mybatis的自动映射

0x00:引子
在 MyBatis 的映射配置文件中,select 标签查询配置结果集时使用过 resultType 属性,当在 resultType 中定义一个 Java 包装类时,如果 sql 语句查询的结果中有列名与该 Java 包装类中的属性名一致,则该字段就会被映射到该属性上。这里用到的就是 MyBatis 的自动映射功能,

当 sql 语句查询出结果时,如果对应输出配置的 Java 包装类中有相同名称的属性,且拥有 set 方法,则该结果就会被自动映射。

0x01:原理

MyBatis 的自动映射功能是建立在 resultMap 基础之上的。resultType 属性自动映射的原理是,当 sql 映射输出配置为 resultType 时,MyBatis 会生成一个空的 resultMap,然后指定这个 resultMap 的 type 为指定的 resultType 的类型,接着 MyBatis 检测查询结果集中字段与指定 type 类型中属性的映射关系,对结果进行自动映射。

在 MyBatis 全局配置文件中,在 setting 标签内设置自动映射模式:



<setting name="autoMappingBehavior" value="PARTIAL"/>

0x02:配置

在 MyBatis 中,自动映射有三种模式,分别是 NONE、PARTIAL、FULL。其中 NONE 表示不启用自动映射,PARTIAL 表示只对非嵌套的 resultMap 进行自动映射,FULL 表示对所有的 resultMap 都进行自动映射。默认的自动映射模式为 PARTIAL。

0x03:拓展

在 sql 查询结果中,如果只有部分字段与输入配置类型中的属性名称不一样,则可以仅在 resultMap 中指定不一样的字段对应的输出类型的属性,其他的则会直接进行自动映射。

例如以下示例,Java 包装类中用户名属性为 username,而在 t_user 表中用户名的字段名为 name,这里需要手动映射 name 字段,其他的属性可以通过默认的自动映射机制来映射:


<resultMap type="cn.com.mybatis.pojo.User" id="UserResult">
<result property="username" column="name"/>
</resultMap>
<select id="findUserById" parameterType="java.lang.Long" resultMap="UserResult">
select id,name,email from t_user where id=#{id}
</select>

在 User 类中,包含了手动映射的 username 属性和自动映射的 id、email 属性。

如果在某些 resultMap 中不想使用自动映射,则可以单独在该 resultMap 中设置 autoMapping 的属性为 false,此时该 resultMap 仅映射指定的映射字段:


<select id="findUserById" parameterType="java.lang.Long" resultMap="UserResult" autoMapping="false">
select id,name,email from t_user where id=#{id}
</select>

当配置了 autoMapping 属性后,就会忽略全局配置文件中的 autoMappingBehavior 映射模式。

0x04:关于 FULL

关于 FULL 模式,应该谨慎使用,该模式不管 resultMap 是嵌套的还是非嵌套的,都会进行自动映射,这可能会造成某些嵌套属性与查询结果的字段名一致而误被自动映射,例如以下示例:


<resultMap id="replyResult" type="cn.com.mybatis.pojo.Reply">
<association property="user" resultMap="userResult"/>
</resultMap>

<resultMap id="userResult" type="cn.com.mybatis.pojo.User">
<result property="username" column="name"/>
</resultMap>

<select id="queryReplyInfo" parameterType="java.lang.Long" resultMap="replyResult">
select R.id,R.title,R.info,U.name form
reply R left join t_user U on R.user_id = U.id where R.id=#{id}
</select>

来源:https://www.cnblogs.com/lowerma/p/11639310.html

标签:Mybatis,模糊,查询,映射
0
投稿

猜你喜欢

  • Spring定时任务使用及如何使用邮件监控服务器

    2023-01-12 16:38:58
  • Java虚拟机GC日志分析

    2023-02-21 15:04:40
  • Springboot启动流程详细分析

    2023-11-29 00:23:10
  • Java Springboot 重要知识点整理汇总

    2022-03-17 01:06:57
  • Java Stream流的常见生成和操作方法总结

    2023-08-26 12:06:21
  • Java实现雪花算法的原理和实战教程

    2021-11-20 18:20:41
  • Java语言中Swing组件编程详解

    2023-04-19 04:08:22
  • 解决Java中OutOfMemoryError的问题

    2023-01-26 13:51:03
  • JDK1.8下载、安装和环境配置超详细教程(最新最完整)

    2022-07-22 12:58:34
  • SpringBoot+WebSocket实现消息推送功能

    2021-11-15 12:16:18
  • 深入浅析Mybatis的缺陷问题

    2023-07-19 19:39:46
  • C# 4.0 大数的运算--BigInteger的应用详解

    2022-02-02 06:40:05
  • Springboot 异步任务和定时任务的异步处理

    2022-08-20 07:21:12
  • 详解Java字节码编程之非常好用的javassist

    2021-08-27 04:54:30
  • Java简单实现SpringMVC+MyBatis分页插件

    2023-09-09 23:08:45
  • Java BufferWriter写文件写不进去或缺失数据的解决

    2023-07-20 14:57:02
  • Java通过FTP服务器上传下载文件的方法

    2021-08-15 07:26:39
  • 解决SpringBoot中使用@Async注解失效的问题

    2023-08-24 07:38:46
  • SpringCloud实现灰度发布的方法步骤

    2023-03-17 05:18:37
  • Java基于动态规划法实现求最长公共子序列及最长公共子字符串示例

    2021-10-11 22:20:17
  • asp之家 软件编程 m.aspxhome.com