JPA写简单的增删改查

1.类

package com.zz.entity;

import lombok.Data;

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

/**
 * @Description: java类作用描述
 * @Author: ll
 * @CreateDate: 2019/6/2$21:37$
 */
@Entity
@Data
public class Commodity {
    @Id
    @GeneratedValue
    Long id;
    String commNumber;//编号
    String commName;//名字
    String commPrice;//价格
    String commType;//类型

}

2.repository

package com.zz.repository;

import com.zz.entity.Commodity;
import org.springframework.data.domain.Page;
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.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import javax.validation.constraints.Past;
import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: ll
 * @CreateDate: 2019/6/7$14:28$
 */
@Repository
public interface CommodityRepository extends JpaRepository {

//根据id删除
@Modifying
@Transactional
    @Query(value = "delete from commodity where id=?1 ", nativeQuery = true)
    public void deleteCommodityById(long id);

//修改
    @Modifying
    @Transactional
    @Query(value = "update commodity set comm_number=?1,comm_name=?2,comm_price=?3,comm_type=?4 where id=?5",nativeQuery = true)
    public void updateCommodityById(String commNumber,String commName,String commPrice,String commType,long id);




}

3.service

package com.zz.Service;

import com.zz.entity.Commodity;
import com.zz.repository.CommodityRepository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Description: java类作用描述
 * @Author: ll
 * @CreateDate: 2019/6/7$14:39$
 */

@Service
@Transactional
public class CommodityService {
    @Resource
    CommodityRepository commodityRepository;

//增
   public Commodity saveCommodity(Commodity commodity){
       Commodity commodity1=commodityRepository.save(commodity);
       return commodity;
   }


}

4.controller

package com.zz.controller;


import com.zz.Service.CommodityService;
import com.zz.entity.Commodity;

import com.zz.repository.CommodityRepository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: java类作用描述
 * @Author: ll
 * @CreateDate: 2019/6/7$15:03$
 */
@RestController
public class CommodityController {
    @Resource
    CommodityService commodityService;
    @Resource
    CommodityRepository commodityRepository;


    @RequestMapping("/save")
    public String saveComm(Commodity commodity){
        commodityService.saveCommodity(commodity);
        return "添加";
    }
    @RequestMapping("/delete")
    public String deleteComm( @RequestParam("id") Long id){
//        Commodity commodity=new Commodity();
        commodityRepository.deleteCommodityById(id);
        return "删除";

    }
@RequestMapping("/update")
public String updateComm(@RequestParam("id") Long id,Commodity c){
       commodityRepository.updateCommodityById(c.getCommNumber(),c.getCommName(),c.getCommPrice(),c.getCommType(),id);
       return "修改";
}

@RequestMapping("/select")
    public  Commodity selectComm(@RequestParam("id") Long id){
        Commodity commodity=new Commodity();
        commodity=commodityRepository.findOne(id);
        return commodity;
}

}

提示:用postman可以测试

你可能感兴趣的:(Java)