mybatis like相关问题

Mybatis 中关于like的使用

  1. 使用字符串传值
    我们要使用#{0}#{1}#{2去匹配传入的每个参数},因为带like,所以在传入值的时候要在名字左右拼上%号,像这样’%tom%’,而不是’tom’,还有一点就是一定注意传入的顺序

Mapper.java

1
public List<BdipChatPoint> selectListByPage(String modelUrl,String username);

Mapper.xml

1
2
3
4
5
6
<select id="selectListByPage" parameterType="java.lang.String" resultMap="pointMap">
select * from bdip_chat_point
where tree_id = #{0}
and user_name like #{1}
</select>

main.java

1
2
3

mapper.selectListByPage("1","%tom%");

有的人在Mapper.xml可能会这样写,是取不到值的,下面是错误演示

1
2
3
4
5
<select id="selectListByPage" parameterType="java.lang.String" resultMap="pointMap">
select * from bdip_chat_point
where tree_id = #{modelUrl}
and user_name like #{username}
</select>

如果我们就想使用这种方法查,要怎么办呢?可以用Map封装参数,看一下使用Map传值

2.使用Map传值

Mapper.java

1
2
public List<BdipChatPoint> selectListByPage(Map map);

Mapper.xml

1
2
3
4
5
6
7

<select id="selectListByPage" parameterType="Map" resultMap="pointMap">
select * from bdip_chat_point
where tree_id = #{modelUrl}
and user_name like #{username}
</select>

调用的时候初始化map对象

1
2
3
4
5

Map map = new HashMap();
map.put("modelUrl", "624");
map.put("username", "%tom%");

需要注意的是like #{username}map.put("username", "%tom%");如果你不想去拼%号,怎么办?我们可以这样子写。

Mapper.xml

1
2
3
4
5
6
7

<select id="selectListByPage" parameterType="Map" resultMap="pointMap">
select * from bdip_chat_point
where tree_id = #{modelUrl}
and user_name like '%${username}%'
</select>

1
2
3
4
5

Map map = new HashMap();
map.put("modelUrl", "624");
map.put("username", "tom");

good luck!~


mybatis like相关问题
http://example.com/2018/04/10/2018-04-10-mybatis-mybatis like相关问题/
Author
Hoey
Posted on
April 10, 2018
Licensed under