网站服务器是主机吗,帮平台做推广怎么赚钱,wordpress 简繁体 插件,国内国外重大新闻OGNL if
要求#xff1a;查询员工#xff0c;要求#xff0c;携带了哪个字段查询条件就带上这个字段的值 //携带了哪个字段查询条件就带上这个字段的值public ListEmployee getEmpsByConditionIf(Employee employee);!-- public ListEmployee getEm…OGNL if
要求查询员工要求携带了哪个字段查询条件就带上这个字段的值 //携带了哪个字段查询条件就带上这个字段的值public ListEmployee getEmpsByConditionIf(Employee employee);!-- public ListEmployee getEmpsByConditionIf(Employee employee);--
select idgetEmpsByConditionIf resultTypecom.atguigu.mybatis.bean.Employeeselect * from tb1_employeewhere!--test,判断表达式(OGNL)OGNL参照PPT或者官方文档c:if test从参数中取值进行判断遇见特殊符号应该去写转义字符;--if test id!nullid #{id}/ifif test lastName!null amp;amp; lastName!quot;quot;and last_name like #{lastName}/if
if testemail!null and email.trim()!quot;quot;and email #{email}
/if!--ognl会进行字符串与数字的转换判断 00 --
if testgender0 or gender1and gender #{gender}/if/selectTestpublic void test04() throws IOException {SqlSessionFactory sqlSessionFactory getSqlSessionFactory();SqlSession sqlSession sqlSessionFactory.openSession();try{EmployeeMapperDynamicSQL mapper sqlSession.getMapper(EmployeeMapperDynamicSQL.class);Employee employee new Employee(3,%e%,jerryqq.com,null);ListEmployee emps mapper.getEmpsByConditionIf(employee);for (Employee emp:emps){System.out.println(emp);}}finally {sqlSession.close();}}查询的时候如果某些条件没带sql的拼装可能出现问题
解决方法使用where
where
第一种方法
给where后面加上11以后的条件都and xxx 第二种方法
mybatis使用where标签来将所有的查询条件包括在内。mybatis就会将where标签中拼接的sql多出来的and或者or去掉where只会去掉第一个多出来的and或者or。
!-- public ListEmployee getEmpsByConditionIf(Employee employee);--
select idgetEmpsByConditionIf resultTypecom.atguigu.mybatis.bean.Employeeselect * from tb1_employeewhereif test id!nullid #{id}/ifif test lastName!null amp;amp; lastName!quot;quot;and last_name like #{lastName}/ifif testemail!null and email.trim()!quot;quot;and email #{email}/if!--ognl会进行字符串与数字的转换判断 00 --if testgender0 or gender1and gender #{gender}/if/where/selecttrim
后面多出的and或者or where标签不能解决
prefix “”:前缀;trim标签体中整个字符串拼串后的结果。prefix给拼串后的整个字符串加一个前缀prefixOverrides “”:前缀覆盖;去掉整个字符串多余的字符suffix “”:给拼串后的整个字符串加一个后缀suffixOverrides “”:后缀覆盖去掉整个字符串后面多余的字符
!-- public ListEmployee getEmpsByConditionIf(Employee employee);--
select idgetEmpsByConditionIf resultTypecom.atguigu.mybatis.bean.Employeeselect * from tb1_employeetrim prefixwhere suffixOverridesandif test id!nullid #{id} and/ifif test lastName!null amp;amp; lastName!quot;quot;last_name like #{lastName} and/ifif testemail!null and email.trim()!quot;quot;email #{email} and/if!--ognl会进行字符串与数字的转换判断 00 --if testgender0 or gender1gender #{gender}/if/trim/select