MyBatis之Like模糊查询的两种实现方式
- 电脑硬件
- 2025-08-04 06:57:02

MyBatis之Like模糊查询的两种方式 文章目录 MyBatis之Like模糊查询的两种方式1. Mybatis中Like模糊查询1. 传递参数值时直接拼接%%2. Mapper.xml中绑定标签 1. Mybatis中Like模糊查询
有两种方式:
在Java代码中传递参数值时直接拼接 %%mapper.xml中利用数据库内置拼接函数实现(注意不同的数据库拼接方式不同)以下案例中使用的是Oracle数据库 1. 传递参数值时直接拼接%% java中 Map map = new HashMap(3); map.put("userOrgId","%"+userOrgId+"%"); map.put("userName","%"+userName+"%"); map.put("userRealName","%"+userRealName+"%"); Mappper.xml中 <if test="userOrgId != null"> and depart_ids LIKE #{userOrgId} </if> <if test="userName != null"> and username LIKE #{userName} </if> <if test="userRealName != null"> and realname LIKE #{userRealName} </if> 2. Mapper.xml中绑定标签 java中 Map map = new HashMap(3); map.put("userOrgId",userOrgId); map.put("userName",userName); map.put("userRealName",userRealName); Mappper.xml中 <if test="userOrgId != null"> and depart_ids LIKE CONCAT(CONCAT('%',#{userOrgId}),'%') </if> <if test="userName != null"> and username LIKE CONCAT(CONCAT('%',#{userName}),'%') </if> <if test="userRealName != null"> and realname LIKE CONCAT(CONCAT('%',#{userRealName}),'%') </if>MyBatis之Like模糊查询的两种实现方式由讯客互联电脑硬件栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“MyBatis之Like模糊查询的两种实现方式”