一、前言
前面一节我们最后留下了一个疑问,如果第一个条件当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","[email protected]",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 for the right syntax to use near 'and emp_name='queen' and emp_name='queen' and dept_i' at line 6 ### The error may exist in EmpMapper.xml ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: select id,emp_name empName,emp_email empEmail, dept_id deptId from t_emp where and emp_name=? and emp_name=? and dept_id=?如上,控制台报错,说MySQL语法错误。
那么这个时候我们要怎么处理这种问题,这里我们提出两种解决方案
①在where语句后面添加1=1,形如”where 1=1″
②使用MyBatis提供的where标签
下面我们通过相关案例来说明这两种情况。
二、案例
第一种解决办法:在where语句后面添加1=1
测试一下,看看控制台打印结果
2017-08-12 18:32:22,648 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpByConditions]-[DEBUG] ==> Preparing: select id,emp_name empName,emp_email empEmail, dept_id deptId from t_emp where 1=1 and emp_name=? and emp_name=? and dept_id=? 2017-08-12 18:32:22,717 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpByConditions]-[DEBUG] ==> Parameters: queen(String), queen(String), 1(Integer) 2017-08-12 18:32:22,771 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpByConditions]-[DEBUG] <== Total: 1 Emp [id=1, empName=queen, [email protected], deptId=1]
如上:控制台打印正常,结果查询出来了
第二种解决办法:使用MyBatis提供的where标签
对EmpMapper.xml文件进行改造,MyBatis使用where标签将所有的查询条件包括在内,MyBatis就会将where标签中拼装的SQL,多出来的and或者or去掉。
测试一下,看结果如何?
2017-08-12 18:36:50,512 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpByConditions]-[DEBUG] ==> Preparing: select id,emp_name empName,emp_email empEmail, dept_id deptId from t_emp WHERE emp_name=? and emp_name=? and dept_id=? 2017-08-12 18:36:50,561 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpByConditions]-[DEBUG] ==> Parameters: queen(String), queen(String), 1(Integer) 2017-08-12 18:36:50,606 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpByConditions]-[DEBUG] <== Total: 1 Emp [id=1, empName=queen, [email protected], deptId=1]打印SQL正常,结果正确。
使用第二种方式的时候,我们要注意 and 放置的位置,如果我们将and这样拼接,那会出现什么情况呢?如下:
修改测试方法
测试一下,看结果如何?
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 for the right syntax to use near '' at line 6 ### The error may exist in EmpMapper.xml ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: select id,emp_name empName,emp_email empEmail, dept_id deptId from t_emp WHERE emp_name=? and emp_name=? and ### 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 for the right syntax to use near '' at line 6 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
这种方式会报错,所以我们得出结论:where标签只会去掉第一个多出来and或者or,使用where标签时要把and放在前面
=======欢迎大家拍砖,小手一抖,多多点赞哟!=======
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。