[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);
}

实现

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调用

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接口

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 {

}

ok

ps:mapper name  语句要统一对应

 <!--动态查询-->
    <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>

原文地址:https://www.cnblogs.com/ukzq/p/10420577.html

时间: 2024-07-31 12:01:35

[implements] - 一个接口的使用的相关文章

一个接口能否继承另一个接口?一个抽象类能否实现一个接口?

都可以: 一.一个接口能否继承另一个接口? 接口继承接口  本质就是一个抽象类继承另一个抽象类(它们都没写方法的实例).当一个类继承了一个抽象类,它必须要重写父类 抽象类中的抽象方法,如果不去重写父类抽象方法的实例,那么这个类也是抽象类(这个抽象子类,直到重写这个抽象方法的实例为止, 才能摆脱抽象的命运).其实我们想想,一个类去实现一个接口也不就是一个实体子类把一个抽象的所以抽象方法实例化(重写了 抽象父类 中全部抽象方法),当然抽象类和接口还是有区别的.我上上篇就写到了抽象类和接口的区别,有兴

35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n); (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和; (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n

  35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n): (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和: (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n!): (4)编写测试类E,在测试类E的main方法中使用接口回调的形式来测试实现 接口的类. p

一个接口有多个实现类的调用方式

1.普通方式实现: // 定义一个接口 interface Person { void eat(String str); } // 第一个实现类 class FirstPerson implements Person { private String name; FirstPerson(String name) { this.name = name; } public void eat(String str) { System.out.println(name + "正在吃" + st

创建一个接口Shape,其中有抽象方法area,类Circle 、Rectangle实现area方法计算其面积并返回。又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值boolean型方法isStar;在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象;如是2,生成Rectangle对象;如是3,生成S

题目补充: 创建一个接口Shape,其中有抽象方法area,类Circle .Rectangle实现area方法计算其面积并返回. 又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值boolean型方法isStar: 在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象: 如是2,生成Rectangle对象:如是3,生成Star对象).然后将Vector中元素依次取出,判断其是否为Star类.如是返回其

spring的@primary和@qualifier注解解决一个接口多个实现的注入问题

@Primary注解 Spring中有提供一个@Primary注解,具体的作用是在一个接口有多个实现类的情况下,会默认选择其中一种实现,帮助Spring对象的正常注入. 比如说现在有一个接口UserService,它有两个实现类UserServiceImpl1和UserServiceImpl2,根据依赖倒置的原则,在Spring中注入的对象应该是接口,由接口去调用具体的实现. // 接口 public interface UserService { String getUserName(); }

一个接口多个同名实现调用解决

声明了一个接口 public interface UserService { MessageBean setCurrentUser(User user); User getCurrentUser(); } 在不同的两个包里实现了两次,单元测试的时候没问题,但是实际测试的时候返现两个实现的调用错了. 解决: 其中一个实现标注 @Service("userService") 调用时 @Autowired @Qualifier("userService") private

spring容器注入一个接口的两个实现类

spring容器中能拥有两个同种类型的bean吗?我有两个dao类同时实现一个接口,这两个接口注入时报了异常如下. Text代码   org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.xxx.xxx.xxx.integration.dao.IDAO] is defined: expected single matching bean but found 2: [

autofac 一个接口多个实现的顺序执行

接口: namespace AutofacTest.Interface { public interface IUserInfo { string GetUserINfo(int uid); int sort(); } } 接口的实现: public class CrmUserInfoHelper : IUserInfo { public string GetUserINfo(int uid) { return "crmuserinfo"; } public int sort() {

(原创)多系统间需要对接,我写了一个接口框架。实用性非常强,写出来大家交流。需要的可以直接搬过去用。(第1篇)

业务是这样:有个手机app,在ios或者android上开发的,他需要访问服务器数据.我在服务器上,做了一个接口,专门开放一些数据给app去用. 我写的接口优势:我写的这个接口,全部是面向对象结构,代码简单而非常好用,极易扩展.可读性很强,容错性高. 个人建议有需要的同学可以直接拿去用到项目中..我会一步步讲他的制作方法. 第1步,创建一个工程,在工程中,创建一个网站,以及一个业务操作类库.如下图: 上图是网站. 上图是类库 第2步,创建几个根类.cs文件.如上图,创建:Post.cs文件:用于