1.添加依賴
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2.添加配置
# PageHelper分頁插件
pagehelper:
helperDialect: mysql
supportMethodsArguments: true
params: count=countSql
3.添加分頁工具類
PageUtils.java 這個類內(nèi)的方法是分頁最外層調(diào)用要用到
package com.example.springboot.util;
import com.example.springboot.common.core.page.PageDomain;
import com.example.springboot.common.core.page.TableSupport;
import com.example.springboot.common.core.util.SqlUtil;
import com.github.pagehelper.PageHelper;
/**
* 分頁工具類
*/
public class PageUtils extends PageHelper{
/**
* 設(shè)置請求分頁數(shù)據(jù)
*/
public static void startPage()
{
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
{
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
PageHelper.startPage(pageNum, pageSize, orderBy);
}
}
}
4.PageUtils.java內(nèi)引用到的工具類及其方法
PageDomain.java
package com.example.springboot.common.core.page;
import com.example.springboot.util.StringUtils;
/**
* 分頁數(shù)據(jù)
*/
public class PageDomain{
/** 當(dāng)前記錄起始索引 */
private Integer pageNum;
/** 每頁顯示記錄數(shù) */
private Integer pageSize;
/** 排序列 */
private String orderByColumn;
/** 排序的方向desc或者asc */
private String isAsc = "asc";
/** 分頁參數(shù)合理化 */
private Boolean reasonable = true;
public String getOrderBy()
{
if (StringUtils.isEmpty(orderByColumn))
{
return "";
}
return StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc;
}
public Integer getPageNum()
{
return pageNum;
}
public void setPageNum(Integer pageNum)
{
this.pageNum = pageNum;
}
public Integer getPageSize()
{
return pageSize;
}
public void setPageSize(Integer pageSize)
{
this.pageSize = pageSize;
}
public String getOrderByColumn()
{
return orderByColumn;
}
public void setOrderByColumn(String orderByColumn)
{
this.orderByColumn = orderByColumn;
}
public String getIsAsc()
{
return isAsc;
}
public void setIsAsc(String isAsc)
{
if (StringUtils.isNotEmpty(isAsc))
{
// 兼容前端排序類型
if ("ascending".equals(isAsc))
{
isAsc = "asc";
}
else if ("descending".equals(isAsc))
{
isAsc = "desc";
}
this.isAsc = isAsc;
}
}
public Boolean getReasonable()
{
if (StringUtils.isNull(reasonable))
{
return Boolean.TRUE;
}
return reasonable;
}
public void setReasonable(Boolean reasonable)
{
this.reasonable = reasonable;
}
}
TableSupport.java
package com.example.springboot.common.core.page;
import com.example.springboot.common.core.util.ServletUtils;
/**
* 表格數(shù)據(jù)處理
*/
public class TableSupport
{
/**
* 當(dāng)前記錄起始索引
*/
public static final String PAGE_NUM = "pageNum";
/**
* 每頁顯示記錄數(shù)
*/
public static final String PAGE_SIZE = "pageSize";
/**
* 排序列
*/
public static final String ORDER_BY_COLUMN = "orderByColumn";
/**
* 排序的方向 "desc" 或者 "asc".
*/
public static final String IS_ASC = "isAsc";
/**
* 分頁參數(shù)合理化
*/
public static final String REASONABLE = "reasonable";
/**
* 封裝分頁對象
*/
public static PageDomain getPageDomain()
{
PageDomain pageDomain = new PageDomain();
pageDomain.setPageNum(ServletUtils.getParameterToInt(PAGE_NUM));
pageDomain.setPageSize(ServletUtils.getParameterToInt(PAGE_SIZE));
pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE));
return pageDomain;
}
public static PageDomain buildPageRequest()
{
return getPageDomain();
}
}
CharsetKit.java
package com.example.springboot.common.core.util;
import com.example.springboot.util.StringUtils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* 字符集工具類
*/
public class CharsetKit
{
/** ISO-8859-1 */
public static final String ISO_8859_1 = "ISO-8859-1";
/** UTF-8 */
public static final String UTF_8 = "UTF-8";
/** GBK */
public static final String GBK = "GBK";
/** ISO-8859-1 */
public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO_8859_1);
/** UTF-8 */
public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8);
/** GBK */
public static final Charset CHARSET_GBK = Charset.forName(GBK);
/**
* 轉(zhuǎn)換為Charset對象
*
* @param charset 字符集桐绒,為空則返回默認(rèn)字符集
* @return Charset
*/
public static Charset charset(String charset)
{
return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
}
/**
* 轉(zhuǎn)換字符串的字符集編碼
*
* @param source 字符串
* @param srcCharset 源字符集单雾,默認(rèn)ISO-8859-1
* @param destCharset 目標(biāo)字符集移盆,默認(rèn)UTF-8
* @return 轉(zhuǎn)換后的字符集
*/
public static String convert(String source, String srcCharset, String destCharset)
{
return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));
}
/**
* 轉(zhuǎn)換字符串的字符集編碼
*
* @param source 字符串
* @param srcCharset 源字符集,默認(rèn)ISO-8859-1
* @param destCharset 目標(biāo)字符集,默認(rèn)UTF-8
* @return 轉(zhuǎn)換后的字符集
*/
public static String convert(String source, Charset srcCharset, Charset destCharset)
{
if (null == srcCharset)
{
srcCharset = StandardCharsets.ISO_8859_1;
}
if (null == destCharset)
{
destCharset = StandardCharsets.UTF_8;
}
if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset))
{
return source;
}
return new String(source.getBytes(srcCharset), destCharset);
}
/**
* @return 系統(tǒng)字符集編碼
*/
public static String systemCharset()
{
return Charset.defaultCharset().name();
}
}
Convert.java
package com.example.springboot.common.core.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.util.Set;
import com.example.springboot.util.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
/**
* 類型轉(zhuǎn)換器
*/
public class Convert
{
/**
* 轉(zhuǎn)換為字符串<br>
* 如果給定的值為null魔市,或者轉(zhuǎn)換失敗少辣,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static String toStr(Object value, String defaultValue)
{
if (null == value)
{
return defaultValue;
}
if (value instanceof String)
{
return (String) value;
}
return value.toString();
}
/**
* 轉(zhuǎn)換為字符串<br>
* 如果給定的值為<code>null</code>,或者轉(zhuǎn)換失敗浪秘,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static String toStr(Object value)
{
return toStr(value, null);
}
/**
* 轉(zhuǎn)換為字符<br>
* 如果給定的值為null,或者轉(zhuǎn)換失敗埠况,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Character toChar(Object value, Character defaultValue)
{
if (null == value)
{
return defaultValue;
}
if (value instanceof Character)
{
return (Character) value;
}
final String valueStr = toStr(value, null);
return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0);
}
/**
* 轉(zhuǎn)換為字符<br>
* 如果給定的值為<code>null</code>耸携,或者轉(zhuǎn)換失敗,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Character toChar(Object value)
{
return toChar(value, null);
}
/**
* 轉(zhuǎn)換為byte<br>
* 如果給定的值為<code>null</code>辕翰,或者轉(zhuǎn)換失敗夺衍,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Byte toByte(Object value, Byte defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof Byte)
{
return (Byte) value;
}
if (value instanceof Number)
{
return ((Number) value).byteValue();
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
return Byte.parseByte(valueStr);
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為byte<br>
* 如果給定的值為<code>null</code>,或者轉(zhuǎn)換失敗喜命,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Byte toByte(Object value)
{
return toByte(value, null);
}
/**
* 轉(zhuǎn)換為Short<br>
* 如果給定的值為<code>null</code>沟沙,或者轉(zhuǎn)換失敗,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Short toShort(Object value, Short defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof Short)
{
return (Short) value;
}
if (value instanceof Number)
{
return ((Number) value).shortValue();
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
return Short.parseShort(valueStr.trim());
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為Short<br>
* 如果給定的值為<code>null</code>壁榕,或者轉(zhuǎn)換失敗矛紫,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Short toShort(Object value)
{
return toShort(value, null);
}
/**
* 轉(zhuǎn)換為Number<br>
* 如果給定的值為空,或者轉(zhuǎn)換失敗护桦,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Number toNumber(Object value, Number defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof Number)
{
return (Number) value;
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
return NumberFormat.getInstance().parse(valueStr);
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為Number<br>
* 如果給定的值為空含衔,或者轉(zhuǎn)換失敗,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Number toNumber(Object value)
{
return toNumber(value, null);
}
/**
* 轉(zhuǎn)換為int<br>
* 如果給定的值為空,或者轉(zhuǎn)換失敗贪染,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Integer toInt(Object value, Integer defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof Integer)
{
return (Integer) value;
}
if (value instanceof Number)
{
return ((Number) value).intValue();
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
return Integer.parseInt(valueStr.trim());
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為int<br>
* 如果給定的值為<code>null</code>缓呛,或者轉(zhuǎn)換失敗,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Integer toInt(Object value)
{
return toInt(value, null);
}
/**
* 轉(zhuǎn)換為Integer數(shù)組<br>
*
* @param str 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Integer[] toIntArray(String str)
{
return toIntArray(",", str);
}
/**
* 轉(zhuǎn)換為Long數(shù)組<br>
*
* @param str 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Long[] toLongArray(String str)
{
return toLongArray(",", str);
}
/**
* 轉(zhuǎn)換為Integer數(shù)組<br>
*
* @param split 分隔符
* @param split 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Integer[] toIntArray(String split, String str)
{
if (StringUtils.isEmpty(str))
{
return new Integer[] {};
}
String[] arr = str.split(split);
final Integer[] ints = new Integer[arr.length];
for (int i = 0; i < arr.length; i++)
{
final Integer v = toInt(arr[i], 0);
ints[i] = v;
}
return ints;
}
/**
* 轉(zhuǎn)換為Long數(shù)組<br>
*
* @param split 分隔符
* @param str 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Long[] toLongArray(String split, String str)
{
if (StringUtils.isEmpty(str))
{
return new Long[] {};
}
String[] arr = str.split(split);
final Long[] longs = new Long[arr.length];
for (int i = 0; i < arr.length; i++)
{
final Long v = toLong(arr[i], null);
longs[i] = v;
}
return longs;
}
/**
* 轉(zhuǎn)換為String數(shù)組<br>
*
* @param str 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static String[] toStrArray(String str)
{
return toStrArray(",", str);
}
/**
* 轉(zhuǎn)換為String數(shù)組<br>
*
* @param split 分隔符
* @param split 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static String[] toStrArray(String split, String str)
{
return str.split(split);
}
/**
* 轉(zhuǎn)換為long<br>
* 如果給定的值為空杭隙,或者轉(zhuǎn)換失敗哟绊,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Long toLong(Object value, Long defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof Long)
{
return (Long) value;
}
if (value instanceof Number)
{
return ((Number) value).longValue();
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
// 支持科學(xué)計數(shù)法
return new BigDecimal(valueStr.trim()).longValue();
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為long<br>
* 如果給定的值為<code>null</code>,或者轉(zhuǎn)換失敗痰憎,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Long toLong(Object value)
{
return toLong(value, null);
}
/**
* 轉(zhuǎn)換為double<br>
* 如果給定的值為空票髓,或者轉(zhuǎn)換失敗,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Double toDouble(Object value, Double defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof Double)
{
return (Double) value;
}
if (value instanceof Number)
{
return ((Number) value).doubleValue();
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
// 支持科學(xué)計數(shù)法
return new BigDecimal(valueStr.trim()).doubleValue();
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為double<br>
* 如果給定的值為空铣耘,或者轉(zhuǎn)換失敗洽沟,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Double toDouble(Object value)
{
return toDouble(value, null);
}
/**
* 轉(zhuǎn)換為Float<br>
* 如果給定的值為空,或者轉(zhuǎn)換失敗蜗细,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Float toFloat(Object value, Float defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof Float)
{
return (Float) value;
}
if (value instanceof Number)
{
return ((Number) value).floatValue();
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
return Float.parseFloat(valueStr.trim());
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為Float<br>
* 如果給定的值為空裆操,或者轉(zhuǎn)換失敗,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Float toFloat(Object value)
{
return toFloat(value, null);
}
/**
* 轉(zhuǎn)換為boolean<br>
* String支持的值為:true炉媒、false踪区、yes、ok吊骤、no缎岗,1,0 如果給定的值為空,或者轉(zhuǎn)換失敗白粉,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static Boolean toBool(Object value, Boolean defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof Boolean)
{
return (Boolean) value;
}
String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
valueStr = valueStr.trim().toLowerCase();
switch (valueStr)
{
case "true":
return true;
case "false":
return false;
case "yes":
return true;
case "ok":
return true;
case "no":
return false;
case "1":
return true;
case "0":
return false;
default:
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為boolean<br>
* 如果給定的值為空传泊,或者轉(zhuǎn)換失敗,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static Boolean toBool(Object value)
{
return toBool(value, null);
}
/**
* 轉(zhuǎn)換為Enum對象<br>
* 如果給定的值為空蜗元,或者轉(zhuǎn)換失敗或渤,返回默認(rèn)值<br>
*
* @param clazz Enum的Class
* @param value 值
* @param defaultValue 默認(rèn)值
* @return Enum
*/
public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (clazz.isAssignableFrom(value.getClass()))
{
@SuppressWarnings("unchecked")
E myE = (E) value;
return myE;
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
return Enum.valueOf(clazz, valueStr);
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為Enum對象<br>
* 如果給定的值為空,或者轉(zhuǎn)換失敗奕扣,返回默認(rèn)值<code>null</code><br>
*
* @param clazz Enum的Class
* @param value 值
* @return Enum
*/
public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value)
{
return toEnum(clazz, value, null);
}
/**
* 轉(zhuǎn)換為BigInteger<br>
* 如果給定的值為空,或者轉(zhuǎn)換失敗掌敬,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static BigInteger toBigInteger(Object value, BigInteger defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof BigInteger)
{
return (BigInteger) value;
}
if (value instanceof Long)
{
return BigInteger.valueOf((Long) value);
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
return new BigInteger(valueStr);
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為BigInteger<br>
* 如果給定的值為空惯豆,或者轉(zhuǎn)換失敗,返回默認(rèn)值<code>null</code><br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static BigInteger toBigInteger(Object value)
{
return toBigInteger(value, null);
}
/**
* 轉(zhuǎn)換為BigDecimal<br>
* 如果給定的值為空奔害,或者轉(zhuǎn)換失敗楷兽,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @param defaultValue 轉(zhuǎn)換錯誤時的默認(rèn)值
* @return 結(jié)果
*/
public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue)
{
if (value == null)
{
return defaultValue;
}
if (value instanceof BigDecimal)
{
return (BigDecimal) value;
}
if (value instanceof Long)
{
return new BigDecimal((Long) value);
}
if (value instanceof Double)
{
return new BigDecimal((Double) value);
}
if (value instanceof Integer)
{
return new BigDecimal((Integer) value);
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr))
{
return defaultValue;
}
try
{
return new BigDecimal(valueStr);
}
catch (Exception e)
{
return defaultValue;
}
}
/**
* 轉(zhuǎn)換為BigDecimal<br>
* 如果給定的值為空,或者轉(zhuǎn)換失敗华临,返回默認(rèn)值<br>
* 轉(zhuǎn)換失敗不會報錯
*
* @param value 被轉(zhuǎn)換的值
* @return 結(jié)果
*/
public static BigDecimal toBigDecimal(Object value)
{
return toBigDecimal(value, null);
}
/**
* 將對象轉(zhuǎn)為字符串<br>
* 1芯杀、Byte數(shù)組和ByteBuffer會被轉(zhuǎn)換為對應(yīng)字符串的數(shù)組 2、對象數(shù)組會調(diào)用Arrays.toString方法
*
* @param obj 對象
* @return 字符串
*/
public static String utf8Str(Object obj)
{
return str(obj, CharsetKit.CHARSET_UTF_8);
}
/**
* 將對象轉(zhuǎn)為字符串<br>
* 1、Byte數(shù)組和ByteBuffer會被轉(zhuǎn)換為對應(yīng)字符串的數(shù)組 2揭厚、對象數(shù)組會調(diào)用Arrays.toString方法
*
* @param obj 對象
* @param charsetName 字符集
* @return 字符串
*/
public static String str(Object obj, String charsetName)
{
return str(obj, Charset.forName(charsetName));
}
/**
* 將對象轉(zhuǎn)為字符串<br>
* 1却特、Byte數(shù)組和ByteBuffer會被轉(zhuǎn)換為對應(yīng)字符串的數(shù)組 2、對象數(shù)組會調(diào)用Arrays.toString方法
*
* @param obj 對象
* @param charset 字符集
* @return 字符串
*/
public static String str(Object obj, Charset charset)
{
if (null == obj)
{
return null;
}
if (obj instanceof String)
{
return (String) obj;
}
else if (obj instanceof byte[])
{
return str((byte[]) obj, charset);
}
else if (obj instanceof Byte[])
{
byte[] bytes = ArrayUtils.toPrimitive((Byte[]) obj);
return str(bytes, charset);
}
else if (obj instanceof ByteBuffer)
{
return str((ByteBuffer) obj, charset);
}
return obj.toString();
}
/**
* 將byte數(shù)組轉(zhuǎn)為字符串
*
* @param bytes byte數(shù)組
* @param charset 字符集
* @return 字符串
*/
public static String str(byte[] bytes, String charset)
{
return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
}
/**
* 解碼字節(jié)碼
*
* @param data 字符串
* @param charset 字符集筛圆,如果此字段為空裂明,則解碼的結(jié)果取決于平臺
* @return 解碼后的字符串
*/
public static String str(byte[] data, Charset charset)
{
if (data == null)
{
return null;
}
if (null == charset)
{
return new String(data);
}
return new String(data, charset);
}
/**
* 將編碼的byteBuffer數(shù)據(jù)轉(zhuǎn)換為字符串
*
* @param data 數(shù)據(jù)
* @param charset 字符集,如果為空使用當(dāng)前系統(tǒng)字符集
* @return 字符串
*/
public static String str(ByteBuffer data, String charset)
{
if (data == null)
{
return null;
}
return str(data, Charset.forName(charset));
}
/**
* 將編碼的byteBuffer數(shù)據(jù)轉(zhuǎn)換為字符串
*
* @param data 數(shù)據(jù)
* @param charset 字符集太援,如果為空使用當(dāng)前系統(tǒng)字符集
* @return 字符串
*/
public static String str(ByteBuffer data, Charset charset)
{
if (null == charset)
{
charset = Charset.defaultCharset();
}
return charset.decode(data).toString();
}
// ----------------------------------------------------------------------- 全角半角轉(zhuǎn)換
/**
* 半角轉(zhuǎn)全角
*
* @param input String.
* @return 全角字符串.
*/
public static String toSBC(String input)
{
return toSBC(input, null);
}
/**
* 半角轉(zhuǎn)全角
*
* @param input String
* @param notConvertSet 不替換的字符集合
* @return 全角字符串.
*/
public static String toSBC(String input, Set<Character> notConvertSet)
{
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++)
{
if (null != notConvertSet && notConvertSet.contains(c[i]))
{
// 跳過不替換的字符
continue;
}
if (c[i] == ' ')
{
c[i] = '\u3000';
}
else if (c[i] < '\177')
{
c[i] = (char) (c[i] + 65248);
}
}
return new String(c);
}
/**
* 全角轉(zhuǎn)半角
*
* @param input String.
* @return 半角字符串
*/
public static String toDBC(String input)
{
return toDBC(input, null);
}
/**
* 替換全角為半角
*
* @param text 文本
* @param notConvertSet 不替換的字符集合
* @return 替換后的字符
*/
public static String toDBC(String text, Set<Character> notConvertSet)
{
char c[] = text.toCharArray();
for (int i = 0; i < c.length; i++)
{
if (null != notConvertSet && notConvertSet.contains(c[i]))
{
// 跳過不替換的字符
continue;
}
if (c[i] == '\u3000')
{
c[i] = ' ';
}
else if (c[i] > '\uFF00' && c[i] < '\uFF5F')
{
c[i] = (char) (c[i] - 65248);
}
}
String returnString = new String(c);
return returnString;
}
/**
* 數(shù)字金額大寫轉(zhuǎn)換 先寫個完整的然后將如零拾替換成零
*
* @param n 數(shù)字
* @return 中文大寫數(shù)字
*/
public static String digitUppercase(double n)
{
String[] fraction = { "角", "分" };
String[] digit = { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" };
String[][] unit = { { "元", "萬", "億" }, { "", "拾", "佰", "仟" } };
String head = n < 0 ? "負(fù)" : "";
n = Math.abs(n);
String s = "";
for (int i = 0; i < fraction.length; i++)
{
s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
}
if (s.length() < 1)
{
s = "整";
}
int integerPart = (int) Math.floor(n);
for (int i = 0; i < unit[0].length && integerPart > 0; i++)
{
String p = "";
for (int j = 0; j < unit[1].length && n > 0; j++)
{
p = digit[integerPart % 10] + unit[1][j] + p;
integerPart = integerPart / 10;
}
s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
}
return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
}
}
ServletUtils.java
package com.example.springboot.common.core.util;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.example.springboot.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* 客戶端工具類
*/
public class ServletUtils
{
/**
* 獲取String參數(shù)
*/
public static String getParameter(String name)
{
return getRequest().getParameter(name);
}
/**
* 獲取String參數(shù)
*/
public static String getParameter(String name, String defaultValue)
{
return Convert.toStr(getRequest().getParameter(name), defaultValue);
}
/**
* 獲取Integer參數(shù)
*/
public static Integer getParameterToInt(String name)
{
return Convert.toInt(getRequest().getParameter(name));
}
/**
* 獲取Integer參數(shù)
*/
public static Integer getParameterToInt(String name, Integer defaultValue)
{
return Convert.toInt(getRequest().getParameter(name), defaultValue);
}
/**
* 獲取Boolean參數(shù)
*/
public static Boolean getParameterToBool(String name)
{
return Convert.toBool(getRequest().getParameter(name));
}
/**
* 獲取Boolean參數(shù)
*/
public static Boolean getParameterToBool(String name, Boolean defaultValue)
{
return Convert.toBool(getRequest().getParameter(name), defaultValue);
}
/**
* 獲取request
*/
public static HttpServletRequest getRequest()
{
return getRequestAttributes().getRequest();
}
/**
* 獲取response
*/
public static HttpServletResponse getResponse()
{
return getRequestAttributes().getResponse();
}
/**
* 獲取session
*/
public static HttpSession getSession()
{
return getRequest().getSession();
}
public static ServletRequestAttributes getRequestAttributes()
{
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes) attributes;
}
/**
* 將字符串渲染到客戶端
*
* @param response 渲染對象
* @param string 待渲染的字符串
* @return null
*/
public static String renderString(HttpServletResponse response, String string)
{
try
{
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
/**
* 是否是Ajax異步請求
*
* @param request
*/
public static boolean isAjaxRequest(HttpServletRequest request)
{
String accept = request.getHeader("accept");
if (accept != null && accept.indexOf("application/json") != -1)
{
return true;
}
String xRequestedWith = request.getHeader("X-Requested-With");
if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
{
return true;
}
String uri = request.getRequestURI();
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
{
return true;
}
String ajax = request.getParameter("__ajax");
if (StringUtils.inStringIgnoreCase(ajax, "json", "xml"))
{
return true;
}
return false;
}
}
SqlUtil.java
package com.example.springboot.common.core.util;
import com.example.springboot.util.StringUtils;
import com.sun.xml.internal.ws.util.UtilException;
/**
* sql操作工具類
*/
public class SqlUtil
{
/**
* 定義常用的 sql關(guān)鍵字
*/
public static String SQL_REGEX = "select |insert |delete |update |drop |count |exec |chr |mid |master |truncate |char |and |declare ";
/**
* 僅支持字母闽晦、數(shù)字、下劃線提岔、空格仙蛉、逗號、小數(shù)點(支持多個字段排序)
*/
public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+";
/**
* 檢查字符碱蒙,防止注入繞過
*/
public static String escapeOrderBySql(String value)
{
if (StringUtils.isNotEmpty(value) && !isValidOrderBySql(value))
{
throw new UtilException("參數(shù)不符合規(guī)范荠瘪,不能進(jìn)行查詢");
}
return value;
}
/**
* 驗證 order by 語法是否符合規(guī)范
*/
public static boolean isValidOrderBySql(String value)
{
return value.matches(SQL_PATTERN);
}
/**
* SQL關(guān)鍵字檢查
*/
public static void filterKeyword(String value)
{
if (StringUtils.isEmpty(value))
{
return;
}
String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|");
for (int i = 0; i < sqlKeywords.length; i++)
{
if (StringUtils.indexOfIgnoreCase(value, sqlKeywords[i]) > -1)
{
throw new UtilException("參數(shù)存在SQL注入風(fēng)險");
}
}
}
}
StringUtils.java
package com.example.springboot.util;
import java.util.*;
/**
* 字符串工具類
*
* @author 鐘俊 (zhongjun), email: zhongjun@toguide.cn, date: 5/12/2021 2:11 PM
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils {
/**
* 空字符串
*/
private static final String NULLSTR = "";
/**
* 下劃線
*/
private static final char SEPARATOR = '_';
/**
* 獲取參數(shù)不為空值
*
* @param value defaultValue 要判斷的value
* @return value 返回值
*/
public static <T> T nvl(T value, T defaultValue) {
return value != null ? value : defaultValue;
}
/**
* * 判斷一個Collection是否為空, 包含List振亮,Set巧还,Queue
*
* @param coll 要判斷的Collection
* @return true:為空 false:非空
*/
public static boolean isEmpty(Collection<?> coll) {
return isNull(coll) || coll.isEmpty();
}
/**
* * 判斷一個Collection是否非空,包含List坊秸,Set麸祷,Queue
*
* @param coll 要判斷的Collection
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
}
/**
* * 判斷一個對象數(shù)組是否為空
*
* @param objects 要判斷的對象數(shù)組
* * @return true:為空 false:非空
*/
public static boolean isEmpty(Object[] objects) {
return isNull(objects) || (objects.length == 0);
}
/**
* * 判斷一個對象數(shù)組是否非空
*
* @param objects 要判斷的對象數(shù)組
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Object[] objects) {
return !isEmpty(objects);
}
/**
* * 判斷一個Map是否為空
*
* @param map 要判斷的Map
* @return true:為空 false:非空
*/
public static boolean isEmpty(Map<?, ?> map) {
return isNull(map) || map.isEmpty();
}
/**
* * 判斷一個Map是否為空
*
* @param map 要判斷的Map
* @return true:非空 false:空
*/
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
/**
* * 判斷一個字符串是否為空串
*
* @param str String
* @return true:為空 false:非空
*/
public static boolean isEmpty(String str) {
return isNull(str) || NULLSTR.equals(str.trim());
}
/**
* * 判斷一個字符串是否為非空串
*
* @param str String
* @return true:非空串 false:空串
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* * 判斷一個對象是否為空
*
* @param object Object
* @return true:為空 false:非空
*/
public static boolean isNull(Object object) {
return object == null;
}
/**
* * 判斷一個對象是否非空
*
* @param object Object
* @return true:非空 false:空
*/
public static boolean isNotNull(Object object) {
return !isNull(object);
}
/**
* * 判斷一個對象是否是數(shù)組類型(Java基本型別的數(shù)組)
*
* @param object 對象
* @return true:是數(shù)組 false:不是數(shù)組
*/
public static boolean isArray(Object object) {
return isNotNull(object) && object.getClass().isArray();
}
/**
* 去空格
*/
public static String trim(String str) {
return (str == null ? "" : str.trim());
}
/**
* 截取字符串
*
* @param str 字符串
* @param start 開始
* @return 結(jié)果
*/
public static String substring(final String str, int start) {
if (str == null) {
return NULLSTR;
}
if (start < 0) {
start = str.length() + start;
}
if (start < 0) {
start = 0;
}
if (start > str.length()) {
return NULLSTR;
}
return str.substring(start);
}
/**
* 截取字符串
*
* @param str 字符串
* @param start 開始
* @param end 結(jié)束
* @return 結(jié)果
*/
public static String substring(final String str, int start, int end) {
if (str == null) {
return NULLSTR;
}
if (end < 0) {
end = str.length() + end;
}
if (start < 0) {
start = str.length() + start;
}
if (end > str.length()) {
end = str.length();
}
if (start > end) {
return NULLSTR;
}
if (start < 0) {
start = 0;
}
if (end < 0) {
end = 0;
}
return str.substring(start, end);
}
/**
* 字符串轉(zhuǎn)set
*
* @param str 字符串
* @param sep 分隔符
* @return set集合
*/
public static final Set<String> str2Set(String str, String sep) {
return new HashSet<String>(str2List(str, sep, true, false));
}
/**
* 字符串轉(zhuǎn)list
*
* @param str 字符串
* @param sep 分隔符
* @param filterBlank 過濾純空白
* @param trim 去掉首尾空白
* @return list集合
*/
public static final List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) {
List<String> list = new ArrayList<String>();
if (StringUtils.isEmpty(str)) {
return list;
}
// 過濾空白字符串
if (filterBlank && StringUtils.isBlank(str)) {
return list;
}
String[] split = str.split(sep);
for (String string : split) {
if (filterBlank && StringUtils.isBlank(string)) {
continue;
}
if (trim) {
string = string.trim();
}
list.add(string);
}
return list;
}
/**
* 駝峰轉(zhuǎn)下劃線命名
*/
public static String toUnderScoreCase(String str) {
if (str == null) {
return null;
}
StringBuilder sb = new StringBuilder();
// 前置字符是否大寫
boolean preCharIsUpperCase = true;
// 當(dāng)前字符是否大寫
boolean curreCharIsUpperCase = true;
// 下一字符是否大寫
boolean nexteCharIsUpperCase = true;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (i > 0) {
preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
} else {
preCharIsUpperCase = false;
}
curreCharIsUpperCase = Character.isUpperCase(c);
if (i < (str.length() - 1)) {
nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
}
if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
sb.append(SEPARATOR);
} else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
sb.append(SEPARATOR);
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
/**
* 是否包含字符串
*
* @param str 驗證字符串
* @param strs 字符串組
* @return 包含返回true
*/
public static boolean inStringIgnoreCase(String str, String... strs) {
if (str != null && strs != null) {
for (String s : strs) {
if (str.equalsIgnoreCase(trim(s))) {
return true;
}
}
}
return false;
}
/**
* 將下劃線大寫方式命名的字符串轉(zhuǎn)換為駝峰式。如果轉(zhuǎn)換前的下劃線大寫方式命名的字符串為空褒搔,則返回空字符串阶牍。 例如:HELLO_WORLD->HelloWorld
*
* @param name 轉(zhuǎn)換前的下劃線大寫方式命名的字符串
* @return 轉(zhuǎn)換后的駝峰式命名的字符串
*/
public static String convertToCamelCase(String name) {
StringBuilder result = new StringBuilder();
// 快速檢查
if (name == null || name.isEmpty()) {
// 沒必要轉(zhuǎn)換
return "";
} else if (!name.contains("_")) {
// 不含下劃線,僅將首字母大寫
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
// 用下劃線將原始字符串分割
String[] camels = name.split("_");
for (String camel : camels) {
// 跳過原始字符串中開頭星瘾、結(jié)尾的下?lián)Q線或雙重下劃線
if (camel.isEmpty()) {
continue;
}
// 首字母大寫
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
return result.toString();
}
/**
* 駝峰式命名法 例如:user_name->userName
*/
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == SEPARATOR) {
upperCase = true;
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {
sb.append(c);
}
}
return sb.toString();
}
@SuppressWarnings("unchecked")
public static <T> T cast(Object obj) {
return (T) obj;
}
}
5.controller層調(diào)用
@GetMapping("/a01")
public List<User> testSelect1() {
PageUtils.startPage();
List<User> userList = userMapper.selectList(null);
return userList;
}
6.postman調(diào)用傳參
當(dāng)前記錄起始索引 pageNum
每頁顯示記錄數(shù) pageSize
親測有效可用