Spring Boot + Hibernate将空值插入mysql

时间:2018-03-08 19:28:49

标签: mysql spring hibernate jpa

我有这样的配置(使用Maven):

的pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>springbootPlural</groupId>
  <artifactId>das-boot</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>


  <name>das-boot</name>
  <url>http://maven.apache.org</url>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/>
  </parent>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <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>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.39</version>
    </dependency>
  </dependencies>
</project>

application.properties:

server.port=0 #(for random port)

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/mojabaza
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

User.class
package com.test.testowa.model;

import org.springframework.data.jpa.domain.AbstractPersistable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User extends AbstractPersistable<Long> {

    private String userId;
    private String userName;
    private String password;

    public User() {

    }


    public User(String userId, String userName, String password) {
        this.userId = userId;
        this.userName = userName;
        this.password = password;
    }

    public String getUserId() {
        return userId;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

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

UserController.class

package com.test.testowa.controller;

import com.test.testowa.Service.UserService;
import com.test.testowa.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }


    @RequestMapping("/delete/{id}")
    public String deleteUser(@PathVariable Long id)
    {
        return userService.deleteUser(id);
    }


    @RequestMapping("/add")
    public User addUser(User user)
    {
        return userService.addUser(user);
    }


    @RequestMapping("/list/{id}")
    public User findOne(@PathVariable Long id)
    {

        return userService.findOne(id);
    }

    @RequestMapping("/list")
    public List<User> userList()
    {
        return userService.userList();
    }



}

UserServiceImpl.class

package com.test.testowa.Service.impl;

import com.test.testowa.Service.UserService;
import com.test.testowa.model.User;
import com.test.testowa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

private UserRepository userRepository;

@Autowired
    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public List<User> userList() {
        return userRepository.findAll();
    }


    @Override
    public User findOne(Long id) {
        return userRepository.findOne(id);
    }

    @Override
    public User addUser(User user) {
        return userRepository.save(user);
    }

    @Override
    public String deleteUser(Long id) {
        userRepository.delete(id);
    return "{'message':'User deleted'}";
    }
}

UserRepository.class

package com.test.testowa.repository;

import com.test.testowa.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {


}

UserService.class

package com.test.testowa.Service;

import com.test.testowa.model.User;

import java.util.List;

public interface UserService {
    List<User> userList();
    User findOne(Long id);

    User addUser(User user);
    String deleteUser(Long id);
}

Hibernate创建表“user”完美无缺,但当我在Postman中插入一些值时,例如POST - &gt; localhost:43441 / shipwrecks / add,带有application / json标题和正文:

{
"id2":1,
"name":"name1",
"description":"desc1"
}

我得到了:Hibernate: insert into shipwreck (description, id2, name) values (?, ?, ?)在控制台中。当我在mysql中手动插入数据时一切顺利。另外GET - &gt; localhost:43563 / shipwrecks / list效果很好。我在这段代码中犯了错误吗?也许错误版的Spring Boot?

1 个答案:

答案 0 :(得分:1)

您需要在Controller中为add方法指定POST方法。我假设您的User对象为null,并且您在请求正文中放入的数据无法到达您的服务。