string转@Data @AllArgsConstructor @NoArgsConstructor @TableName("t_user") public class User { @TableField("uid") private Integer uid; @TableField("username") private String username; @TableField("password") private String password; @TableField("salt") private String salt; @TableField("phone") private String phone; @TableField("email") private String email; @TableField("gender") private Integer gender; @TableField("avatar") private String avatar; @TableLogic @TableField("is_delete") private Integer is_delete; }
时间: 2024-04-01 20:32:53 浏览: 56
您可以使用Java反射机制或者其他类似的工具来实现将字符串转换为User类对象。具体的方法可以参考以下示例代码:
```java
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.lang.reflect.Field;
public class StringToUserConverter {
public static void main(String[] args) {
String userStr = "1,john,123456,salt,13512345678,john@example.com,1,avatar.png,0";
User user = convert(userStr, User.class);
System.out.println(user);
}
public static <T> T convert(String str, Class<T> clazz) {
String[] fields = str.split(",");
T obj = null;
try {
obj = clazz.newInstance();
Field[] declaredFields = clazz.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
Field field = declaredFields[i];
field.setAccessible(true);
if (field.isAnnotationPresent(TableField.class)) {
TableField tableField = field.getAnnotation(TableField.class);
String fieldName = tableField.value();
Object fieldValue = convertValue(fields[i], field.getType());
field.set(obj, fieldValue);
} else if (field.isAnnotationPresent(TableLogic.class)) {
Object fieldValue = convertValue(fields[i], field.getType());
field.set(obj, fieldValue);
}
}
if (clazz.isAnnotationPresent(TableName.class)) {
TableName tableName = clazz.getAnnotation(TableName.class);
Field tableField = clazz.getDeclaredField("tableName");
tableField.setAccessible(true);
tableField.set(obj, tableName.value());
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
private static Object convertValue(String value, Class<?> type) {
if (int.class.equals(type) || Integer.class.equals(type)) {
return Integer.parseInt(value);
} else if (String.class.equals(type)) {
return value;
} else {
// TODO: convert other types
return null;
}
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_user")
class User {
@TableField("uid")
private Integer uid;
@TableField("username")
private String username;
@TableField("password")
private String password;
@TableField("salt")
private String salt;
@TableField("phone")
private String phone;
@TableField("email")
private String email;
@TableField("gender")
private Integer gender;
@TableField("avatar")
private String avatar;
@TableLogic
@TableField("is_delete")
private Integer isDelete;
private String tableName;
}
```
这段代码中,我们首先定义了一个`StringToUserConverter`类,其中包含了一个静态方法`convert`,该方法可以将一个字符串转换为指定类型的对象。在`convert`方法中,我们首先通过反射获取目标类型的所有字段,然后逐个解析输入字符串的各个部分,并将其转换为对应的字段类型。在字段解析过程中,我们通过`@TableField`和`@TableLogic`注解来标识需要解析的字段,同时也支持自定义的注解。在解析完所有字段后,我们还可以通过`@TableName`注解来获取表名,并将其设置到对象的`tableName`字段中。最后,我们返回转换后的对象。在示例代码中,我们将输入字符串转换为了一个`User`对象,并将其输出到控制台上。
阅读全文