_parameter:代表整个参数
- 单个参数:_parameter就是这个参数
<!--当使用@Param("employee")修饰变量后,就不能在使用"_parameter",可以使用employee或param1来获取变量中的值-->
<!--public List<Employee> getEmployeeTestInnerParameter(@Param("employee") Employee employee);-->
<select id="getEmployeeTestInnerParameter3" resultType="com.ll.mybatis.entity.Employee">
select id, last_name, email, gender
from tbl_employee
<if test="employee != null">
<!--<if test="_parameter!=null">-->
<where>
<if test="employee.lastName!=null">
last_name=#{employee.lastName}
</if>
<if test="employee.email!=null">
and email=#{employee.email}
</if>
</where>
</if>
</select>
下面是错误写法
<!--public List<Employee> getEmployeeTestInnerParameter(Employee employee);-->
<select id="getEmployeeTestInnerParameter" resultType="com.ll.mybatis.entity.Employee">
select id, last_name, email, gender
from tbl_employee
<!--下面写法错误,list类型形参employee没有使用@param修饰,不能直接使用employee,mybatis会误认为是对象中的属性-->
<if test="employee != null">
<!--<if test="_parameter!=null">-->
<where>
<if test="employee.lastName!=null">
last_name=#{employee.lastName}
</if>
<if test="employee.email!=null">
and email=#{employee.email}
</if>
</where>
</if>
</select>
下面是正确写法
<!--public List<Employee> getEmployeeTestInnerParameter(Employee employee);-->
<select id="getEmployeeTestInnerParameter" resultType="com.ll.mybatis.entity.Employee">
select id, last_name, email, gender
from tbl_employee
<if test="_parameter!=null">
<where>
<!--获取属性_parameter可以省略-->
<if test="_parameter.lastName!=null">
last_name=#{lastName}
</if>
<if test="email!=null">
and email=#{_parameter.email}
</if>
</where>
</if>
</select>
- 多个参数:多个参数会被封装为一个map:_parameter就是代表这个map
_databaseId
<!--
databaseIdProvider:数据库厂商标识,支持多数据库厂商
-->
<databaseIdProvider type="DB_VENDOR">
<property name="SQL Server" value="sqlserver"/>
<property name="DB2" value="db2"/>
<property name="Oracle" value="oracle"/>
<property name="MySQL" value="mysql"/>
</databaseIdProvider>
如果配置了databaseIdProvider标签,_databaseId就是代表当前数据库的别名
<!--根据不同的数据库执行不同的内容-->
<!--public List<Employee> getEmployeeTestInnerParameter(Employee employee);-->
<select id="getEmployeeTestInnerParameter" resultType="com.ll.mybatis.entity.Employee">
<!--mysql数据库-->
<if test="_databaseId=='mysql'">
select id, last_name, email, gender
from tbl_employee
<if test="_parameter!=null">
<where>
<if test="_parameter.lastName!=null">
last_name=#{lastName}
</if>
<if test="_parameter.email!=null">
and email=#{email}
</if>
</where>
</if>
</if>
<!--orcale数据库-->
<if test="_databaseId=='orcale'">
select id, last_name, email, gender
from employees
</if>
</select>