🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**方式二,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)); } ~~~