spring data jpa实现增删改查

一、引子:

1.spring data jpa 官网地址:https://projects.spring.io/spring-data-jpa/

2.JpaRepository接口地址:https://docs.spring.io/spring-data/jpa/docs/2.1.0.RELEASE/api/

父接口:CrudRepository, PagingAndSortingRepository, QueryByExampleExecutor, Repository

 

(1)finAll()方法:

spring data jpa实现增删改查_第1张图片

 

二、增删改查代码

(一)创建springboot框架

spring data jpa实现增删改查_第2张图片

1.项目结构

spring data jpa实现增删改查_第3张图片

2.apllication.properties配置mysql连接信息

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456

3.创建user实体

package com.igsway.application;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String name;

    private String email;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


}

4.创建dao层接口

package com.igsway.application;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public interface UserRepository extends JpaRepository {

    @Modifying
   @Query(value = "update user set name = :name where id = :id",nativeQuery = true)
    void updateNameById(@Param("id") Integer id, @Param("name") String name);

}

5.创建controller层

package com.igsway.application;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(path="/demo")
public class MainController {
    @Autowired
    private UserRepository userRepository;
    //增加用户信息
    @GetMapping(path = "/add")
    public @ResponseBody
    String addNewUser(@RequestParam("name") String name, @RequestParam("email") String email) {

        User n = new User();
        n.setName(name);
        n.setEmail(email);
        userRepository.save(n);
        return "Saved";
    }
    

   //迭代查询所有用户信息
    @GetMapping(path = "/all")
    public @ResponseBody
    Iterable getAllUsers() {
        return userRepository.findAll(Sort.by("id"));

    }


    //按照id查询用户信息
    @GetMapping(path = "/byId")
    public @ResponseBody
    //请求参数id映射绑定函数参数id,函数参数与数据库参数已在实体通过注解映射绑定
    User getUserById(@RequestParam("id") Integer id) {
        // This returns a JSON or XML with the users
        return userRepository.findById(id).get();

    }


    //按照id删除用户
    @GetMapping(path = "/deleteById")
    public @ResponseBody
    void delUser(@RequestParam("id") Integer id) {
          userRepository.deleteById(id);
    }

    //通过id对name字段进行更新
    @GetMapping(path = "/update")
    public @ResponseBody
    void update(@RequestParam("id") Integer id,@RequestParam("name") String name) {
        userRepository.updateNameById(id,name);
    }
}


6.主函数

package com.igsway.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

 

 

 

你可能感兴趣的:(java)