日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Mybatis - tk.mybatis deleteByPrimaryKey無(wú)法正確識(shí)別主鍵

作者:daopinz 更新時(shí)間: 2022-04-21 編程語(yǔ)言

? ? 為了給項(xiàng)目其他人提供模塊的swagger服務(wù),在本地window里安裝了ubuntu子系統(tǒng),將模塊服務(wù)運(yùn)行在Tomcat中,奇怪的是,每天晚上下班時(shí)啟動(dòng)服務(wù),早上上班來(lái)就會(huì)看到日志catalina.out文件都會(huì)暴增到10G左右,今天剛好有空,來(lái)查一查這個(gè)問(wèn)題。。。?

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)

? ? 首先日志中出現(xiàn)大量DELETE FROM 的SQL語(yǔ)句,但是參數(shù)卻有點(diǎn)怪怪的。。。

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)

? ? 對(duì)應(yīng)的代碼部分是:?

xxxxMapper.deleteByPrimaryKey(v.getId());

? ? 這個(gè)是Mybatis自帶的根據(jù)主鍵ID刪除表數(shù)據(jù)的方法啊,怎么會(huì)出現(xiàn)問(wèn)題???

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  不能為空
 * @author liuzh
 */
@RegisterMapper
public interface DeleteByPrimaryKeyMapper {

    /**
     * 根據(jù)主鍵字段進(jìn)行刪除,方法參數(shù)必須包含完整的主鍵屬性
     *
     * @param key
     * @return
     */
    @DeleteProvider(type = BaseDeleteProvider.class, method = "dynamicSQL")
    int deleteByPrimaryKey(Object key);

}

? ? 經(jīng)過(guò)排查,發(fā)現(xiàn)是因?yàn)樵趯?shí)體類的id參數(shù),沒(méi)有配置主鍵ID的注解,導(dǎo)致mybatis找不到這條刪除語(yǔ)句的主鍵,所以才會(huì)出現(xiàn)這個(gè)問(wèn)題。

? ? 需,加上@Id注解,表明該id是主鍵ID,才能保證刪除邏輯執(zhí)行正常。

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

? ? ps:當(dāng)然,也可以直接手寫(xiě)SQL語(yǔ)句,進(jìn)行刪除(不推薦)

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

?

原文鏈接:https://zhangdaopin.blog.csdn.net/article/details/118149475

欄目分類
最近更新