Mybatis - tk.mybatis deleteByPrimaryKey无法正确识别主键

Posted Zhang Daopin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis - tk.mybatis deleteByPrimaryKey无法正确识别主键相关的知识,希望对你有一定的参考价值。

    为了给项目其他人提供模块的swagger服务,在本地window里安装了ubuntu子系统,将模块服务运行在Tomcat中,奇怪的是,每天晚上下班时启动服务,早上上班来就会看到日志catalina.out文件都会暴增到10G左右,今天刚好有空,来查一查这个问题。。。 

org.springframework.dao.DataIntegrityViolationException:
### Error updating database.  Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1' for column 'created' at row 1
### The error may involve com.xxx.xxx.repository.xxx.deleteByPrimaryKey-Inline
### The error occurred while setting parameters
### SQL: DELETE FROM xxxx WHERE  id = ? AND cid = ? AND xxx_id = ? AND xxx_num = ? AND created = ? AND xxx = ?
### Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1' for column 'created' at row 1
; Data truncation: Incorrect datetime value: '1' for column 'created' at row 1; nested exception is com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1' for column 'created' at row 1
        at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:104)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
        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.$Proxy45.delete(Unknown Source)
        at org.mybatis.spring.SqlSessionTemplate.delete(SqlSessionTemplate.java:310)
        at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
        at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
        at com.sun.proxy.$Proxy96.deleteByPrimaryKey(Unknown Source)

    首先日志中出现大量DELETE FROM 的SQL语句,但是参数却有点怪怪的。。。

JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3cd9a44f] will not be managed by Spring
==>  Preparing: DELETE FROM xxx WHERE id = ? AND cid = ? AND xxx_id = ? AND xxx_num = ? AND created = ? AND xxx = ?
==> Parameters: 1(Long), 1(Long), 1(Long), 1(Long), 1(Long), 1(Long)

    对应的代码部分是: 

xxxxMapper.deleteByPrimaryKey(v.getId());

    这个是Mybatis自带的根据主键ID删除表数据的方法啊,怎么会出现问题???

package tk.mybatis.mapper.common.base.delete;

import org.apache.ibatis.annotations.DeleteProvider;
import tk.mybatis.mapper.annotation.RegisterMapper;
import tk.mybatis.mapper.provider.base.BaseDeleteProvider;

/**
 * 通用Mapper接口,删除
 *
 * @param <T> 不能为空
 * @author liuzh
 */
@RegisterMapper
public interface DeleteByPrimaryKeyMapper<T> {

    /**
     * 根据主键字段进行删除,方法参数必须包含完整的主键属性
     *
     * @param key
     * @return
     */
    @DeleteProvider(type = BaseDeleteProvider.class, method = "dynamicSQL")
    int deleteByPrimaryKey(Object key);

}

    经过排查,发现是因为在实体类的id参数,没有配置主键ID的注解,导致mybatis找不到这条删除语句的主键,所以才会出现这个问题。

    需,加上@Id注解,表明该id是主键ID,才能保证删除逻辑执行正常。

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    ps:当然,也可以直接手写SQL语句,进行删除(不推荐)

    @Delete("DELETE FROM xxxx WHERE id=#{id}")
    void deleteInfoById(@Param("id") Long id);

 

以上是关于Mybatis - tk.mybatis deleteByPrimaryKey无法正确识别主键的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis - tk.mybatis deleteByPrimaryKey无法正确识别主键

Mybatis - tk.mybatis deleteByPrimaryKey无法正确识别主键

Mybatis - tk.mybatis deleteByPrimaryKey无法正确识别主键

达梦数据库适配:DM8+SpringBoot+HiKari+MyBatis3.4.6+tk.MyBatis+PageHelper

SpringBoot框架之通用mapper插件(tk.mybatis)

springboot集成报错,想要集成tk.mybatis报错,反射方法异常