解決mybatis plus字段為null或空字符串無法保存到數(shù)據(jù)庫的問題
背景
項(xiàng)目中集成了mybatis plus, 今天在做后臺(tái)的一個(gè)常規(guī)的增刪改查時(shí),發(fā)現(xiàn)字段值為null時(shí),這個(gè)字段不會(huì)被保存到數(shù)據(jù)庫
解決辦法
在字段上加上
@TableField(strategy = FieldStrategy.IGNORED)
strategy字段更新插入策略屬性說明:
IGNORED(0): “忽略判斷”, 所有字段都更新和插入
NOT_NULL(1): “非 NULL 判斷”, 只更新和插入非NULL值
NOT_EMPTY(2): “非空判斷”, 只更新和插入非NULL值且非空字符串
另外一種方式可全局配置,未親手實(shí)踐。
補(bǔ)充:Mybatis查詢數(shù)據(jù)部分字段顯示為null,怎么轉(zhuǎn)成空串("")
1、先定義一個(gè)handler,來把字段為null的轉(zhuǎn)成空串("")
2、在Mapper.xml中,把可能為空的字段,加上typeHandler屬性,指定處理的handler類的全路徑。
CustomStringTypeHandler.java
package com.wang.common.mybatis.handler;
import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Name: CustomStringTypeHandler
* @Desc: 自定義mybatis處理類,將null返回為空串(‘')
* @Author: Administrator
* @Date: 2019-09-03 18:20
*/
@MappedTypes({String.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class CustomStringTypeHandler extends BaseTypeHandler<String> {
@Override
public String getResult(ResultSet rs, String columnName) {
String result;
try {
result = getNullableResult(rs, columnName);
} catch (Exception e) {
throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e);
}
return result;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, parameter);
}
@Override
public String getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getString(columnName) == null? "" : rs.getString(columnName);
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getString(columnIndex) == null? "" : rs.getString(columnIndex);
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getString(columnIndex) == null? "" : cs.getString(columnIndex);
}
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wang.sss.fw.mapper.BusinessTripMapper"> <resultMap id="BaseResultMap" type="com.wang.sss.fw.pojo.BusinessTrip"> <result column="REQUEST_ID" jdbcType="VARCHAR" property="requestId" /> <result column="JOB_NUMBER" jdbcType="VARCHAR" property="jobNumber" /> <result column="REQUEST_DATE" jdbcType="VARCHAR" property="requestDate" /> <result column="DEPARTMENT" jdbcType="VARCHAR" property="department" /> <result column="BUSINESS_DAYS" jdbcType="VARCHAR" property="businessDays"/> <result column="CFD" jdbcType="VARCHAR" property="cfd" /> <result column="MDD" jdbcType="VARCHAR" property="mdd" /> <result column="START_TIME" jdbcType="VARCHAR" property="startTime" /> <result column="END_TIME" jdbcType="VARCHAR" property="endTime" /> <result column="REASON" jdbcType="VARCHAR" property="reason" typeHandler="com.wang.common.mybatis.handler.CustomStringTypeHandler"/> <result column="REMARK" jdbcType="VARCHAR" property="remark" typeHandler="com.wang.common.mybatis.handler.CustomStringTypeHandler"/> </resultMap> </mapper>
沒有加typeHandler屬性,處理之前的查詢結(jié)果:
BusinessTrip(requestId=11925, jobNumber=5721, requestDate=2019-05-06, department=57, businessDays=21, cfd=上海, mdd=南京, startTime=2019-05-06 13:36, endTime=2019-05-07 13:36, reason=null, remark=null)
增加typeHandler屬性,處理后的結(jié)果:(reason和remark字段都變成了空串)
BusinessTrip(requestId=11925, jobNumber=5721, requestDate=2019-05-06, department=57, businessDays=21, cfd=上海, mdd=南京, startTime=2019-05-06 13:36, endTime=2019-05-07 13:36, reason=, remark=)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
MyBatis中resultMap和resultType的區(qū)別詳解
這篇文章主要介紹了MyBatis中resultMap和resultType的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車剖析
這篇文章主要為大家介紹了java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車的示例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
JAVA實(shí)現(xiàn)用戶抽獎(jiǎng)功能(附完整代碼)
這篇文章主要給大家介紹了關(guān)于JAVA實(shí)現(xiàn)用戶抽獎(jiǎng)功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java中?springcloud.openfeign應(yīng)用案例解析
使用OpenFeign能讓編寫Web?Service客戶端更加簡單,使用時(shí)只需定義服務(wù)接口,然后在上面添加注解,OpenFeign也支持可拔插式的編碼和解碼器,這篇文章主要介紹了Java中?springcloud.openfeign應(yīng)用案例解析,需要的朋友可以參考下2024-06-06
SpringBoot日程管理Quartz與定時(shí)任務(wù)Task實(shí)現(xiàn)詳解
定時(shí)任務(wù)是企業(yè)級(jí)開發(fā)中必不可少的組成部分,諸如長周期業(yè)務(wù)數(shù)據(jù)的計(jì)算,例如年度報(bào)表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報(bào)告,還有搶購類活動(dòng)的商品上架,這些都離不開定時(shí)任務(wù)。本節(jié)將介紹兩種不同的定時(shí)任務(wù)技術(shù)2022-09-09

