2017-04-20 71 views
2

我做了一個spring啓動應用程序,在應用程序中我是spring-data-jpa。在數據庫中插入數據時存在問題,它顯示數據已插入,但數據庫中沒有數據。以下是我的代碼。請讓我知道我在做什麼錯誤。提前致謝。數據沒有被插入到Spring引導+ Spring數據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類

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); 
    } 

} 

repository類

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' 

} 
+1

取下gradle這個 –

回答

2

如果H2數據庫在classpath中發現,春季啓動會自動爲您的使用內存數據庫H2。只需從你的gradle中刪除H2依賴關係!

+0

你是對H2的依賴,但現在它給我: - 無法確定數據庫類型無 –

+0

通常嵌入式數據庫驅動程序類,春天開機嘗試配置數據源,並且在沒有拋出此錯誤信息配置提供。但正如我所看到的,你已經把這個放在你的application.properties上。你能否通過堆棧跟蹤? – Aboullaite

2

compile("com.h2database:h2")更改爲test("com.h2database:h2")它會做的伎倆(你最有可能想爲你未來的測試有H2)。

而且,對於你一些建議:不需要上述public String saveProduct(/*@RequestBody User user*/)方法

  • 註釋@Bean;
  • 註釋@Component以上public class User extends BasePOJO類是不需要的;
  • 如果移動public class AVS類項目 (package avs;)的基礎包,你可以擺脫這兩個註解: @EntityScan(basePackages = {"avs.pojo"}) @EnableJpaRepositories(basePackages = {"avs.repository"}) 與目前執行的問題是,如果你添加一個服務類avs.service包,它不會被春天拿起,你將被迫配置@ComponentScan,這是一種選擇,但@SpringBootApplication自動,如果它在一個正確的地方。
相關問題