spring jdbcTemplate使用queryForList示例

时间:2022-10-13 15:57:49

查询代码:

LogVo 日志要显示的内容(Log的部分或者全部列)

Log是日志完整的实体

    public List<LogVO> findLogByDate(String startDate,String endDate) {
        JdbcTemplate jdbcTemplate=(JdbcTemplate) queryDslJdbcTemplate.getJdbcOperations();
        List<LogVO> logs_1=new ArrayList<LogVO>();
        if(jdbcTemplate!=null){
            String sql="select * from logs where create_time <= '"
                    +endDate+"' and create_time >= '"+startDate+"'";
            List<Map<String,Object>> logs=new ArrayList<Map<String,Object>>();
            logs=jdbcTemplate.queryForList(sql);
            logs_1=mapToObject(logs);
        }
        return logs_1;
    }

返回值映射:

private List<LogVO> mapToObject(List<Map<String,Object>> logs){
        List<LogVO> Logs=new ArrayList<LogVO>();
        Map<String,Object> map=null;
        String userTypeName="";
        Byte userType=0;
        if(logs.size()>0){
            for(int i=0;i<logs.size();i++){
                LogVO Log=new LogVO();
                map=logs.get(i);
                if(null!= map.get("user_id")){
                        Log.setUserId(map.get("user_id").toString());
                    }
                }
                
                if(null!=map.get("user_name")){
                    Log.setUserName(map.get("user_name").toString());
                }
                                
                Logs.add(Log);
            }
            
        }
            return Logs;
    }