其他
1.数字字符串以逗号分割,转成整形数组
String ids = "1,2,3,4,5";
List<Integer> idList = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
2.mysql的find_in_set()函数的使用
1 | select id, list, name from table where find_in_set( 'daodao' ,list); |
list不是常量时,IN只进行匹配第一个
list是常量,则可以直接用IN, 否则要用find_in_set()函数。
3.mysql的concat()和group_concat()函数的使用
3.1concat的使用
1、功能:将多个字符串连接成一个字符串。
2、语法:concat(str1, str2,...)
返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null。
3、举例:
例1:select concat (id, name, score) as info from tt2;
3.2 concat_ws()函数
1、功能:和concat()一样,将多个字符串连接成一个字符串,但是可以一次性指定分隔符~(concat_ws就是concat with separator)
2、语法:concat_ws(separator, str1, str2, ...)
说明:第一个参数指定分隔符。需要注意的是分隔符不能为null,如果为null,则返回结果为null。
3.3 group_concat()函数
1、功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果。
2、语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )
说明:通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。
1 | SELECT group_concat(town) FROM `players` group by town |
4.springboot2.X以上版本默认连接池Hikari
Hikari是一款非常强大,高效,并且号称“史上最快连接池”。由于其性能方面比较好,并且在springboot2.0之后,采用的默认数据库连接池就是Hikari,在引用parents后不用专门再添加依赖。
性能方面的比较:hikariCP>druid>tomcat-jdbc>dbcp>c3p0 。hikariCP的高性能得益于最大限度的避免锁竞争。
所以有了Hikari这个连接池,它是一个高速、免费、开源的JAVA连接池,它的性能几乎是C3P0、DBCP的25倍,十分强悍。
下面是连接池的相关配置信息:
application.yml中的配置
## 数据库配置 spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username = root spring.datasource.password = 123456 ## Hikari 连接池配置 ------ 详细配置请访问:https://github.com/brettwooldridge/HikariCP ## 最小空闲连接数量 spring.datasource.hikari.minimum-idle=5 ## 空闲连接存活最大时间,默认600000(10分钟) spring.datasource.hikari.idle-timeout=180000 ## 连接池最大连接数,默认是10 spring.datasource.hikari.maximum-pool-size=10 ## 此属性控制从池返回的连接的默认自动提交行为,默认值:true spring.datasource.hikari.auto-commit=true ## 连接池名称 spring.datasource.hikari.pool-name=MyHikariCP ## 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟 spring.datasource.hikari.max-lifetime=1800000 ## 数据库连接超时时间,默认30秒,即30000 spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.connection-test-query=SELECT 1
5.springboot切换使用undertow容器
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 默认是使用的tomcat -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- undertow容器支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
添加配置文件
@Component public class UndertowFactoryCustomizer implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> { @Override public void customize(UndertowServletWebServerFactory factory) { factory.addDeploymentInfoCustomizers(deploymentInfo -> { WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo(); webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 1024)); deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo); }); } }
6.自定义注解aop切面Log日志
7断言帮助类
public final class AssertUtil { private static final String[] IMG_EXTS = {"png", "jpg", "jpeg"}; /** * Don't let anyone instantiate this class */ private AssertUtil() { } /** * 判断给定的文件名后缀是否为图片 * * @param ext 文件名后缀, 不带点 * @param errMsg 错误信息 */ public static void isImgExt(String ext, String errMsg) { isImgExt(ext, 0, errMsg); } public static void isPositiveInt(Integer integer, String errMsg) { isTrue(integer != null && integer > 0, errMsg); } public static void isPositiveLong(Long value, String errMsg) { isTrue(value != null && value > 0, errMsg); } public static void isPositiveDecimal(BigDecimal decimal, String errMsg) { isTrue(decimal != null && decimal.compareTo(BigDecimal.ZERO) > 0, errMsg); } public static void isPositiveDecimal(BigDecimal decimal, int errcode, String errMsg) { isTrue(decimal != null && decimal.compareTo(BigDecimal.ZERO) > 0, errcode, errMsg); } public static void isNonnegativeDecimal(BigDecimal decimal, String errMsg) { isTrue(decimal != null && decimal.compareTo(BigDecimal.ZERO) >= 0, errMsg); } /** * 判断给定的文件名后缀是否为图片 * * @param ext 文件名后缀, 不带点 * @param errCode 断言失败的错误代码 * @param errMsg 错误信息 */ public static void isImgExt(String ext, int errCode, String errMsg) { if (StringUtils.isBlank(ext) || Arrays.stream(IMG_EXTS).noneMatch(img -> img.equalsIgnoreCase(ext))) { throw new BusinessException(errCode, errMsg); } } /** * 判断一个布尔表达式, 若表达式为{@code true}则抛出指定错误信息的{@code BusinessException}. * * @param expression 布尔表达式 * @param message 断言失败时的错误信息 * @throws BusinessException */ public static void notTrue(boolean expression, String message) throws BusinessException { notTrue(expression, 400, message); } /** * 判断一个布尔表达式, 若表达式为{@code true}则抛出指定错误信息的{@code BusinessException}. * * @param expression 布尔表达式 * @param errCode 断言失败时的错误代码 * @param message 断言失败时的错误信息 * @throws BusinessException */ public static void notTrue(boolean expression, int errCode, String message) throws BusinessException { if (expression) { throw new BusinessException(errCode, message); } } /** * 判断一个布尔表达式, 若表达式为{@code false}则抛出指定错误信息的{@code BusinessException}. * * @param expression 布尔表达式 * @param message 断言失败时的错误信息 * @throws BusinessException */ public static void isTrue(boolean expression, String message) throws BusinessException { isTrue(expression, 400, message); } /** * 判断一个布尔表达式, 若表达式为{@code false}则抛出指定错误信息的{@code BusinessException}. * * @param expression 布尔表达式 * @param errCode 断言失败时的错误代码 * @param message 断言失败时的错误信息 * @throws BusinessException */ public static void isTrue(boolean expression, int errCode, String message) throws BusinessException { if (!expression) { throw new BusinessException(errCode, message); } } /** * 如果对象为{@code null}, 则抛出异常 * * @param object 要判断的对象 * @throws BusinessException */ public static void notNull(Object object) throws BusinessException { notNull(object, "不能处理空对象"); } /** * 如果对象为{@code null}, 则抛出异常 * * @param object 要判断的对象 * @param message 断言失败时的错误信息 * @throws BusinessException */ public static void notNull(Object object, String message) throws BusinessException { notNull(object, 400, message); } /** * 如果对象为{@code null}, 则抛出异常 * * @param object 要判断的对象 * @param errCode 断言失败时的错误代码 * @param errMsg 断言失败时的错误信息 * @throws BusinessException */ public static void notNull(Object object, int errCode, String errMsg) throws BusinessException { if (object == null) { throw new BusinessException(errCode, errMsg); } } /** * 如果字符串为{@code null}、空字符串或仅包含空白字符, 则抛出异常 * * @param text 要进行检查的字符串 * @throws BusinessException */ public static void hasText(String text) throws BusinessException { hasText(text, 400, "参数不能为空字符串"); } /** * 如果字符串为{@code null}、空字符串或仅包含空白字符, 则抛出异常 * * @param text 要进行检查的字符串 * @param message 断言失败时的错误信息 * @throws BusinessException */ public static void hasText(String text, String message) throws BusinessException { hasText(text, 400, message); } /** * 如果字符串为{@code null}、空字符串或仅包含空白字符, 则抛出异常 * * @param text 要进行检查的字符串 * @param errCode 断言失败时的错误代码 * @param errMsg 错误信息 * @throws BusinessException */ public static void hasText(String text, int errCode, String errMsg) throws BusinessException { if (StringUtils.isBlank(text)) { throw new BusinessException(errCode, errMsg); } } /** * 如果数组为{@code null}或长度为0, 则抛出异常 * * @param array 要进行检查的数组 * @param message 断言失败时的错误信息 * @param <T> 数组的数据类型 * @throws BusinessException */ public static <T> void notEmpty(T[] array, String message) throws BusinessException { notEmpty(array, 400, message); } /** * 如果数组为{@code null}或长度为0, 则抛出异常 * * @param array 要进行检查的数组 * @param errCode 断言失败时的错误代码 * @param errMsg 错误信息 * @param <T> 数组的数据类型 * @throws BusinessException */ public static <T> void notEmpty(T[] array, int errCode, String errMsg) throws BusinessException { if (array == null || array.length == 0) { throw new BusinessException(errCode, errMsg); } } /** * 如果数组里包含有{@code null}的元素, 则抛出异常. 注意: 若数组本身为{@code null}则不会进行处理, 直接返回 * * @param array 要进行检查的数组 * @param message 断言失败时的错误信息 * @param <T> 数组的数据类型 * @throws BusinessException */ public static <T> void noNullElements(T[] array, String message) throws BusinessException { noNullElements(array, 400, message); } /** * 如果数组里包含有{@code null}的元素, 则抛出异常. 注意: 若数组本身为{@code null}则不会进行处理, 直接返回 * * @param array 要进行检查的数组 * @param errCode 断言失败时的错误代码 * @param errMsg 错误信息 * @param <T> 数组的数据类型 * @throws BusinessException */ public static <T> void noNullElements(T[] array, int errCode, String errMsg) throws BusinessException { if (array != null) { for (T element : array) { if (element == null) { throw new BusinessException(errCode, errMsg); } } } } /** * 如果集合为{@code null},或者不包含任何元素,则抛出异常 * * @param collection 要进行检查的集合 * @param message 断言失败时的错误信息 * @throws BusinessException */ public static void notEmpty(Collection<?> collection, String message) throws BusinessException { notEmpty(collection, 400, message); } /** * 如果集合为{@code null},或者不包含任何元素,则抛出异常 * * @param collection 要进行检查的集合 * @param errCode 断言失败时的错误代码 * @param errMsg 错误信息 * @throws BusinessException */ public static void notEmpty(Collection<?> collection, int errCode, String errMsg) throws BusinessException { if (collection == null || collection.isEmpty()) { throw new BusinessException(errCode, errMsg); } } /** * 如果键值对为{@code null},或者不包含任何键值,则抛出异常 * * @param map 要进行检查的键值对 * @param message 断言失败时的错误信息 * @throws BusinessException */ public static void notEmpty(Map<?, ?> map, String message) throws BusinessException { notEmpty(map, 400, message); } /** * 如果键值对为{@code null},或者不包含任何键值,则抛出异常 * * @param map 要进行检查的键值对 * @param errCode 断言失败时的错误代码 * @param errMsg 错误信息 * @throws BusinessException */ public static void notEmpty(Map<?, ?> map, int errCode, String errMsg) throws BusinessException { if (map == null || map.isEmpty()) { throw new BusinessException(errCode, errMsg); } } }
业务异常类
public class BusinessException extends RuntimeException { private int code = 400; public BusinessException(String message) { super(message); } public BusinessException(String message, Throwable cause) { super(message, cause); } public BusinessException(int code, String message) { super(message); this.code = code; } public BusinessException(int code, String message, Throwable cause) { super(message, cause); this.code = code; } public int getCode() { return code; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?