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 标签内设置自动映射模式:

1
<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 字段,其他的属性可以通过默认的自动映射机制来映射:

1
2
3
4
5
6
<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 仅映射指定的映射字段:

1
2
3
<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 是嵌套的还是非嵌套的,都会进行自动映射,这可能会造成某些嵌套属性与查询结果的字段名一致而误被自动映射,例如以下示例:

1
2
3
4
5
6
7
8
9
10
11
12
<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>

  

最新文章

  1. logistic回归
  2. 【C语言入门教程】2.5 字符型数据
  3. 微软职位内部推荐-SDE II
  4. 当C++学到第20天的时候我崩溃了(找回刚开始的激情)
  5. UDP模式与TCP模式的区别
  6. “文件XXX正由另一进程使用,因此该进程无法访问此文件”
  7. Hdu3072-Intelligence System(强连通求最小值)
  8. C# Socket的TCP通讯
  9. 关于DLL的学习
  10. cocos2d-x-Json/XML文件
  11. (转)测试如何区别是前端的问题还是后台的bug
  12. [ERROR] --gtid-mode=ON or UPGRADE_STEP_1 or UPGRADE_STEP_2 requires --log-bin and --log-slave-updates
  13. 001_angular4.0框架学习
  14. python 读csv数据 通过改变分隔符去掉引号
  15. mysql高可用架构 -&gt; MHA简介-01
  16. 偶数求1/2+1/4+...+1/n奇数1/1+1/3+...+1/n
  17. centeros php 实战
  18. 2017-2018-1 20155323《信息安全技术》实验二 Windows口令破解
  19. es6之Iterator
  20. C++/Java中继承关系引发的调用关系详解

热门文章

  1. 你了解MySQL的加锁规则吗?
  2. Docker学习-Kubernetes - 集群部署
  3. Reporting service个人使用经验
  4. CSS如何修改tr边框属性
  5. C#实现整型数据字任意编码任意进制的转换和逆转换
  6. 用户环境变量 shell变量 别名
  7. python中的__call__方法
  8. windows下大数据开发环境搭建(1)——Hadoop环境搭建
  9. 线程同步&amp;线程池
  10. Redis 的底层数据结构(对象)