当我尝试以下代码时,我在NullPointerException
附近获得jdbcTemplate.queryForObject
。我正在尝试针对Netezza数据库的简单select语句。下面是我的代码设置方式的示例:
类AccessDOA.java:
package WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
public class AccessDAO {
@Autowired
public JdbcTemplate jdbcTemplate;
public Person getUserDetails() {
String query = "Select name, email, age from user LIMIT 1";
return jdbcTemplate.queryForObject(query, (resultSet, i) -> {
return new Person(resultSet.getString(1), resultSet.getString(2), resultSet.getInt(3));
});
}
}
Controller正如此调用它:
PersonController.java
package WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class PersonController {
@RequestMapping(value = "/person")
public
@ResponseBody
Person dailyStats(@RequestParam Integer id) {
System.out.println("got this far");
AccessDAO newConn = new AccessDAO();
Person newPerson = new Person();
newPerson = newConn.getUserDetails();
return newPerson;
}
}
我最初有代码在PersonController
内调用我的数据库,它完美地运行并将person对象返回给邮递员。但是我希望将我的数据库调用保留在他们自己的类中,所以我一直在将这个逻辑移到AccessDAO.java
。