spring jdbcTemplate query

1. spring jdbcTemplate query需要实现mapRow方法

package com.cdv.apolloagent.jdbc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.cdv.apolloagent.jdbc.dao.TaskDao;
import com.cdv.apolloagent.model.Task;
@Repository
public class TaskDaoImpl implements TaskDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Task> findTaskList(int flag, int limit, int max){
        String sql = "select t.* from task t where t.flag = ? limit ?,?";
        List<Object> params = new ArrayList<Object>();
        params.add(flag);
        params.add(limit);
        params.add(max);
        
        Object[] para = params.toArray(new Object[params.size()]);
        List<Task> taskList = this.jdbcTemplate.query(sql.toString(),para,
                new RowMapper<Task>() {
                    public Task mapRow(ResultSet rs, int rowNum) throws SQLException {
                        Task task = new Task();
                        task.setId(rs.getLong("id"));
                        task.setWorkorderid(rs.getInt("workorderid"));
                        task.setFlag(rs.getInt("flag"));
                        return task;
                    }
                });
        return taskList;
    }
}

 

posted @ 2016-05-17 14:42  fangfan  阅读(2155)  评论(0编辑  收藏  举报