1. 程式人生 > >黑馬十次方專案day01-13之base模組的增刪改查

黑馬十次方專案day01-13之base模組的增刪改查

文章目錄

對標籤的增刪改查的實現

分為control service dao的三層的開發

pojo

注意pojo的屬性,要和資料庫中的欄位名,對應上,
這樣在使用jpa的時候,就不用使用column註解了.會自動把屬性和表中的欄位對應上.


package com.tensquare.base.pojo;

import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; /** * 類名稱:Label * 類描述:標籤的實體類 * * @author: taohongchao * 建立時間:2019/1/6 15:21 * Version 1.0 */ @Entity @Table(name = "tb_label") public class Label implements Serializable { @Id private String id;//標籤的id
private String labelname;//標籤的名稱 private String state;//狀態 private Long count;//使用的數量 private Long fans;//關注數 private String recommend; //是否推薦 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getLabelname
() { return labelname; } public void setLabelname(String labelname) { this.labelname = labelname; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Long getCount() { return count; } public void setCount(Long count) { this.count = count; } public Long getFans() { return fans; } public void setFans(Long fans) { this.fans = fans; } public String getRecommend() { return recommend; } public void setRecommend(String recommend) { this.recommend = recommend; } }

Controller層

package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 類名稱:LabelController
 * 類描述:基礎模組.Label的controller層
 *
 * @author: taohongchao
 * 建立時間:2019/1/6 15:11
 * Version 1.0
 */
@RestController
@CrossOrigin  //允許不同平臺之間的跨域訪問
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * 方法名: findAll
     * 方法描述: 查詢所有的標籤
     * 修改日期: 2019/1/6 15:20
      * @param
     * @return entity.Result
     * @author taohongchao
     * @throws
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查詢成功!",labelService.findAll());
    }

    /**
     * 方法名: findById
     * 方法描述: 根據id查詢標籤
     * 修改日期: 2019/1/6 15:20
      * @param labelId
     * @return entity.Result
     * @author taohongchao
     * @throws
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public Result findById(@PathVariable("labelId") String labelId) {
        return new Result(true, StatusCode.OK, "查詢成功!",labelService.findById(labelId));
    }

    /**
     * 方法名: save
     * 方法描述: 儲存標籤
     * 修改日期: 2019/1/6 15:23
      * @param label
     * @return entity.Result
     * @author taohongchao
     * @throws
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Label label) {
        labelService.save(label);
        return new Result(true, StatusCode.OK, "儲存成功!");
    }

    /**
     * 方法名: update
     * 方法描述: 根據標籤的id,修改標籤
     * 修改日期: 2019/1/6 15:26
      * @param labelId
     * @param label
     * @return entity.Result
     * @author taohongchao
     * @throws
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
    public Result update(@PathVariable("labelId") String labelId,@RequestBody Label label) {
        label.setId(labelId);
        labelService.update(label);
        return new Result(true, StatusCode.OK, "修改成功!");
    }

    /**
     * 方法名: deleteById
     * 方法描述: 根據標籤的id,刪除標籤
     * 修改日期: 2019/1/6 15:27
      * @param labelId
     * @return entity.Result
     * @author taohongchao
     * @throws
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable("labelId") String labelId) {
        labelService.deleteById(labelId);
        return new Result(true, StatusCode.OK, "刪除成功!");
    }


}


service層

package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.util.List;

/**
 * 類名稱:LabelService
 * 類描述:標籤的service層
 *
 * @author: taohongchao
 * 建立時間:2019/1/6 15:40
 * Version 1.0
 */
@Service
@Transactional
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    public List<Label> findAll() {
        return labelDao.findAll();
    }

    public void save(Label label) {
        label.setId(idWorker.nextId()+"");
        labelDao.save(label);
    }


    public Label findById(String id) {
        return labelDao.findById(id).get();
    }

    public void update(Label label) {
        //save方法有id就更新,沒id就儲存
        labelDao.save(label);
    }

    public void deleteById(String id) {
         labelDao.deleteById(id);
    }





}


dao層

package com.tensquare.base.dao;

import com.tensquare.base.pojo.Label;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * 類名稱:LabelDao
 * 類描述:標籤的dao層介面
 *
 * @author: taohongchao
 * 建立時間:2019/1/6 15:35
 * Version 1.0
 */
public interface LabelDao extends JpaRepository<Label,String>, JpaSpecificationExecutor<Label> {
}