一、前言
前面一节我们最后留下了一个疑问,如果第一个条件当id为空的时候,我们的SQL语句拼接就产生了问题,我们先通过案例复现一下这个问题,修改testFindEmpByCondition方法
@Test
public void testFindEmpByCondition() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmpMapper mapper = openSession.getMapper(EmpMapper.class);
Emp emp = new Emp(null,"ant","ant@sina.com",1);
Emp empFind = mapper.findEmpByConditions(emp);
System.out.println(empFind);
} finally {
openSession.close();
}
}
控制台打印如下
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version