当前位置: 首页 > news >正文

结合mybatis-plus实现Function获取java实体类的属性名

1、工具类

package com.yh.tunnel.util;import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.google.common.base.CaseFormat;
import com.yh.tunnel.domain.Plan;import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;/*** @Description* @Date 2019/7/18 10:31* @Author zsj*/
public class TableUtil {//驼峰命名 转下划线命名public static String getColumnName(String fieldName) {String column = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName);return column;}//  下划线命名  转 驼峰命名public static String getFieldName(String columuName) {String fieldName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columuName);return fieldName;}/*** 获取实体类的字段名称*/public static <T> String getFieldName(SFunction<T, ?> fn) {SerializedLambda serializedLambda = getSerializedLambda(fn);// 从lambda信息取出method、field、class等String fieldName = serializedLambda.getImplMethodName().substring("get".length());fieldName = fieldName.replaceFirst(fieldName.charAt(0) + "", (fieldName.charAt(0) + "").toLowerCase());Field field;try {field = Class.forName(serializedLambda.getImplClass().replace("/", ".")).getDeclaredField(fieldName);} catch (ClassNotFoundException | NoSuchFieldException e) {throw new RuntimeException(e);}return fieldName.replaceAll("[A-Z]", "$0");}private static <T> SerializedLambda getSerializedLambda(SFunction<T, ?> fn) {// 从function取出序列化方法Method writeReplaceMethod;try {writeReplaceMethod = fn.getClass().getDeclaredMethod("writeReplace");} catch (NoSuchMethodException e) {throw new RuntimeException(e);}// 从序列化方法取出序列化的lambda信息boolean isAccessible = writeReplaceMethod.isAccessible();writeReplaceMethod.setAccessible(true);SerializedLambda serializedLambda;try {serializedLambda = (SerializedLambda) writeReplaceMethod.invoke(fn);} catch (IllegalAccessException | InvocationTargetException e) {throw new RuntimeException(e);}writeReplaceMethod.setAccessible(isAccessible);return serializedLambda;}public static void main(String[] args) {SFunction<Plan, String> getProperty1Function = Plan::getTunnelId;String propertyName = getFieldName(getProperty1Function);System.out.println("属性名: " + propertyName);}}

2、mybatis-plus 中文排序

 SFunction<Plan, String> getProperty1Function = Plan::getName;String propertyName = TableUtil.getFieldName(getProperty1Function);String coloumName = TableUtil.getColumnName(propertyName);QueryWrapper<Plan> wrapper = new QueryWrapper<>();wrapper.lambda().ne(Plan::getName, "无预警");wrapper.orderByDesc("CAST(" + coloumName + " AS CHAR CHARACTER SET gbk)");

http://www.lryc.cn/news/505717.html

相关文章:

  • vue 响应式数据原理
  • android 计算CRC
  • Linux tinyproxy 使用教程
  • 局部规划器设计思路
  • 数字图像处理技术期末复习
  • UITableView显示数据,增加数据,删除数据及移动数据行
  • 金智塔科技喜获CCF中国数字金融大会 GraphRAG竞赛二等奖
  • 方案解读:数字化扩展中如何提升多云应用安全能力?
  • “年轻科技旗舰”爱玛A7 Plus正式发布,全国售价4999元
  • oracle开窗函数笔记、over()笔记
  • 【HarmonyOS】HarmonyOS 和 Flutter混合开发 (一)之鸿蒙Flutter环境安装
  • 海外招聘丨卢森堡大学—人工智能和机器学习中的 PI 用于图像分析
  • LeetCode hot100-85
  • linux 内核数据包处理中的一些坑和建议
  • C++ 的衰退复制(decay-copy)
  • vue-cli 5接入模块联邦 module federation
  • 【Rust自学】3.6. 控制流:循环
  • 【第八节】git与github
  • win如何访问Linux数据库(本地)
  • Windows设置所有软件默认以管理员身份运行
  • 前端 计算发布时间(如“1小时前”、“3天前”等)
  • shardingjdbc 4.0.0 seata分布式事务Failed to fetch schema问题
  • 罗德与施瓦茨NRT2功率反射仪,NRT2通过式功率计
  • QLineEdit限制输入固定字节数(UTF-8编码)
  • 基于ubuntu的mysql 8.0安装教程
  • K8s ConfigMap的基础功能介绍
  • Linux——Shell
  • armsom产品编译烧录Linux固件
  • VSCode:Markdown插件安装使用 -- 最简洁的VSCode中Markdown插件安装使用
  • AI 行业发展趋势:科技创新引领未来变革