有四个参数,都不是必填项,那么要按照所填写的内容来查找数据:
xml:
<!-- 传输类型是传递对象,结果类型是map <![CDATA[XXXXXXXX]]> 是转义小于号的 --> <select id="selectPersonByCondition" parameterType="xxx.x.QueryCondition" resultMap="BaseResultMap"> select * from person t <where> <if test="name != null"> t.name like '%${name}%' </if> <if test="gender != null"> and t.gender = #{gender} </if> <if test="personAddr != null"> and t.person_addr like '%${personAddr}%' </if> <if test="birthday != null"> <![CDATA[ and t.birthday < #{birthday} ]]> </if> </where> </select>
java:
public void selectPersonByCondition() { //创建SqlSession SqlSession session = sessionFactory.openSession(); try { QueryCondition qc = new QueryCondition(); qc.setName("张三"); qc.setPerson_addr("shagnhai"); qc.setGender(1); qc.setBirthday(new Date()); List<Person> pList = session.selectList("xxx.x.mapper.PersonTestMapper.selectPersonByCondition",qc); for(Person p : pList) { System.out.println(p); } }catch (Exception e) { e.printStackTrace(); session.rollback(); }finally { session.close(); } }