service层 service serviceImpl @Service @Autowired 用法

service

一般也会有一个BaseService,用来规范方法。service定义方法。serviceImpl实现方法。

样例

package com.lwk.service;
import com.lwk.entity.Weather;
import java.util.Date;
import java.util.List;

public interface WeatherService {
    Weather queryById(Integer keyId);    //通过id查询,扩展通过属性查询
    List queryByDate();   //日期查询
    List queryList(String city, Integer weatherDegree, String weather, Integer highDegree, Integer lowDegree, Date date);
    Integer delete(Integer keyId);       //通过id删除,扩展删除队列
    void add(Weather weather);           //添加信息
    void update(Weather weathe);         //更新信息
    List addList(List list);    //多次添加
    List updateList(List list); //多次更新
}

serviceImpl

@Service 声明为服务,一般服务的方法在哪,@Service 就加在哪
@Autowired 是用在JavaBean中的注解,通过byType形式,用来将指定的字段或方法注入所需的外部资源。简单来说引用注释分为两类。
1.作为类中的属性,引用 @Autowired,可以在引用时除去getter and setter 这些Bean 的方法,加快引入的速度
2.作为方法,引用 @Autowired,可去除元素引入。
详细的可以参考该博客,https://www.w3cschool.cn/wkspring/rw2h1mmj.html

服务中方法的编写

一般服务的方法分为可直接通过Mapper自带方法控制的方法用Example自行编写语句的方法

1.可直接通过Mapper自带方法控制的方法

public void update(Weather weather) {//更新
        weatherMapper.updateByPrimaryKey(weather);
    }

2.用Example自行编写语句的方法

 public List queryList(String city,Integer weatherDegree,String weather,Integer highDegree ,Integer lowDegree, Date date){
        List list=new ArrayList<>();
        Example example=new Example(Weather.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleteFlag",0);
        if(city!=null&&!"".equals(city)){
            criteria.andLike("city","%"+city+"%");
        }
        criteria.andEqualTo("weather",weather);
        criteria.andGreaterThanOrEqualTo("weatherDegree",lowDegree).andLessThanOrEqualTo("weatherDegree",highDegree);
        list=weatherMapper.selectByExample(example);
        return list;
    }

样例

package com.lwk.service.impl;
import com.lwk.dao.WeatherMapper;
import com.lwk.entity.Weather;
import com.lwk.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class weatherServiceImpl implements WeatherService {
    @Autowired
    private WeatherMapper weatherMapper;

    public Weather queryById(Integer keyId) {//查询
        return weatherMapper.selectByPrimaryKey(keyId);
    }

    public List queryByDate(){//日期查询      //(没写完,自行完成)
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = df.format(date);

        String yearMonth = format.substring(0,format.length()-3);
        String day;
        List < Weather > list = new ArrayList<>();
        Example example=new Example(Weather.class);
        Example.Criteria criteria=example.createCriteria();
        criteria = criteria.andLike("date","%"+yearMonth+"%");
        weather = weatherMapper.selectOneByExample(example);
        return list;
    }

    public List queryList(String city,Integer weatherDegree,String weather,Integer highDegree ,Integer lowDegree, Date date){
        List list=new ArrayList<>();
        Example example=new Example(Weather.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleteFlag",0);
        if(city!=null&&!"".equals(city)){
            criteria.andLike("city","%"+city+"%");
        }
        criteria.andEqualTo("weather",weather);
        criteria.andGreaterThanOrEqualTo("weatherDegree",lowDegree).andLessThanOrEqualTo("weatherDegree",highDegree);
        list=weatherMapper.selectByExample(example);
        return list;
    }

    public void add(Weather weather) {//添加
        weatherMapper.insert(weather);
    }

    public void update(Weather weather) {//更新
        weatherMapper.updateByPrimaryKey(weather);
    }

    public Integer delete(Integer keyId) {//删除
        //查询
        //删除标记置为1
        //更新数据信息
        return null;
    }

    public List addList(List list) {//多添
        list = new ArrayList<>();
        return list;
    }

    public List updateList(List list) {//多改
        list = new ArrayList<>();
        return list;
    }
}

你可能感兴趣的:(注释用法)