示例代码:

接口定义:
package com.mybatis.dao; import com.mybatis.bean.Employee; import java.util.List; public interface EmployeeMapper {
//携带了哪个字段查询条件就带上这个字段的值
public List<Employee> getEmpsByConditionIf(Employee employee); public List<Employee> getEmpsByConditionIfWhere(Employee employee);
} 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">
<mapper namespace="com.mybatis.dao.EmployeeMapper">
<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
<!-- public List<Employee> getEmpsByConditionIf(Employee employee); -->
<select id="getEmpsByConditionIf" resultType="com.mybatis.bean.Employee">
select * from tbl_employee
where 1=1
<if test="id!=null">
and id=#{id}
</if>
<if test="lastName!=null && lastName!=''">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=""">
and email=#{email}
</if>
<!-- ognl会进行字符串与数字的转换判断 "0"==0 -->
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</select> <!--public List<Employee> getEmpsByConditionIfWhere(Employee employee);-->
<select id="getEmpsByConditionIfWhere" resultType="com.mybatis.bean.Employee">
select * from tbl_employee
<where>
<if test="id!=null">
id=#{id}
</if>
<if test="lastName!=null && lastName!=''">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=""">
and email=#{email}
</if>
<!-- ognl会进行字符串与数字的转换判断 "0"==0 -->
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</where>
</select>
</mapper> 测试代码:
package com.mybatis.demo; import com.mybatis.bean.Employee;
import com.mybatis.dao.EmployeeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; public class MyTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
} @Test
public void test() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession(true);
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//查询的时候如果某些条件没带可能sql拼装会有问题
//1、给where后面加上1=1,以后的条件都and xxx.
//2、mybatis使用where标签来将所有的查询条件包括在内。mybatis就会将where标签中拼装的sql,多出来的and或者or去掉
//where只会去掉第一个多出来的and或者or。
List<Employee> emps = mapper.getEmpsByConditionIf(new Employee(null, "jetty", "jetty@126.com", 1));
for (Employee emp : emps) {
System.out.println(emp);
}
System.out.println("-------------------");
List<Employee> employees = mapper.getEmpsByConditionIfWhere(new Employee(null, "jetty", "jetty@126.com", 1));
for (Employee emp : employees) {
System.out.println(emp);
}
} finally {
openSession.close();
}
}
}

最新文章

  1. 五子棋AI清月连珠开源
  2. nexus
  3. Djunit工作记录
  4. android 定时任务
  5. Maven-在eclipse中安装Maven插件
  6. Linux下面对于VIM编辑器的代码折叠使用与screen
  7. iis10,php 5.2 isapi 扩展
  8. hdu1281棋盘游戏
  9. WinForm下的loading框实现
  10. c#写的热键注册程序
  11. VxWorks 基本启动方式
  12. TP中的AJAX返回ajaxReturn()
  13. shell编程学习笔记(一):编写我的第一段代码
  14. 180714、JRebel插件安装配置与破解激活(多方案)详细教程
  15. WDA-Webdynpro应用发布至EP
  16. django缓存基于类的视图
  17. 【Java】PreparedStatement VS Statement
  18. UIWebView UITextView
  19. 【附源文件】日记类App原型制作分享-Grid Diary
  20. Hadoop HBase概念学习系列之HBase里的4维坐标系统(第一步定位行键 -&gt; 第二步定位列簇 -&gt; 第三步定位列修饰符 -&gt; 第四步定位时间戳)(十八)

热门文章

  1. Python程序设计7——文件读写
  2. 断电操作导致的jboss项目部署失败------从早上九点一直到下午4点才解决
  3. Cadence如何自定义快捷键
  4. C指针的解析
  5. Git相关安装包打包下载
  6. day04-Linux系统中用户控制及文件权限管理方法
  7. MongoDB入门,安装配置与基本CURD操作
  8. 使用pycharm创建自己的第一个django项目
  9. 智能合约安全事故回顾(2)-BEC溢出攻击
  10. Python之运算符以及基本数据类型的object