/**
* 通过结果集返回一个集合对象(包括关联表)
* @param resultSet
* @param tClass
* @param <T>
* @return
*/
@SneakyThrows
public static <T> List<T> getListAndSonList(ResultSet resultSet, Class<T> tClass) {
List<T> list = new ArrayList<>();
while (resultSet.next()) {
getObjAndSon(list,tClass,resultSet);
}
return list;
}
上述是一个根据结果集返回集合的方法。
/**
* 通过一条结果集查询回来一条数据对象(包含关联表)
* @param list
* @param tClass
* @param resultSet
* @param <T>
* @return
*/
@SneakyThrows
public static <T> Object getObjAndSon(List<T> list, Class<T> tClass, ResultSet resultSet) {
String idName = tClass.getAnnotation(Table.class).id();
T t = tClass.newInstance();
// 判断是否是同一个元素
boolean a = false;
if (list != null && list.size() != 0) {
T t1 = list.get(list.size() - 1);
Field declaredField = tClass.getDeclaredField(idName);
declaredField.setAccessible(true);
if (resultSet.getString(idName).equals(declaredField.get(t1).toString())) {
t = t1;
a = true;
}
}
for (Field field : tClass.getDeclaredFields()) {
field.setAccessible(true);
// 是关联表
SonTable annotation1 = field.getAnnotation(SonTable.class);
if (annotation1 != null) {
Object o = field.get(t) == null ? new ArrayList() : field.get(t);
Class<?> aClass = Class.forName(annotation1.value());
// 如果是集合类型
if (field.getType().getTypeName().equals(List.class.getTypeName())) {
List o2 = (List)o;
getObjAndSon(o2,aClass, resultSet);
if (!a) {
field.set(t, o2);
}
}// 是对象类型
else {
field.set(t, getObjAndSon(null,aClass, resultSet));
}
continue;
}
if (!a) {
// 寻找主字段映射
String name = field.getName();
TableField annotation = field.getAnnotation(TableField.class);
if (annotation != null) {
if (!annotation.exist()) {
continue;
}
if (annotation.value() != null && annotation.value() != "") {
name = annotation.value();
}
}
NameValueUtils.nameValue(t, field.getName(), resultSet.getString(name));
}
}
if (!a && list != null) {
list.add(t);
}
return t;
}
上面是最关键的递归函数,用于处理比如a和b一对多,b和c一对多,查询a b c,即使再往下嵌套几层也没有问题。
@Documented
@Inherited
@Target({ ElementType.FIELD, ElementType.METHOD ,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SonTable {
String value() default "";
}
@Documented
@Inherited
@Target({ ElementType.FIELD, ElementType.METHOD ,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SonTableField {
String value() default "";
}
@Documented
@Inherited
@Target({ ElementType.FIELD, ElementType.METHOD ,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value() default "";
String id() default "";
}
@Documented
@Inherited
@Target({ ElementType.FIELD, ElementType.METHOD ,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TableField {
String value() default "";
boolean exist() default true;
}
上述是我自定义的一些注解依次是子表,子表列名,表名和表列名。
public class NameValueUtils {
/**
* 将指定属性的值给对象赋值
* @param o
* @param name
* @param value
*/
@SneakyThrows
public static void nameValue(Object o,String name,String value ){
if(value == null || value.equals("null") || value.equals("")){
return;
}
Class<?> aClass = o.getClass();
Field field = aClass.getDeclaredField(name);
field.setAccessible(true);
Class<?> type = field.getType();
Object o1 = null;
if (type == String.class) {
o1 = value;
} else if (type == int.class || type == Integer.class) {
o1 = Integer.parseInt(value);
} else if (type == boolean.class || type == Boolean.class) {
o1 = Boolean.parseBoolean(value);
} else if (type == long.class || type == Long.class) {
o1 = Long.parseLong(value);
} else if (type == double.class || type == Double.class) {
o1 = Double.parseDouble(value);
} else if(type == Date.class){
o1 = DateUtils.getDate(value);
}
field.set(o,o1);
}
}
上面这个是我用到的给对象赋值的工具类,因为字符串类型参数无法直接赋值给Integer这些类型的,需要转一下形。
@Data
@Table(value = "customer",id = "username")
public class Customer {
private String username;
private String password;
private String name;
@TableField(value = "id_number")
private String idNumber;
private Double money;
private Date date;
@SonTable(value = "com.wangyufan.demo.pojo.CustomerCollection")
@TableField(exist = false)
private List<CustomerCollection> collections;
}
@Data
@Table(id="id",value = "customer_collection")
public class CustomerCollection {
private Long id;
// 余额
private Double balance;
@SonTableField("collection_money")
private Double money;
@TableField(value = "create_time")
private Date createTime;
@TableField(value = "customer_id")
private String customerId;
@SonTable(value = "com.wangyufan.demo.pojo.Scc")
@TableField(exist = false)
private Scc sccs;
}
@Data
@Table(value = "scc",id = "sid")
public class Scc {
private Long sid;
private Long cid;
}
这是我用到的三个实体类,可以看出customer和customer—collection是一对多,customer—collection和scc是一对一关系。
@SneakyThrows
@Test
public void test(){
System.out.println(JdbcObjectUtils.selectBySqlAndSon("select username, c.`name`, `password` ,id_number,c.money as money,date,id ,cs.money as collection_money,balance, create_time,customer_id ,sid,cid from customer c, customer_collection cs, scc where c.username = cs.customer_id and cs.id = scc.cid", Customer.class));
}
这是用到的测试方法,最后返回结果如下图。
[Customer(username=111, password=111, name=李一叹, idNumber=0, money=35800.0, date=Fri Jul 14 09:45:37 CST 2023, collections=[CustomerCollection(id=1, balance=10000.0, money=35800.0, createTime=Fri Jul 14 13:53:51 CST 2023, customerId=111, sccs=Scc(sid=1, cid=1)), CustomerCollection(id=2, balance=10000.0, money=35800.0, createTime=null, customerId=111, sccs=Scc(sid=3, cid=2))])]
可以看到都查询出来了,即使是再嵌套几层也没有问题。
初学者第一次写通用工具类方法,有些粗糙,还请大佬们多多包含。