choose
如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个
public List<Employee> getEmpByConditionChoose(Employee employee);
<!-- public List<Employee> getEmpByConditionChoose(Employee employee);--><select id="getEmpByConditionChoose" resultType="com.atguigu.mybatis.bean.Employee">select * from tb1_employee<where><!--如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个--><choose><when test="id!=null">id = #{id}</when><when test="lastName!=null">last_name like #{lastName}</when><when test="email!=null">email = #{email}</when><otherwise>gender =0</otherwise></choose></where></select>
@Testpublic void test04() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);Employee employee = new Employee(3,"%e%","jerry@qq.com",null);List<Employee> list = mapper.getEmpByConditionChoose(employee);for (Employee emp:list){System.out.println(emp);}}finally {sqlSession.close();}}