我的习惯 parameterType(输入参数,统一用map,放在map里面)
<resultMap id="BaseResultMap" type="com.finance.cmp.ruleEngine.dao.model.TVarBasic">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="var_name" jdbcType="VARCHAR" property="varName" />
<result column="var_caption" jdbcType="VARCHAR" property="varCaption" />
<result column="var_sourceType" jdbcType="VARCHAR" property="varSourcetype" />
<result column="var_sourceRestful" jdbcType="VARCHAR" property="varSourcerestful" />
<result column="var_sourceVar" jdbcType="VARCHAR" property="varSourcevar" />
<result column="status" jdbcType="VARCHAR" property="status" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
</resultMap>
1.查询有多少条数据count 返回一条一列的int ,resultType为int
<select id="getTotalVarBasicList" parameterType="map" resultType="int">
SELECT COUNT(1) FROM t_var_basic
where 1=1
<if test="varName != null and varName != '' ">
and var_name = #{varName}
</if>
<if test="varCaption != null and varCaption != '' ">
and var_caption = #{varCaption}
</if>
<if test="varSourcetype != null and varSourcetype != '' ">
and var_sourceType = #{varSourcetype}
</if>
</select>
2.查询单表 返回一个List 动态slq 分页 resultMap 为自动生成的BaseResultMap
<select id="getVarBasicList" parameterType="map" resultMap="BaseResultMap">
SELECT * FROM t_var_basic
where 1=1
<if test="varName != null and varName != '' ">
and var_name = #{varName}
</if>
<if test="varCaption != null and varCaption != '' ">
and var_caption = #{varCaption}
</if>
<if test="varSourcetype != null and varSourcetype != '' ">
and var_sourceType = #{varSourcetype}
</if>
<if test="start != null ">
LIMIT #{start}, #{size}
</if>
</select>
3.dao层接口:
//查总条数 不分页
int getTotalVarBasicList(Map<String, Object> temMap);
//分页
List<TVarBasic> getVarBasicList(Map<String, Object> temMap);
4.service接口:
//VarBasicQueryVo 里面把分页的参数和要按什么条件查的参数
//统一封装到自己定义的这个对象里
Map<String, Object> selectByList(VarBasicQueryVo vo);
5.service 实现类:
@Override
public Map<String,Object> selectByList(VarBasicQueryVo vo) {
Map<String,Object> resultMap=new HashMap<>();
Map<String,Object> temMap=new HashMap<>();
//把vo里封装的参数放入map中 也可一个个put
temMap=MapUtils.beanToMap(vo, Constant.LIST_FIELD);
//查询总条数
int totalNum=tVarBasicMapper.getTotalVarBasicList(temMap);
log.info("总条数为:"+totalNum);
if(totalNum<1) {
resultMap.put("total", 0);
resultMap.put("data", null);
return resultMap;
}
String pageIndex = vo.getPage().toString();
String pageSize = vo.getRows().toString();
temMap.put("start", 0);
temMap.put("size", 10);
if ((!AcStringUtils.isEmpty(pageIndex)) && (!AcStringUtils.isEmpty(pageSize))) {
int start = ((Integer.parseInt(pageIndex)) - 1) * (Integer.parseInt(pageSize));
int size = Integer.parseInt(pageSize);
// int
// end=((Integer.parseInt(pageIndex)))*(Integer.parseInt(pageSize));
temMap.put("start", start);
temMap.put("size", size);
}
//分页按条件
List<TVarBasic> dataList= tVarBasicMapper.getVarBasicList(temMap);
resultMap.put("pageIndex", pageIndex);
resultMap.put("totalPages"
,(totalNum % Integer.parseInt(pageSize) > 0
? totalNum / Integer.parseInt(pageSize) + 1
:totalNum / Integer.parseInt(pageSize)
)
);
resultMap.put("total", totalNum);
resultMap.put("data", dataList);
return resultMap;
}
6.controller:
/**
* select all
*
* @param vo
* @return
*/
@PostMapping("list")
public Result selectList(@RequestBody @Validated VarBasicQueryVo vo) {
Map<String, Object> resultMap = null;
try {
resultMap = itVarBasicService.selectByList(vo);
} catch (Exception e) {
return ResultUtil.error(ResultEnum.SERVER_ERROR, e.getMessage());
}
return ResultUtil.success(resultMap);
}