[implements] - 一個接口的使用

4種貨物,如何使用一個接口實現CRUD:前端

package com.tansuo365.test1.service.goods; import com.tansuo365.test1.entity.Goods; import com.tansuo365.test1.mapper.goods.IGoodsCommonMapper; import java.util.List; /** * 貨品公用service接口 * {@link #setGoodsTypeMapper 設置貨品類型} * {@link #delete 刪除單條元組} * {@link #deleteBatchByPKs 批量刪除} * {@link #addBySelective 單條動態錄入} * {@link #insertBatchList 批量錄入} */
public interface IGoodsCommonService { public void setGoodsTypeMapper(IGoodsCommonMapper goodsTypeMapper); public IGoodsCommonMapper getGoodsTypeMapper(); //根據主鍵刪除
    public Integer delete(Long id); //根據主鍵批量刪除
    public Integer deleteBatchByPKs(Long[] ids); //錄入(動態)
    public Integer addBySelective(Goods goods); //批量錄入
    public Integer insertBatchList(List<Goods> list); //按需獲取
    public List<Goods> getBySelective(Goods goods); //選擇所有
    public List<Goods> getAll(); //動態更新
    public Integer updateBySelective(Goods goods); }

實現java

package com.tansuo365.test1.service.goods; import com.tansuo365.test1.entity.Goods; import com.tansuo365.test1.mapper.goods.IGoodsCommonMapper; import com.tansuo365.test1.util.PetroleumCokeGradeUtil; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; /** * 貨物crud公用service */ @Service public class GoodsCommonService implements IGoodsCommonService{ private IGoodsCommonMapper goodsCommonMapper; @Override public void setGoodsTypeMapper(IGoodsCommonMapper goodsTypeMapper) { this.goodsCommonMapper = goodsTypeMapper; } @Override public IGoodsCommonMapper getGoodsTypeMapper() { return goodsCommonMapper; } @Override public Integer delete(Long id) { return goodsCommonMapper.deleteByPrimaryKey(id); } @Override public Integer deleteBatchByPKs(Long[] ids) { return goodsCommonMapper.deleteBatchByPKArr(ids); } @Override public Integer addBySelective(Goods goods) { return goodsCommonMapper.insertSelective(goods); } @Override public Integer insertBatchList(List<Goods> list) { return goodsCommonMapper.insertBatch(list); } @Override public List<Goods> getBySelective(Goods goods) { return goodsCommonMapper.selectGoodsSelective(goods); } @Override public List<Goods> getAll() { return goodsCommonMapper.selectAll(); } @Override public Integer updateBySelective(Goods goods) { return goodsCommonMapper.updateByPrimaryKeySelective(goods); } }

Controller調用git

package com.tansuo365.test1.controller.goods; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.tansuo365.test1.bean.goods.PetroleumCoke; import com.tansuo365.test1.bean.log.LogEnum; import com.tansuo365.test1.entity.Goods; import com.tansuo365.test1.mapper.goods.PetroleumCokeMapper; import com.tansuo365.test1.service.goods.IGoodsCommonService; //import com.tansuo365.test1.service.goods.PetroleumCokeServiceImpl;
import com.tansuo365.test1.service.redis.RedisService; import com.tansuo365.test1.util.CodeJudgerUtils; import com.tansuo365.test1.util.PetroleumCokeGradeUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 石油焦Controller */ @RestController @RequestMapping("/petroleumCoke") public class PetroleumCokeController { private String instance = "石油焦"; @Autowired private IGoodsCommonService goodsCommonService; @Resource private PetroleumCokeMapper petroleumCokeMapper; @Autowired private RedisService redisService; @Autowired private RedisTemplate redisTemplate; @Autowired private CodeJudgerUtils codeJudgerUtils; /*動態獲取數據*/ @RequestMapping("/selectSelective") public Map<String, Object> selectSelective(PetroleumCoke petroleumCoke, Integer page, Integer rows) { goodsCommonService.setGoodsTypeMapper(petroleumCokeMapper); Map<String, Object> map = new HashMap<String, Object>(); PageHelper.startPage(page, rows); List<Goods> list = goodsCommonService.getBySelective(petroleumCoke); PageInfo<Goods> pageInfo = new PageInfo<Goods>(list); map.put("rows", pageInfo.getList()); map.put("total", pageInfo.getTotal()); int code = 0; codeJudgerUtils.whichCodeIsOK(list,code,LogEnum.SEARCH_ACTION.toString(), instance); return map; } /*動態插入數據*/
    //插入數據時根據sulfur字段斷定品級並更新品級字段grade(特有)
    @RequestMapping("/insertSelective") public Integer insertSelective(PetroleumCoke petroleumCoke) { goodsCommonService.setGoodsTypeMapper(petroleumCokeMapper); Goods goods = PetroleumCokeGradeUtil.setGradeBySulfur(petroleumCoke); int code = goodsCommonService.addBySelective(goods); codeJudgerUtils.whichCodeIsOK(null,code, LogEnum.ADD_ACTION.toString(), instance); return code; } /*動態更新數據*/ @RequestMapping("/updateByPrimaryKeySelective") public Integer updateByPrimaryKeySelective(PetroleumCoke petroleumCoke) { goodsCommonService.setGoodsTypeMapper(petroleumCokeMapper); int code = goodsCommonService.updateBySelective(petroleumCoke); codeJudgerUtils.whichCodeIsOK(null,code, LogEnum.UPDATE_ACTION.toString(), instance); return code; } /*刪除數據*/ @RequestMapping("/deleteByPrimaryKey") public Integer deleteByPrimaryKey(Long id) { goodsCommonService.setGoodsTypeMapper(petroleumCokeMapper); int code = goodsCommonService.delete(id); codeJudgerUtils.whichCodeIsOK(null,code,LogEnum.DELETE_ACTION.toString(),instance); return code; } /*批量刪除*/ @RequestMapping("/deleteBatchByPKs") public Integer deleteBatch(@RequestParam(value = "ids[]") Long[] ids) { goodsCommonService.setGoodsTypeMapper(petroleumCokeMapper); int code = goodsCommonService.deleteBatchByPKs(ids); codeJudgerUtils.whichCodeIsOK(null,code,LogEnum.DELETE_ACTION.toString(),instance); return code; } // @Cacheable(value = "petroleumCokes") 不能加入緩存
    /*選取全部石油焦信息*/ @RequestMapping("/selectAll") public List<Goods> selectAllPetroleumCoke() { goodsCommonService.setGoodsTypeMapper(petroleumCokeMapper); List<Goods> all = goodsCommonService.getAll(); int code = 0; codeJudgerUtils.whichCodeIsOK(all,code,LogEnum.SEARCH_ACTION.toString(),instance); return all; } //查詢全部石油焦,加入緩存機制 // @RequestMapping("selectAll") // public List selectAllPetroleumCoke(){ //// List<PetroleumCoke> petroleumCokes = petroleumCokeMapper.selectAllPetroleumCoke(); //// map.put("petroleumCokes",petroleumCokes); //// return map; //
//        //字符串的序列化器 redis // RedisSerializer redisSerializer = new StringRedisSerializer(); // redisTemplate.setKeySerializer(redisSerializer); // List petroleumCokes = redisService.lGet("petroleumCokes", 0, -1); // System.out.println("查詢緩存數據爲"+petroleumCokes); // if (0 == petroleumCokes.size()) { // synchronized(this){ // System.out.println("進入第一個if"); // petroleumCokes = redisService.lGet("petroleumCokes", 0, -1); // if(0 == petroleumCokes.size()){ // System.out.println("第二個if顯示了,表示緩存沒有查到petroleumCokes."); //                    //緩存爲空,查詢數據庫 // petroleumCokes = petroleumCokeMapper.selectAllPetroleumCoke(); //                    //把數據庫查詢出來的數據放入redis // redisService.lSet("petroleumCokes",petroleumCokes); // } // } //
// } // return petroleumCokes; //
// }
 }

實體繼承Goods接口github

package com.tansuo365.test1.bean.goods; //import cn.afterturn.easypoi.excel.annotation.Excel;

import com.fasterxml.jackson.annotation.JsonFormat; import com.tansuo365.test1.entity.Goods; import com.tansuo365.test1.excel.ExcelCell; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.annotation.Transient; import java.io.Serializable; import java.util.Date; /** * 貨品石油焦 * 原使用poi的導出(@Excel),改成直接使用js前端進行導出(@ExcelCell) * 指定的index若是不指定將按照數據庫順序給出,不影響導出 */ @Data @Builder @NoArgsConstructor @AllArgsConstructor public class PetroleumCoke implements Serializable,Goods { private static final long serialVersionUID = -6077958594667413658L; // @ExcelCell(index = 0)
    private Long id; // @Excel(name = "品級", orderNum = "0") // @ExcelCell(index = 1)
    private String grade; // @Excel(name = "省份", orderNum = "1")
    @ExcelCell(index = 0) private String province; // @Excel(name = "企業", orderNum = "2")
    @ExcelCell(index = 1) private String company; // @Excel(name = "簡稱", orderNum = "3")
    @ExcelCell(index = 2) private String s_company; // @Excel(name = "硫含量%", orderNum = "4")
    @ExcelCell(index = 3) private Double sulfur; // @Excel(name = "灰分%", orderNum = "5")
    @ExcelCell(index = 4) private Double ash; // @Excel(name = "揮發分%", orderNum = "6")
    @ExcelCell(index = 5) private Double volatile_matter; // @Excel(name = "扣水率%", orderNum = "7")
    @ExcelCell(index = 6) private Double wdr; // @Excel(name = "釩含量ppm", orderNum = "8")
    @ExcelCell(index = 7) private Double vanadium; // @Excel(name = "真密度g/cm³", orderNum = "9")
    @ExcelCell(index = 8) private Double density; // @Excel(name = "粉焦量%", orderNum = "10")
    @ExcelCell(index = 9) private Double coke_content; // @Excel(name = "類型", orderNum = "11")
    @ExcelCell(index = 10) private String coke_type; // @Excel(name = "今日報價", orderNum = "12")
    @ExcelCell(index = 11) private Double today_price; // @Excel(name = "備註", orderNum = "13")
    @ExcelCell(index = 12) private String remarks; // private Boolean expand_2; // private Boolean expand_3;
 @ExcelCell(index = 13) private String reporter; // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") // @Excel(name = "建立時間", exportFormat = "yyyy-MM-dd HH:mm:ss", orderNum = "14")
    @ExcelCell(index = 14) private Date create_time; // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") //時區+8
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") // @Excel(name = "更新時間", exportFormat = "yyyy-MM-dd HH:mm:ss", orderNum = "15") // @ExcelCell(index = 14)
    private Date update_time; // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 @javax.persistence.Transient private String b_time; //起始時間 搜索用到 // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 @javax.persistence.Transient private String e_time; //結束時間 搜索用到
 }
package com.tansuo365.test1.entity; public interface Goods { }

okweb

ps:mapper name  語句要統一對應redis

<!--動態查詢-->
    <select id="selectGoodsSelective" parameterType="com.tansuo365.test1.bean.goods.PetroleumCoke" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from petroleum_coke_tbl <where>
            <if test="id != null and id !='' "> and id = #{id} </if>
            <if test="province != null and province !='' "> and province = #{province} </if>
            <if test="company != null and company !='' "> and company = #{company} </if>
            <if test="s_company != null and s_company != '' "> and s_company = #{s_company} </if>
            <if test="sulfur != null and sulfur !='' "> and sulfur = #{sulfur} </if>
            <if test="ash != null and ash !='' "> and ash = #{ash} </if>
            <if test="volatile_matter != null and volatile_matter !='' "> and volatile_matter = #{volatile_matter} </if>
            <if test="wdr != null and wdr !='' "> and wdr = #{wdr} </if>
            <if test="vanadium != null and vanadium !='' "> and vanadium = #{vanadium} </if>
            <if test="coke_type != null and coke_type !='' "> and coke_type = #{coke_type} </if>
            <if test="today_price != null and today_price !='' "> and today_price = #{today_price} </if>
            <if test="remarks != null and remarks !='' "> and remarks = #{remarks} </if>
            <if test="reporter != null and reporter !='' "> and reporter = #{reporter} </if>
            <if test="grade !=null and grade != '' "> and grade = #{grade} </if>
            <if test="create_time != null and create_time !='' "> and create_time = #{create_time} </if>
            <if test="update_time != null and update_time !='' "> and update_time = #{update_time} </if>
            <if test="density != null and density != '' "> and density = #{density} </if>
            <if test="coke_content != null and coke_content != '' "> and coke_content = #{coke_content} </if>
            <if test="b_time != null and b_time != '' "> create_time &gt;= #{b_time} </if>
            <if test="e_time != null and e_time != '' "> and create_time &lt;= #{e_time} </if>
        </where> order by create_time DESC </select>
相關文章
相關標籤/搜索