数据没有插入Spring boot + Spring数据JPA中?

时间:2017-04-20 15:31:19

标签: spring spring-boot spring-data-jpa

我已经制作了一个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);
        }
    }

控制器类

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 Class

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'

}

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

compile("com.h2database:h2")更改为test("com.h2database:h2")并且它会解决问题(您很可能希望在将来的测试中使用H2)。

另外,还有一些建议:

  • 注释@Bean不需要public String saveProduct(/*@RequestBody User user*/)方法;
  • 注释@Component不需要public class User extends BasePOJO类;
  • 如果将类public class AVS移动到项目的基础包中 (package avs;)你可以删除两个注释: @EntityScan(basePackages = {"avs.pojo"}) @EnableJpaRepositories(basePackages = {"avs.repository"}) 当前实现的问题是,如果你将一个Service类添加到avs.service包中,它将不会被spring选中,你将被迫配置@ComponentScan,这是一种选择,但是@SpringBootApplication会自动执行此操作,如果它位于正确的位置。