SpringBoot 增删改查

huangapple go评论86阅读模式
英文:

SpringBoot CRUD

问题

package com.javahelps.restservice;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.javahelps.restservice.entity.User;

import com.javahelps.restservice.repository.UserRepository;

import com.javahelps.restservice.repository.UserRepository2;

import com.javahelps.restservice.repository.UserRepository3;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    protected CommandLineRunner init(final UserRepository userRepository, UserRepository2 userRepository2, UserRepository3 userRepository3) {
        return null;
    }
}
package com.javahelps.restservice.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.javahelps.restservice.entity.User;

import com.javahelps.restservice.repository.UserRepository;
import com.javahelps.restservice.repository.UserRepository3;

import javassist.tools.web.BadHttpRequest;

@RestController
@RequestMapping(path = "/productnames")
public class UserController {

    @Autowired
    private UserRepository repository;
    private UserRepository3 repository3;

    @GetMapping
    public Iterable<User> findAll() {
        return repository.findAll();
    }

    @GetMapping(path = "/{barcode}")
    public User find(@PathVariable("barcode") String barcode) {
        return repository.findOne(barcode);
    }

    @PostMapping(consumes = "application/json")
    public User create(@RequestBody User user) {
        return repository.save(user);
    }

    @DeleteMapping(path = "/{barcode}")
    public void delete(@PathVariable("barcode") String barcode) {
        repository.delete(barcode);
    }

    @DeleteMapping(path = "/1/{id}")
    public void delete(@PathVariable("id") Integer id) {
        repository.delete(id);
    }

    @PutMapping(path = "/{barcode}")
    public User update(@PathVariable("barcode") String barcode, @RequestBody User user) throws BadHttpRequest {
        if (repository.exists(barcode)) {
            user.setBarcode(barcode);
            return repository.save(user);
        } else {
            throw new BadHttpRequest();
        }
    }
}
package com.javahelps.restservice.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;

import com.javahelps.restservice.entity.User;

@RestResource(exported = false)
@Repository
public interface UserRepository extends JpaRepository<User, String> {

}
package com.javahelps.restservice.entity;

import java.sql.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "ProductNames")
public class User {

    private int id;

    @Id
    private String barcode;
    private String name;
    private String category;
    private int qty;
    private Date dater;
    private Date datel;

    public int getId() {
        return id;
    }

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

    public String getBarcode() {
        return barcode;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }

    public Date getDater() {
        return dater;
    }

    public void setDater(Date dater) {
        this.dater = dater;
    }

    public Date getDatel() {
        return datel;
    }

    public void setDatel(Date datel) {
        this.datel = datel;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", barcode='" + barcode + '\'' +
                ", name='" + name + '\'' +
                ", category='" + category + '\'' +
                ", qty=" + qty +
                ", dater=" + dater +
                ", datel=" + datel +
                '}';
    }
}
英文:

I stuck with my Springboot Crud project and i need your helps.Problem is i want to use GET with my barcode string variable , and to DELETE and PUT using my id int variable but somehow i could not managed to DELETE and PUT with id variable and i stuck with this all the day. i will post my code and i will apriciate every help

> Application.java

package com.javahelps.restservice;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.javahelps.restservice.entity.User;
import com.javahelps.restservice.repository.UserRepository;
import com.javahelps.restservice.repository.UserRepository2;
import com.javahelps.restservice.repository.UserRepository3;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
protected CommandLineRunner init(final UserRepository userRepository , UserRepository2 userRepository2,UserRepository3 userRepository3) {
return null;
};
}

> UserController.java

package com.javahelps.restservice.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties.Session;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.javahelps.restservice.entity.User;
import com.javahelps.restservice.repository.UserRepository;
import com.javahelps.restservice.repository.UserRepository3;
import javassist.tools.web.BadHttpRequest;
@RestController
@RequestMapping(path = &quot;/productnames&quot;)
public class UserController {
@Autowired
private UserRepository repository;
private UserRepository3 repository3;
@GetMapping
public Iterable&lt;User&gt; findAll() {
return repository.findAll();
}
@GetMapping(path = &quot;/{barcode}&quot;)
public User find(@PathVariable(&quot;barcode&quot;) String barcode) {
return repository.findOne(barcode);
}
@PostMapping(consumes = &quot;application/json&quot;)
public User create(@RequestBody User user) {
return repository.save(user);
}
@DeleteMapping(path = &quot;/{barcode}&quot;)
public void delete(@PathVariable(&quot;barcode&quot;) String barcode) {
repository.delete(barcode);
}
@DeleteMapping(path = &quot;1/{id}&quot;)
public void delete(@PathVariable(&quot;id&quot;) Integer id) {
repository.delete(id);
}
@PutMapping(path = &quot;/{barcode}&quot;)
public User update(@PathVariable(&quot;barcode&quot;) String barcode, @RequestBody User user) throws BadHttpRequest {
if (repository.exists(barcode)) {
user.setBarcode(barcode);
return repository.save(user);
} else {
throw new BadHttpRequest();
}
}
}

> UserRepository.java

package com.javahelps.restservice.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;
import com.javahelps.restservice.entity.User;
@RestResource(exported = false)
@Repository
public interface UserRepository extends JpaRepository&lt;User,String&gt; {
}

> User.java

package com.javahelps.restservice.entity;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name=&quot;ProductNames&quot;)
public class User {
private int id;
@Id
private String barcode;
private String name;
private String category;
private int qty;
private Date dater;
private Date datel;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public Date getDater() {
return dater;
}
public void setDater(Date dater) {
this.dater = dater;
}
public Date getDatel() {
return datel;
}
public void setDatel(Date datel) {
this.datel = datel;
}
@Override
public String toString() {
return &quot;User{&quot; + &quot;=&#39;&quot; +&quot;, id=&#39;&quot;+ id + &#39;\&#39;&#39; +&quot;, name=&#39;&quot;+ barcode + &#39;\&#39;&#39; + &quot;, name=&#39;&quot; + name + &#39;\&#39;&#39; + &quot;, category=&#39;&quot; + category + &#39;\&#39;&#39;
+ &quot;, qty=&#39;&quot; + qty + &#39;\&#39;&#39; + &quot;, dater=&#39;&quot; + dater + &#39;\&#39;&#39;+&quot;, datel=&#39;&quot; + datel + &#39;\&#39;&#39;	+&#39;}&#39;;
}
}

答案1

得分: 1

根据id进行删除操作,由于您的主键不是int id,您需要在扩展JpaRepository的接口中编写以下自定义代码。

在您的REST控制器中,您需要这样调用它:repository.deleteById(id);

@Repository
public interface UserRepository extends JpaRepository<User, String> {
    @Modifying
    @Transactional
    @Query(value="delete from User u where u.id = ?1")
    void deleteById(int id);
}

类似地,您可能还需要为更新语句编写代码(用于PUT情况)。
希望对您有所帮助。

英文:

For delete based on id, since your primary key is not int id you have to write the below custom code in your interface extending JpaRepository.

And in your rest controller you have to invoke it like repository.deleteById(id);

@Repository
public interface UserRepository extends JpaRepository&lt;User,String&gt; {
@Modifying
@Transactional
@Query(value=&quot;delete from User u where u.id= ?1&quot;)
void deleteById(int id);
}

Similarly you may have to write code for your update statement as well (for PUT case).
Hope this helps.

huangapple
  • 本文由 发表于 2020年4月11日 01:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61145597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定