我希望我的项目创建一个表,它保存在MySQL中,问题是,当保存所有的值都为空
我被迫写别的东西,但我不知道要添加什么,这个项目可以通过mvc来完成,通过写入保存后更新删除控制器,我这样做后就有可能解决现在出现的问题
我使用docker docker-compose.yaml
version: '3.8'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8081:8080
application.yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/cimpoiesu
username: root
password: example
jpa:
showSql: true
hibernate:
ddl-auto: create
pom.xml
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
主类
package com.example.demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
CommandLineRunner runner(StudentRepository repository) {
return args -> {
Address address = new Address(
"Z",
"X",
"C"
);
Student student = new Student(
"Danila",
"Cimpoiesu",
"danila.cimpoiesu@mail.ru",
Gender.MALE,
address,
List.of("Computer science"),
BigDecimal.TEN,
LocalDateTime.now()
);
repository.save(student);
};
}
}
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends
JpaRepository<Student, Long> {
}
package com.example.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import lombok.Data;
import jakarta.persistence.Id;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@Data
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstname;
private String lastname;
private String email;
private Gender gender;
private Address address;
private List<String> favouriteSubject;
private BigDecimal totalSpentInBooks;
private LocalDateTime created;
public Student() {
}
public Student(String firstname, String lastname, String email, Gender gender, Address address, List<String> favouriteSubject, BigDecimal totalSpentInBooks, LocalDateTime created) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.gender = gender;
this.address = address;
this.favouriteSubject = favouriteSubject;
this.totalSpentInBooks = totalSpentInBooks;
this.created = created;
}
}
package com.example.demo;
import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Embeddable
public class Address {
private String country;
private String city;
private String postCode;
}
package com.example.demo;
public enum Gender {
MALE, FEMALE
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.5)
2023-05-30T15:54:04.647+03:00 INFO 9896 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 19 with PID 9896 (C:\Users\jesus\Desktop\plack\target\classes started by jesus in C:\Users\jesus\Desktop\plack)
2023-05-30T15:54:04.656+03:00 INFO 9896 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2023-05-30T15:54:06.140+03:00 INFO 9896 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-05-30T15:54:06.324+03:00 INFO 9896 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 158 ms. Found 1 JPA repository interfaces.
2023-05-30T15:54:07.720+03:00 INFO 9896 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-05-30T15:54:07.737+03:00 INFO 9896 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-05-30T15:54:07.738+03:00 INFO 9896 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.7]
2023-05-30T15:54:07.918+03:00 INFO 9896 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-05-30T15:54:07.921+03:00 INFO 9896 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3113 ms
2023-05-30T15:54:08.298+03:00 INFO 9896 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-05-30T15:54:08.423+03:00 INFO 9896 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.1.7.Final
2023-05-30T15:54:08.998+03:00 INFO 9896 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-05-30T15:54:09.740+03:00 INFO 9896 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@55adcf9e
2023-05-30T15:54:09.745+03:00 INFO 9896 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-05-30T15:54:09.893+03:00 INFO 9896 --- [ main] SQL dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: drop table if exists student
Hibernate: create table student (id bigint not null auto_increment, city varchar(255), country varchar(255), post_code varchar(255), created datetime(6), email varchar(255), favourite_subject varbinary(255), firstname varchar(255), gender smallint, lastname varchar(255), total_spent_in_books decimal(38,2), primary key (id)) engine=InnoDB
2023-05-30T15:54:11.908+03:00 INFO 9896 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-05-30T15:54:11.923+03:00 INFO 9896 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-05-30T15:54:12.463+03:00 WARN 9896 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-05-30T15:54:13.171+03:00 INFO 9896 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-05-30T15:54:13.187+03:00 INFO 9896 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 9.612 seconds (process running for 10.486)
Hibernate: insert into student (city, country, post_code, created, email, favourite_subject, firstname, gender, lastname, total_spent_in_books) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
我以前做过一个使用MongoDB的项目,它工作了,我想为MySQL重做它
1条答案
按热度按时间nhaq1z211#
表中的那些NULL表示列是可空的。比较id bigintnot null
要查看添加到数据库中的内容,请在代码
repository.save(student);
之后添加以下内容要防止DB接受列的空值,请在字段中添加以下注解: