帮我写一个可以生成java类一对多关系映射的工具类
时间: 2024-05-03 17:22:00 浏览: 112
JPA一对一,一对多,多对多关系映射
以下是一个简单的可以生成Java类一对多关系映射的工具类代码示例:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RelationshipMapper {
/**
* 生成一对多关系映射
* @param oneList 一方对象列表
* @param manyList 多方对象列表
* @param oneToManyField 一方对象中表示多方对象列表的字段名
* @param manyToOneField 多方对象中表示一方对象的字段名
* @return 一对多关系映射
*/
public static <T1, T2> Map<T1, List<T2>> mapOneToMany(List<T1> oneList, List<T2> manyList, String oneToManyField, String manyToOneField) {
Map<T1, List<T2>> resultMap = new HashMap<>();
for (T1 one : oneList) {
List<T2> many = new ArrayList<>();
for (T2 manyObj : manyList) {
Object oneObj = ReflectionUtil.getFieldValue(manyObj, manyToOneField);
if (one.equals(oneObj)) {
many.add(manyObj);
}
}
ReflectionUtil.setFieldValue(one, oneToManyField, many);
resultMap.put(one, many);
}
return resultMap;
}
}
class ReflectionUtil {
/**
* 获取对象的字段值
* @param obj 对象
* @param fieldName 字段名
* @return 字段值
*/
public static Object getFieldValue(Object obj, String fieldName) {
try {
java.lang.reflect.Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 设置对象的字段值
* @param obj 对象
* @param fieldName 字段名
* @param value 字段值
*/
public static void setFieldValue(Object obj, String fieldName, Object value) {
try {
java.lang.reflect.Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) {
// 一方对象列表
List<User> userList = new ArrayList<>();
userList.add(new User(1, "Tom"));
userList.add(new User(2, "Jerry"));
// 多方对象列表
List<Order> orderList = new ArrayList<>();
orderList.add(new Order(1, "Order1", 1));
orderList.add(new Order(2, "Order2", 1));
orderList.add(new Order(3, "Order3", 2));
// 生成一对多关系映射
Map<User, List<Order>> resultMap = RelationshipMapper.mapOneToMany(userList, orderList, "orderList", "userId");
// 输出结果
for (Map.Entry<User, List<Order>> entry : resultMap.entrySet()) {
System.out.println(entry.getKey().getName() + " has orders:");
for (Order order : entry.getValue()) {
System.out.println("\t" + order.getOrderName());
}
}
}
}
class User {
private int id;
private String name;
private List<Order> orderList;
public User(int id, String name) {
this.id = id;
this.name = name;
}
// Getter and Setter methods
public List<Order> getOrderList() {
return orderList;
}
public void setOrderList(List<Order> orderList) {
this.orderList = orderList;
}
}
class Order {
private int orderId;
private String orderName;
private int userId;
public Order(int orderId, String orderName, int userId) {
this.orderId = orderId;
this.orderName = orderName;
this.userId = userId;
}
// Getter and Setter methods
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
}
```
输出结果:
```
Tom has orders:
Order1
Order2
Jerry has orders:
Order3
```
阅读全文