jdbcTemplate为空并抛出空指针异常
嗨,也许有人能解释一下为什么这个jdbcDAO方法不起作用并抛出此异常?
控制器
@Controller
public class MainPageController {
private final VehicleDAO vehicleDAO;
private final UserDAO userDAO;
@Autowired
public MainPageController(VehicleDAO vehicleDAO, UserDAO userDAO) {
this.vehicleDAO = vehicleDAO;
this.userDAO = userDAO;
}
@GetMapping("/main")
public String index(UserDAO user, Model model) {
model.addAttribute("vehicles", vehicleDAO.index());
model.addAttribute("user", userDAO.currentUser());
return "main";
}
@GetMapping("/new-vehicle")
public String newCar(@ModelAttribute("vehicle") Vehicle vehicle) {
return "new-vehicle";
}
@PostMapping("/new-vehicle")
public String addCar(@ModelAttribute("vehicle") Vehicle vehicle,
VehicleDAO vehicleDAO) {
vehicleDAO.add(vehicle);
return "redirect:/main";
}
}
问题出在“添加”方法上。
@Component
public class VehicleDAO {
private final JdbcTemplate jdbcTemplate;
@Autowired
public VehicleDAO(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Vehicle> index() {
return jdbcTemplate.query("SELECT * FROM vehicles", new BeanPropertyRowMapper<>(Vehicle.class));
}
public void add(Vehicle vehicle) {
jdbcTemplate.update("INSERT INTO vehicles (car_brand, car_year, price_rent) VALUES(?, ?, ?)", vehicle.getCAR_BRAND(),
vehicle.getCAR_YEAR(), vehicle.getPRICE_RENT()); // here the problem
}
车辆物体
public class Vehicle {
private int CAR_ID;
private String CAR_BRAND;
private int PRICE_RENT;
private int CAR_YEAR;
public Vehicle() {
}
public String getCAR_BRAND() {
return CAR_BRAND;
}
public void setCAR_BRAND(String CAR_BRAND) {
this.CAR_BRAND = CAR_BRAND;
}
public int getPRICE_RENT() {
return PRICE_RENT;
}
public void setPRICE_RENT(int PRICE_RENT) {
this.PRICE_RENT = PRICE_RENT;
}
public int getCAR_YEAR() {
return CAR_YEAR;
}
public void setCAR_YEAR(int CAR_YEAR) {
this.CAR_YEAR = CAR_YEAR;
}
public int getCAR_ID() {
return CAR_ID;
}
public void setCAR_ID(int CAR_ID) {
this.CAR_ID = CAR_ID;
}
public Vehicle(String CAR_BRAND, int PRICE_RENT, int CAR_YEAR, int CAR_ID) {
this.CAR_BRAND = CAR_BRAND;
this.PRICE_RENT = PRICE_RENT;
this.CAR_YEAR = CAR_YEAR;
this.CAR_ID = CAR_ID;
}
}
POM. xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.messages</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>web</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
属性
spring.datasource.url=jdbc:postgresql://localhost:5432/CoolStoreDB
用户名:用户名
我已经尝试更改我的sql命令并调试了一切。该方法的值,但由于某种原因抛出了jdbcTemplayte的null。
在您的代码中,您使用了setter方法,它用于为对象设置额外的属性或使用特定逻辑自定义对象。
如果您不想要任何额外的自定义,请使用@Autowed
注释以declatrion启动属性。例如。
@Autowired
private JdbcTemplate jdbcTemplate;