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<T,ID>, PagingAndSortingRepository<T,ID>, QueryByExampleExecutor<T>, Repository<T,ID>

 

(1)finAll()方法:

 

二、增删改查代码

(一)创建springboot框架

1.项目结构

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<User, Integer> {

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

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值