ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**方式二,where配合choose** **UserMapper.java** ~~~ //动态sql 查询2 List<User> findByIdAndName2(User u); ~~~ **UserMapper.xml** ~~~ <select id="findByIdAndName2" resultMap="userMapper" parameterType="User"> select * from t_user <where> <choose> <when test="id !=null and id != ''"> and u_id = #{id} </when> <when test="name !=null and name !=''"> and u_name like #{name} </when> <!--以上条件不成立 --> <otherwise> and 1=1 </otherwise> </choose> </where> </select> ~~~ **测试代码** ~~~ @Test public void testFindByIdAndName(){ UserMapper mapper = session.getMapper(UserMapper.class); User u = new User(); u.setId(1); u.setName("%no%"); System.out.println(mapper.findByIdAndName2(u)); } ~~~