通过反射获取sql查询的值--jdbcTemplate、RowMapper

2 篇文章 0 订阅
1 篇文章 0 订阅
private  static  final Map<String,Class> map=new HashMap<>();
    static {
       map.put("PERSON_TEST", Person.class);
       map.put("Students_test", Students.class);


    }


 public List execSelectSql(String tableName,String opration,String loguuid){  //tableName 为map集合的values
        if (!map.keySet().contains(tableName)){
            throw  new IllegalArgumentException("这个表"+tableName+"没有指定的实体类型");
        }

        String baseSql="";
        //查出添加的数据
        if (OprationType.ADD.equalsIgnoreCase(opration)){
            if ("PERSON_TEST".equalsIgnoreCase(tableName)){//日志表特殊处理
                baseSql="SELECT * from "+tableName+" WHERE (is_push=0 OR is_push is NULL) AND (opration_type ='add' OR opration_type is NULL)";
            }else {
                baseSql="SELECT * from "+tableName+" WHERE (is_push=0 OR is_push is NULL) AND (opration_type ='add' OR opration_type is NULL) and log_uuid="+"'"+loguuid+"'";

            }
        }
        //通过反射进行映射,查出实体类集合
        List query = jdbcTemplate.query(baseSql, new RowMapper<Object>() {
            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                Class aClass = map.get(tableName);
                Object obj = BeanUtils.instantiateClass(aClass);
                Field[] fields = aClass.getDeclaredFields();
                for (Field field:fields){
                    field.setAccessible(true);
                    if (field.isAnnotationPresent(Column.class)){
                        Column cloumn = field.getAnnotation(Column.class);
                        try {
                            Class<?> type = field.getType();
                            String simpleName = type.getSimpleName();
                            if (simpleName.equalsIgnoreCase("String")){
                                field.set(obj,rs.getString(cloumn.name()));
                            }
                            if (simpleName.equalsIgnoreCase("Integer")||simpleName.equalsIgnoreCase("int")){
                              field.set(obj,rs.getInt(cloumn.name()));
                            }
                            if (simpleName.equalsIgnoreCase("Boolean")){
                                field.set(obj,rs.getBoolean(cloumn.name()));
                            }
                            if (simpleName.equalsIgnoreCase("Long")){
                                field.set(obj,rs.getLong(cloumn.name()));
                            }
                            if (simpleName.equalsIgnoreCase("Double")){
                                field.set(obj,rs.getDouble(cloumn.name()));
                            }
                            if (simpleName.equalsIgnoreCase("Float")){
                                field.set(obj,rs.getFloat(cloumn.name()));
                            }
                            if (simpleName.equalsIgnoreCase("Date")){
                                field.set(obj,rs.getDate(cloumn.name()));
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return obj;
            }
        });


        return  query;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值