如何将jdbcTemplate.queryForList语句作为List <map <string,object =“”>&gt;传递另一种方法

时间:2018-05-20 15:34:28

标签: java mysql spring list dictionary

我试图将方法viewEmployees中的jdbcTemplate.queryForList语句从我的EmployeeDAO类发送到另一个名为EmployeeServiceImpl的方法。

我使用的是Spring Boot ..

def check_plan_expiration_date():

    now = datetime.now(timezone.utc)
    if user_trainingplan.expiration_date <= now:
        user_trainingplan.expiration_date = datetime.now(timezone.utc)+timedelta(days=7)
        user_trainingplan.save()
    else:
        return user_trainingplan.expiration_date

这是我希望将返回值发送到的另一种方法:

    public class EmployeeDAO{                                         

        public List<Map<String,Object>> viewEmployees() {

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        //This is where i make the sql-statement
        List<Map<String, Object>> rows = jdbcTemplate.queryForList("SELECT * FROM employee");

        //here I want to return an List<Map<String,Object>> of rows
        return rows;
    }
}

我一直收到错误:

  public List viewEmployees(){List<Employee> employees = new ArrayList<>();
        EmployeeDAO employeeDAO = new EmployeeDAO();
        List<Map<String, Object>> rows = employeeDAO.viewEmployees();
        //For loop of rows (Employees)
        for (Map row : rows) {
            Employee employee = new Employee();
            employee.setId((int) (row.get("id")));
            employee.setName((String) (row.get("name")));
            employee.setPosition((String) (row.get("position")));
            employee.setProvision_goal((int) (row.get("provision_goal")));
            employee.setProvision_accumulated((int) (row.get("provision_accumulated")));
            employees.add(employee);
        }
        return employees;
    }

如何退回行?

我的数据源已定义:

java.lang.IllegalArgumentException: Property 'dataSource' is required

    at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:160) ~[spring-jdbc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:166) ~[spring-jdbc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at com.example.demo.dao.EmployeeDAO.viewEmployees(EmployeeDAO.java:37) ~[classes/:na]
    at com.example.demo.services.EmployeeServiceImpl.viewEmployees(EmployeeServiceImpl.java:61) ~[classes/:na]
    at com.example.demo.controller.HomeController.home(HomeController.java:37) ~[classes/:na]

当我结合使用这两种方法时,我对代码没有任何问题。问题似乎是当我尝试在EmployeeDAO中返回行时。

1 个答案:

答案 0 :(得分:0)

这就是我定义dataSource的方法

Context initialContext = new InitialContext();
Context envContext = (Context) initialContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/db1");
JdbcTemplate temp = new JdbcTemplate();
temp.setDataSource(ds);

<强> context.xml中

  <Resource auth="Container" 
   driverClassName="com.mysql.jdbc.Driver" 
   maxActive="100" 
   maxIdle="30" 
   maxWait="10000" 
   minEvictableIdleTimeMillis="30000" 
   name="jdbc/revplatformdb" 
   timeBetweenEvictionRunsMillis="20000"
   type="javax.sql.DataSource"
   password="xxx"
   url="jdbc:mysql://localhost:3306/db1"
   username="database1"/>