1 背景
对于以下代码, 小组内存在分歧
@Override
public List<Student> getByParam(Student student) {
return this.selectList(
new EntityWrapper<Student>()
.eq("student_name", student.studentName)
.eq("student_age", student.studentAge);
}
2 定义工具类实现
/**
* 直接通过 fields[index]获取存在问题: 实体属性可能增减, 顺序可能打乱, 不可靠
*/
Field[] fields = clazz.getDeclaredFields();
2.1 注解, 实体类
package com.***.***;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
int index() default 0;
}
----------------------------------------------
package com.***.***;
public class Student {
@MyAnnotation(index = 1)
private String studentName;
@MyAnnotation(index = 2)
private Integer studentAge;
public Student() {
}
// getter and setter
}
2.2 注解效果测试, 输出
@Test
public void fun15() throws ClassNotFoundException {
Class<?> clazz = Class.forName("com.***.***.Student");
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.getAnnotation(MyAnnotation.class).index() == 1) {
String s = field.toString();
System.out.println(s); // private java.lang.String com.***.***.Student.studentName
System.out.println(s.substring(s.lastIndexOf(".") + 1)); // studentName
}
}
}
3 实体属性转数据库字段的下划线格式
@Test
public void fun16() {
String s = "studentName";
String string = this.getString(s);
System.out.println(string); // student_name
}
private String getString(String s) {
if (StringUtils.isBlank(s)) {
return "";
}
char[] chars = s.toCharArray();
StringBuilder sb = new StringBuilder();
char low, c;
for (int i = 0; i < chars.length; i++) {
c = chars[i];
if (Character.isUpperCase(c)) {
sb.append("_").append(Character.toLowerCase(c));
continue;
}
sb.append(c);
}
return sb.toString();
}
3.1 测试及输出
@Test
public void fun17() throws Exception {
String name = this.fieldToHump(Class.forName("com.***.***.Student"), 1);
System.out.println(getString(name)); // student_name
}
// 获取指定属性(index: 想要的属性对应的注解值)
private String fieldToHump(Class clazz, int index) throws Exception {
Field[] fields = clazz.getDeclaredFields();
String s;
for (Field field : fields) {
if (field.getAnnotation(MyAnnotation.class).index() == index) {
s = field.toString();
return s.substring(s.lastIndexOf(".") + 1);
}
}
throw new RuntimeException("Are u stupid");
}
3.2 替代方案
// 原先代码中有很多这种字符串
@Override
public List<Student> getByParam(Student student) {
return this.selectList(
new EntityWrapper<Student>()
.eq("student_name", student.studentName)
.eq("student_age", student.studentAge);
}
------------------------------------------------------------
// 可以写成
@Override
public List<Student> getByParam(Student student) {
return this.selectList(
new EntityWrapper<Student>()
.eq(getString(fieldToHump(student.getClass(), 1)), studentName)
.eq(getString(fieldToHump(student.getClass(), 2)), studentAge);
}
4 其实已经存在解决方案(lombok版本有要求)
package com.***.***;
import lombok.experimental.FieldNameConstants;
@FieldNameConstants
public class Student {
private String studentName;
private Integer studentAge;
public Student() {
}
// getter and setter
}
------------------------------------------------------------
@Test
public void test18() {
String studentName = Student.Fields.studentName;
String studentAge = Student.Fields.studentAge;
System.out.println(studentName); // studentName
System.out.println(studentAge); // studentAge
}
------------------------------------------------------------
<!--lombok支持 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>