java下划线(数据库字段)与驼峰命名互转

方式一:

public class Tool {
	private static Pattern linePattern = Pattern.compile("_(\\w)");
 
	/** 下划线转驼峰 */
	public static String lineToHump(String str) {
		str = str.toLowerCase();
		Matcher matcher = linePattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
 
	/** 驼峰转下划线(简单写法,效率低于{@link #humpToLine2(String)}) */
	public static String humpToLine(String str) {
		return str.replaceAll("[A-Z]", "_$0").toLowerCase();
	}
 
	private static Pattern humpPattern = Pattern.compile("[A-Z]");
 
	/** 驼峰转下划线,效率比上面高 */
	public static String humpToLine2(String str) {
		Matcher matcher = humpPattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
 
	public static void main(String[] args) {
		String lineToHump = lineToHump("f_parent_no_leader");
		System.out.println(lineToHump);// fParentNoLeader
		System.out.println(humpToLine(lineToHump));// f_parent_no_leader
		System.out.println(humpToLine2(lineToHump));// f_parent_no_leader
	}
}

不纠结""_"+matcher.group(0).toLowerCase()"的话,humpToLine2效率会比humpToLine高一些,看String#replaceAll方法源码即可。

 

方式二:

实体类:

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = -329066647199569031L;

    private String userName;

    private String orderNo;
}
import java.io.IOException;

import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;

public class JsonTest {

    @Test
    public void testToUnderlineJSONString(){
        User user = new User("张三", "1111111");
        try {
            String json = JsonUtils.toUnderlineJSONString(user);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testToSnakeObject(){
        String json = "{\"user_name\":\"张三\",\"order_no\":\"1111111\"}";
        try {
            User user = JsonUtils.toSnakeObject(json, User.class);
            System.out.println(JSONObject.toJSONString(user));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import java.io.IOException;

import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;

public class JsonTest {

    @Test
    public void testToUnderlineJSONString(){
        User user = new User("张三", "1111111");
        try {
            String json = JsonUtils.toUnderlineJSONString(user);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testToSnakeObject(){
        String json = "{\"user_name\":\"张三\",\"order_no\":\"1111111\"}";
        try {
            User user = JsonUtils.toSnakeObject(json, User.class);
            System.out.println(JSONObject.toJSONString(user));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值