<!-- ======= PersonDao.java 接口中的方法 ======= --> public Person queryPersonByIf(Person person);
<!-- ======= PersonDao.xml mapper文件中 ======= --> <selectid="queryPersonByIf"resultType="com.tothefor.Study1.entity.Person"> <!-- 主SQL语句 --> select * from Person where <!-- 部分SQL语句,当条件满足时,会将部分SQL语句加到主SQL语句的后面 --> <iftest="id >= 0 and id != '' "><!-- OGNL语法 --> id = #{id} <!-- 前一个id是字段id,后一个id是对象的属性 --> </if> <iftest="name!=null and name!='' "> name=#{name} </if> </select>
<!-- ======= 测试类 ======= --> PersonDao dao = sqlSession.getMapper(com.tothefor.Study1.dao.PersonDao.class); Person person = new Person(); person.setId(1001); <!-- id --> person.setName(null); <!-- name --> Person result = dao.queryPersonByIf(person); System.out.println("---> "+result);
<!-- ======= 最后实际的SQL语句 ======= --> Preparing: select * from Person where id = ? <!-- name为null,不满足条件 -->
<!-- ======= PersonDao.java 接口中的方法 ======= --> public Person queryPersonByIf(Person person);
<!-- ======= PersonDao.xml mapper文件中 ======= --> <selectid="queryPersonByIf"resultType="com.tothefor.Study1.entity.Person"> <!-- 主SQL语句 --> select * from Person where id=-1 <!-- 部分SQL语句,当条件满足时,会将部分SQL语句加到主SQL语句的后面 --> <iftest="id >= 0 and id != '' "><!-- OGNL语法 --> or id = #{id} <!-- 前一个id是字段id,后一个id是对象的属性 --> </if> <iftest="name!=null and name!='' "> or name=#{name} </if> </select>
<!-- ======= 测试类中,一个也不满足 ======= --> Person person = new Person(); person.setId(-1); person.setName(null); <!-- ======= 最后实际的SQL语句 ======= --> Preparing: select * from Person where id = -1
<!-- ======= 测试类中,满足一个 ======= --> Person person = new Person(); person.setId(1001); person.setName(null); <!-- ======= 最后实际的SQL语句 ======= --> Preparing: select * from Person where id = -1 or id = ?
<!-- ======= 测试类中,满足两个 ======= --> Person person = new Person(); person.setId(1001); person.setName("lee"); <!-- ======= 最后实际的SQL语句 ======= --> Preparing: select * from Person where id = -1 or id = ? or name=?
<select id="queryPersonByWhere" resultType="com.tothefor.Study1.entity.Person"> select * from Person <where> <if test="name!=null and name!='' "> or name =#{name} </if> <if test="id>0"> or id > #{id} </if> </where> </select>
在测试时,直接使用大于符合也可以。
测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Person person = new Person(); person.setId(100); person.setName(null); Person result = dao.queryPersonByWhere(person);
// 最后实际的SQL语句 Preparing: select * from Person WHERE id > ?
Person person = new Person(); person.setId(100); person.setName("lee"); Person result = dao.queryPersonByWhere(person);
// 最后实际的SQL语句 Preparing: select * from Person WHERE name =? or id > ?
<!-- ======= PersonDao.java 接口中的方法 ======= --> public Person queryPersonByForeach(List<Integer> idlist);
<!-- ======= PersonDao.xml mapper文件中 ======= --> <selectid="queryPersonByForeach"resultType="com.tothefor.Study1.entity.Person"> select * from Person where id in
<!-- ======= PersonDao.java 接口中的方法 ======= --> public Person queryPersonByForeach2(List<Person> Plist);
<!-- ======= PersonDao.xml mapper文件中 ======= --> <selectid="queryPersonByForeach2"resultType="com.tothefor.Study1.entity.Person"> select * from Person where id in