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

學無先后,達者為師

網站首頁 編程語言 正文

Mybatis - tk.mybatis deleteByPrimaryKey無法正確識別主鍵

作者:daopinz 更新時間: 2022-04-21 編程語言

? ? 為了給項目其他人提供模塊的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  不能為空
 * @author liuzh
 */
@RegisterMapper
public interface DeleteByPrimaryKeyMapper {

    /**
     * 根據主鍵字段進行刪除,方法參數必須包含完整的主鍵屬性
     *
     * @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);

?

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

欄目分類
最近更新