<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.bgy.mybatis.dao.EmployeeMapper"><!--
if
choose(when,otherwise)
trim(where,set)
foreach
--><!--
查询员工
要求:
携带了那个字段,查询条件就带上这个字段的值
--><selectid="getEmpsByConditionIf"resultType="com.bgy.mybatis.bean.Employee">
select
*
from
tbl_employee
where
<!--
test:判断表达式(OGNL)
从参数中取值
遇见特殊符号需要转义
比如: " " ==(转义)==> ""
--><iftest="id!=null and id!=''">
id=#{id}
</if><iftest="lastName!=null and lastName!=""">
and last_name like #{lastName}
</if><iftest="email!=null and email.trim()!=""">
and email like #{email}
</if><!-- ognl会自动进行字符串和数字的转换判断 "0"==0 --><iftest="gender==0 or gender==1">
and gender=#{gender}
</if></select></mapper>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.bgy.mybatis.dao.EmployeeMapper"><!--
if
choose(when,otherwise)
trim(where,set)
foreach
--><!--
查询员工
要求:
携带了那个字段,查询条件就带上这个字段的值
--><selectid="getEmpsByConditionIf"resultType="com.bgy.mybatis.bean.Employee">
select
*
from
tbl_employee
where
1=1
<!--
查询的时候如果某些条件没带sql,拼装可能有问题
解决:
1、给where后面加上 1=1 ,以后的条件都是 and xxx
2、mybatis使用where标签来将所有的查询条件包括在内。
--><iftest="id!=null and id!=''">
and id=#{id}
</if><iftest="lastName!=null and lastName!=""">
and last_name like #{lastName}
</if><iftest="email!=null and email.trim()!=""">
and email like #{email}
</if><!-- ognl会自动进行字符串和数字的转换判断 "0"==0 --><iftest="gender==0 or gender==1">
and gender=#{gender}
</if></select></mapper>
EmployeeMapper.xml(笔记在这儿)(where标签)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.bgy.mybatis.dao.EmployeeMapper"><!--
if
choose(when,otherwise)
trim(where,set)
foreach
--><!--
查询员工
要求:
携带了那个字段,查询条件就带上这个字段的值
--><selectid="getEmpsByConditionIf"resultType="com.bgy.mybatis.bean.Employee">
select
*
from
tbl_employee
<!--
查询的时候如果某些条件没带sql,拼装可能有问题
解决:
1、给where后面加上 1=1 ,以后的条件都是 and xxx
2、mybatis使用where标签来将所有的查询条件包括在内。
mybatis就会将where标签中拼装的sql,将多出来的and或or去掉。
where只能去掉第一个多出来的and或or。
--><where><iftest="id!=null and id!=''">
id=#{id}
</if><iftest="lastName!=null and lastName!=""">
and last_name like #{lastName}
</if><iftest="email!=null and email.trim()!=""">
and email like #{email}
</if><!-- ognl会自动进行字符串和数字的转换判断 "0"==0 --><iftest="gender==0 or gender==1">
and gender=#{gender}
</if></where></select></mapper>