gpt4 book ai didi

spring - 数据未插入 Spring boot + Spring data JPA 中?

转载 作者:行者123 更新时间:2023-12-02 02:16:41 26 4
gpt4 key购买 nike

我制作了一个spring boot应用程序,在应用程序中我是spring-data-jpa。问题出在向数据库插入数据时,显示数据已插入,但数据库中没有数据。以下是我的代码。请让我知道我在哪里做错了。提前致谢。

主类

    package avs.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan(basePackages = {"avs.pojo"})
@EnableJpaRepositories(basePackages = {"avs.repository"})
public class AVS {
public static void main(String[] args) {
SpringApplication.run(AVS.class, args);
}
}

Controller 类

package avs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import avs.pojo.User;
import avs.repository.UserReposiroty;

@Controller
public class UserController {

@Autowired
private UserReposiroty userRepository;

@RequestMapping(value = "/saveuser", method = RequestMethod.POST)
@ResponseBody
@Bean
public String saveProduct(/*@RequestBody User user*/) {
User user = new User();
user.setUserId("ani");
user.setPassword("ani123");
user.setApplicatonId(123l);
userRepository.save(user);

return user.getUserId().toString();
}
}

Pojo类

package avs.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.stereotype.Component;

@Entity
@Component
@Table(name="user")
public class User extends BasePOJO {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id;

@Column(name="application_id")
private Long applicatonId;

@Column(name="user_id")
private String userId;

@Column(name="password")
private String password;

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getApplicatonId() {
return applicatonId;
}

public void setApplicatonId(Long applicatonId) {
this.applicatonId = applicatonId;
}

@Override
public String toString(){
return String.format(
"Customer[id=%d, userId='%s', password='%s']",
id, userId, password);
}

}

存储库类

package avs.repository;

import org.springframework.data.repository.CrudRepository;

import avs.pojo.User;

public interface UserReposiroty extends CrudRepository<User, Long> {

}

应用程序属性

#port to run tomcat
server.port=8021
#database configuration
spring.datasource.url=jdbc:mysql://localhost/avs_db
spring.datasource.username=avsadmin
spring.datasource.password=avsadmin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

构建gradle文件

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
baseName = 'avs-bookcab'
version = '0.1.0'
}

repositories {
mavenCentral()
}

dependencies {
//Spring boot and MVC
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '1.5.2.RELEASE'
compile("org.springframework.boot:spring-boot-devtools")
//Spring boot and hibernate
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.h2database:h2")
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'

}

最佳答案

如果在类路径中找到 H2 数据库,Spring Boot 将自动在内存中设置一个 H2 数据库供您使用。只需从你的 gradle 中删除 H2 依赖即可!

关于spring - 数据未插入 Spring boot + Spring data JPA 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43523933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com