即使我在控制台中执行了所有正确配置的设置和休眠查询,但是如果我们检查数据库值的数量和类别是否作为NULL值插入,即使我使用了Crud存储库,Springboot也会插入NULL值
Ticket.java
package com.vinay.api.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Entity
@Table(name = "Ticket")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Ticket {
@Id
@GeneratedValue
private int id;
private double amount;
private String category;
}
TicketDao.java
package com.vinay.api.dao;
import org.springframework.data.repository.CrudRepository;
import com.vinay.api.model.Ticket;
public interface TicketDao extends CrudRepository<Ticket, Integer>{
}
TicketController.java
package com.vinay.api.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vinay.api.dao.TicketDao;
import com.vinay.api.model.Ticket;
@RestController
@RequestMapping("/tickets")
public class TicketController {
@Autowired
private TicketDao ticketDao;
@PostMapping("/bookTickets")
public String bookTicket(@RequestBody List<Ticket> tickets)
{
ticketDao.saveAll(tickets);
return "tickets booked: "+tickets.size();
}
@GetMapping("/getTickets")
public List<Ticket> getTickets()
{
return (List<Ticket>) ticketDao.findAll();
}
}
SpringMysqlApplication.java
package com.vinay.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMysqlApplication.class, args);
}
}
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/ticket
spring.datasource.username= root
spring.datasource.password=
spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
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>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-mysql</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-mysql</name>
<description>Springboot mysql</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:0)
尝试指定生成类型策略。
@GeneratedValue(strategy=GenerationType.AUTO)
还使用包装器类而不是原始数据类型。将int更改为Integer。
private Integer id
答案 1 :(得分:0)
我能够解决此问题,因为我没有在IDE中配置lambok,所以我使用了手动吸气器,而setter则删除了@getter和@Setter注释,帮助我解决了这个问题