企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**方式一,where配合if** **1.UserMapper.java** ~~~ //动态sql,根据传入条件 动态生成sql List<User> findByIdAndName(User u); ~~~ **2.UserMapper.xml** ~~~ <select id="findByIdAndName" resultMap="userMapper" parameterType="User"> select * from t_user <where> <if test="id !=null and id !='' "> and u_id = #{id} </if> <if test="name !=null and name !=''"> and u_name like #{name} </if> </where> </select> ~~~ **3.测试代码** ~~~ @Test public void testFindByIdAndName(){ UserMapper mapper = session.getMapper(UserMapper.class); User u = new User(); u.setId(1); u.setName("%no%"); System.out.println(mapper.findByIdAndName(u)); } ~~~