myBatis学习笔记(6)——动态SQL & 模糊查询 ゝ一世哀愁。 2022-08-08 07:01 172阅读 0赞 * \#动态SQL与模糊查询 <select id="selectUser" parameterType="Map" resultType="User"> select * from user where 1=1 <if test="name != '%null%' "> and username like #{name} </if> <if test="money != null and money > 0"> and money <![CDATA[>]]> #{money} </if> </select> > * 这里使用到了`<![CDATA[>]]>` 表示大于号。大于号和小于号在xml文件中是比较特殊的,不能直接使用,应该`<![CDATA[value]]>` 来表示,可以参考[spring学习笔记(3)——bean配置细节注意][spring_3_bean] > * 使用if标签,来动态增加查询的条件 > * `1=1` 是为了避免所有条件都会空时,语句错误 查询 SqlSessionFactory factory = MyBatisUtil.getFactory(); SqlSession session = factory.openSession(true); String statement = "com.bank.userMapper.selectUser"; Map<String,Object> map = new HashMap<String, Object>(); String name = "o"; map.put("name", "%"+name+"%"); map.put("money", 10); System.out.println(session.selectList(statement, map)); session.close(); [spring_3_bean]: /images/20220805/7a149c7e7d36435cad19b515edc21524.png
还没有评论,来说两句吧...