public abstract class BaseService<D extends BaseMapper<T>, T extends BaseEntity>是什么意思
时间: 2023-06-04 20:08:42 浏览: 320
这是Java中泛型类的定义,其中BaseService是一个抽象类,D是泛型参数,表示该类中的成员变量或方法参数都必须是BaseMapper或其子类的实例,T也是泛型参数,表示该类中的成员变量或方法参数都必须是BaseEntity或其子类的实例。
相关问题
springboot项目中public interface MyBaseMapper<T> extends Mapper<T>, MySqlMapper<T>,MyInsertListMapper<T> { }基础分页如何写代码
可以使用MyBatis的PageHelper插件来实现基础分页。以下是一个示例代码:
```
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
public abstract class BaseService<M extends Mapper<T>, T> {
@Autowired
protected M mapper;
public PageInfo<T> page(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<T> list = mapper.selectAll();
return new PageInfo<>(list);
}
}
```
这里定义一个抽象类BaseService,泛型M为Mapper类型,T为实体类型。在page方法中,先调用PageHelper.startPage方法设置分页参数,然后使用Mapper的selectAll方法查询所有数据,最后将查询结果封装在PageInfo对象中返回。在具体的Service中继承BaseService并指定泛型即可使用该方法进行基础分页。
vo类 public class ArealRainfallVo implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "区域编码") private String code; @ApiModelProperty(value = "雨量总和") private BigDecimal value; } Mapper类 public interface WeightBetweenObjectAndRainfallStationMapper extends BaseMapper<WeightBetweenObjectAndRainfallStation> { List<ArealRainfallVo> ListbySumList (List<PointRainfallSumVO> list); } mapper配置类 <mapper namespace="com.ndwp.jboot.bootstrap.mapper.WeightBetweenObjectAndRainfallStationMapper"> <select id="ListbySumList" resultType="com.ndwp.jboot.bootstrap.entity.vo.ArealRainfallVo"> SELECT code, SUM(CASE <foreach collection="list" item = "item"> WHEN rainfall_station_code = '${item.getCode()}' THEN weight * ${item.getValue()} </foreach> END ) AS value FROM bootstrap.weight_between_object_and_rainfall_station GROUP BY code; </select> </mapper> 服务类 public interface IWeightBetweenObjectAndRainfallStationService extends BaseService<WeightBetweenObjectAndRainfallStation> { List<ArealRainfallVo> listAreaRainfall(Date start, Date end); } 服务实现类 @Service public class WeightBetweenObjectAndRainfallStationServiceImpl extends BaseServiceImpl<WeightBetweenObjectAndRainfallStationMapper, WeightBetweenObjectAndRainfallStation> implements IWeightBetweenObjectAndRainfallStationService { @Autowired IDataHistoricRainfallService dataHistoricRainfallService; @Override public List<ArealRainfallVo> listAreaRainfall(Date start, Date end) { List<PointRainfallSumVO> pointRainfallSumVOS = dataHistoricRainfallService.listAllPointRainfallByTime(start, end); return baseMapper.ListbySumList(pointRainfallSumVOS); } } 控制台打印的sql语句能够正常运行,结合代码分析一下为何会返回四个空对象?
根据你提供的代码,我注意到你在mapper配置文件中使用了foreach循环来动态生成SQL语句中的条件部分。然而,你在foreach循环中使用了`${item.getCode()}`和`${item.getValue()}`来获取集合中每个元素的属性值,这是不正确的。
在MyBatis中,`${}`语法用于替换普通的字符串,它不会对参数进行预处理或转义。所以在你的SQL语句中,`${item.getCode()}`和`${item.getValue()}`会被直接替换为对应元素的`getCode()`和`getValue()`方法的返回值,而不是参数值。
为了修复这个问题,你可以改用`#{}`语法来将参数作为预处理参数传递给SQL语句。修改后的mapper配置文件如下所示:
```xml
<mapper namespace="com.ndwp.jboot.bootstrap.mapper.WeightBetweenObjectAndRainfallStationMapper">
<select id="ListbySumList" resultType="com.ndwp.jboot.bootstrap.entity.vo.ArealRainfallVo">
SELECT code, SUM(CASE
<foreach collection="list" item="item" separator=" ">
WHEN rainfall_station_code = #{item.code} THEN weight * #{item.value}
</foreach> END ) AS value
FROM bootstrap.weight_between_object_and_rainfall_station
GROUP BY code;
</select>
</mapper>
```
修改后重新运行代码,应该能够正确地获取到数据库中的数据并返回非空对象。
阅读全文