黑马十次方项目day01-13之base模块的增删改查

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33229669/article/details/85932900

对标签的增删改查的实现

分为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> {
}

猜你喜欢

转载自blog.csdn.net/qq_33229669/article/details/85932900