abel533 / MyBatis-Spring-Boot

Spring Boot集成MyBatis的基础项目
3.37k stars 1.59k forks source link

发现生成的sql与预期的不一样 #46

Open helloworldtang opened 7 years ago

helloworldtang commented 7 years ago

下面的代码在执行时,发现生成的sql与预期的不一样。预期是 UPDATE car_tt set name=? WHERE id = ?


        CarTtDO carTtDO=new CarTtDO();
        carTtDO.setId(1L);
        carTtDO.setName(null);
        carTtDOMapper.updateByPrimaryKey(carTtDO);
### SQL: UPDATE car_tt   WHERE  id = ?
### Cause: java.sql.SQLException: sql injection violation, syntax error: syntax error, error in :'tt   WHERE  id = ?',expect SET, actual WHERE WHERE : UPDATE car_tt   WHERE  id = ?
; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; sql injection violation, syntax error: syntax error, error in :'tt   WHERE  id = ?',expect SET, actual WHERE WHERE : UPDATE car_tt   WHERE  id = ?; nested exception is java.sql.SQLException: sql injection violation, syntax error: syntax error, error in :'tt   WHERE  id = ?',expect SET, actual WHERE WHERE : UPDATE car_tt   WHERE  id = ?
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
    at com.sun.proxy.$Proxy87.update(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy88.updateByPrimaryKeySelective(Unknown Source)
    at com.xiaoyi.app.business.car.biz.CarTtBiz.updateTt(CarTtBiz.java:103)
abel533 commented 7 years ago

测试看了,没有发现问题。 测试如下:

CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Country country = new Country();
country.setId(174);
country.setCountryname(null);
Assert.assertEquals(1, mapper.updateByPrimaryKey(country));

日志如下:

DEBUG [main] - ==>  Preparing: UPDATE country SET countryname = ?,countrycode = ? WHERE id = ? 
DEBUG [main] - ==> Parameters: null, null, 174(Integer)
DEBUG [main] - <==    Updates: 1
helloworldtang commented 7 years ago

是不是有些配置会导致Null会被过虑掉呢?

我这边还是有这个报错 updatebyprimarykeyselective

workcheng commented 7 years ago

很多接口就是根据字段是否为null来选择是否去做更新的,你换其他方法试试

helloworldtang commented 7 years ago

@workcheng 如果把gpsUrl字段赋值的话,sql就正常了。 这里面用的是通用mapper的方法,没有自己写的

abel533 commented 7 years ago

CarTtDO代码发出来看看。