mybatis中幾種typeHandler的定義使用詳解
1.存儲(chǔ)到數(shù)據(jù)庫(kù), 將LONG數(shù)組轉(zhuǎn)換成字符串;從數(shù)據(jù)庫(kù)獲取數(shù)據(jù), 將字符串轉(zhuǎn)為L(zhǎng)ONG數(shù)組
package com.winturn.utils.handler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.winturn.utils.CommonJsonUtil;
/**
* <p>Class: ArrayLongTypeHandler.java</p>
* <p>Description: 存儲(chǔ)到數(shù)據(jù)庫(kù), 將LONG數(shù)組轉(zhuǎn)換成字符串;
* 從數(shù)據(jù)庫(kù)獲取數(shù)據(jù), 將字符串轉(zhuǎn)為L(zhǎng)ONG數(shù)組.
</p>*/
public class ArrayLongTypeHandler extends BaseTypeHandler<Object> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i,
Object parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, CommonJsonUtil.stringify(parameter));
}
@Override
public Object getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return CommonJsonUtil.parse3(rs.getString(columnName), Object.class);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return CommonJsonUtil.parse3(rs.getString(columnIndex), Object.class);
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return CommonJsonUtil.parse3(cs.getString(columnIndex), Object.class);
}
}
2.存儲(chǔ)到數(shù)據(jù)庫(kù), 將基本數(shù)據(jù)數(shù)組轉(zhuǎn)換成字符串;從數(shù)據(jù)庫(kù)獲取數(shù)據(jù), 將字符串根據(jù)','拆分,轉(zhuǎn)為數(shù)組.
package com.winturn.utils.handler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.winturn.utils.CommonJsonUtil;
/**
* <p>Class: ArrayStringTypeHandler.java</p>
* <p>Description: 存儲(chǔ)到數(shù)據(jù)庫(kù), 將基本數(shù)據(jù)數(shù)組轉(zhuǎn)換成字符串;
* 從數(shù)據(jù)庫(kù)獲取數(shù)據(jù), 將字符串根據(jù)','拆分,轉(zhuǎn)為數(shù)組.</p>
*
*
*/
public class ArrayStringTypeHandler extends BaseTypeHandler<Object> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter,
JdbcType jdbcType) throws SQLException {
ps.setString(i, CommonJsonUtil.stringify(parameter));
}
@Override
public Object getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return CommonJsonUtil.parse2(rs.getString(columnName), Object.class);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return CommonJsonUtil.parse2(rs.getString(columnIndex), Object.class);
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return CommonJsonUtil.parse2(cs.getString(columnIndex), Object.class);
}
}
3.jsonarray 格式的字符串轉(zhuǎn)換為相應(yīng)的數(shù)組
package com.winturn.utils.handler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.winturn.utils.CommonJsonUtil;
/**
* <p>Class: ArrayIntegerTypeHandler.java</p>
* <p>Description: jsonarray 格式的字符串轉(zhuǎn)換為相應(yīng)的數(shù)組 </p>
*
*/
public class JsonArrayTypeHandler extends BaseTypeHandler<Object> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i,
Object parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, CommonJsonUtil.stringify(parameter));
}
@Override
public Object getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return CommonJsonUtil.parseJsonToArray(rs.getString(columnName), Object.class);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return CommonJsonUtil.parseJsonToArray(rs.getString(columnIndex), Object.class);
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return CommonJsonUtil.parseJsonToArray(cs.getString(columnIndex), Object.class);
}
}
4.將Float類型的數(shù)組裝換成字符創(chuàng)進(jìn)行存儲(chǔ)
package com.winturn.utils.handler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.winturn.utils.CommonJsonUtil;
/**
* <p>Filename:JsonFloatTypeHandler.java</p>
* <p>Description: 將float類型數(shù)組裝換成字符串
</p>
*
*/
public class JsonFloatTypeHandler extends BaseTypeHandler<Object> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i,
Object parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, CommonJsonUtil.stringifyObject(parameter));
}
@Override
public Object getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return CommonJsonUtil.parseJsonToFloat(rs.getString(columnName), Object.class);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return CommonJsonUtil.parseJsonToFloat(rs.getString(columnIndex), Object.class);
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return CommonJsonUtil.parseJsonToFloat(cs.getString(columnIndex), Object.class);
}
}
5.將map裝換成字符串存儲(chǔ)到數(shù)據(jù)庫(kù),取出時(shí)將字符串裝換成map
package com.winturn.utils.handler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.codehaus.jackson.map.ObjectMapper;
import com.winturn.exceptions.RolerServiceException;
import com.winturn.utils.JsonMapUtil;
/**
*
* @ClassName: JsonMapTypeHandler
* @Description: 將map裝換成數(shù)組存儲(chǔ)數(shù)據(jù)庫(kù),取出時(shí)將字符串裝換成map
* @author sgl
* @date 2015年12月21日 下午6:22:50
*/
public class JsonMapTypeHandler extends BaseTypeHandler<Map<String, Object>> {
ObjectMapper mapper = new ObjectMapper();
@Override
public Map<String, Object> getNullableResult(ResultSet rs, String columnName) {
try {
String value = rs.getString(columnName);
return mapper.readValue(value, Map.class);
} catch (Exception e) {
}
return null;
}
@Override
public Map<String, Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
try {
String value = rs.getString(columnIndex);
return mapper.readValue(value, Map.class);
} catch (Exception e) {
}
return null;
}
@Override
public Map<String, Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
try {
String value = cs.getString(columnIndex);
return mapper.readValue(value, Map.class);
} catch (Exception e) {
}
return null;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType)
throws SQLException {
if (parameter == null) {
ps.setNull(i, Types.VARCHAR);
} else {
try {
ps.setString(i, JsonMapUtil.getJsonStrByMap(parameter));
} catch (RolerServiceException e) {
e.printStackTrace();
}
}
}
}
到此這篇關(guān)于mybatis中幾種typeHandler的定義使用的文章就介紹到這了,更多相關(guān)mybatis typeHandler定義使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于@Autowierd && @Resource 你真的了解嗎
這篇文章主要介紹了關(guān)于@Autowierd && @Resource的具體使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登錄與退出功能
這篇文章主要介紹了Springboot繼承Keycloak實(shí)現(xiàn)單點(diǎn)登陸與退出,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
如何在Java SpringBoot項(xiàng)目中配置動(dòng)態(tài)數(shù)據(jù)源你知道嗎
這篇文章主要介紹了SpringBoot如何在運(yùn)行時(shí)動(dòng)態(tài)添加數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
java并發(fā)編程專題(八)----(JUC)實(shí)例講解CountDownLatch
這篇文章主要介紹了java CountDownLatch的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
SpringBoot實(shí)現(xiàn)讀取YML,yaml,properties文件
yml,yaml,properties三種文件都是用來(lái)存放配置的文件,一些靜態(tài)數(shù)據(jù),配置的數(shù)據(jù)都會(huì)存放到里邊。本文主要為大家整理了SpringBoot實(shí)現(xiàn)讀取YML,yaml,properties文件的方法,需要的可以參考一下2023-04-04
Intellij Idea修改代碼方法參數(shù)自動(dòng)提示快捷鍵的操作
這篇文章主要介紹了Intellij Idea修改代碼方法參數(shù)自動(dòng)提示快捷鍵的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01

