@SelectProvider的用法

那一个分页查询的列子来说
controller

articleService.getArticlesInPage(park, title, index, size, group, status)

service

	public List<Article> getArticlesInPage(Integer park, String title, Integer pageIndex, 
			Integer pageSize,Integer group,Integer status) {
		return articleDao.selectByLimit(park, title, (pageIndex - 1) * pageSize, pageSize,group,status);
	}

dao

	@SelectProvider(type = ArticleProvider.class, method = "selectByLimit")
	List<Article> selectByLimit(@Param("park") Integer park, @Param("title") String title,
			@Param("index") Integer index, @Param("pageSize") Integer pageSize,@Param("group") Integer group,
			@Param("status") Integer status);

ArticleProvider

public String selectByLimit(Map<String, Object> para) {
		String sql = "SELECT a.id, title, ParkNameCN,t.name,types.typeName,type,st.sequence, create_date, modify_date, is_delete "
				+ "FROM article a left join park p on a.park=p.id left join segment_top st on a.id=st.article_id"
				+" left join segment t on t.id=st.segment_id left join types on types.id=a.type"
				+ " WHERE %s title LIKE CONCAT('%%',#{title,jdbcType=VARCHAR},'%%')"
				+ "ORDER BY modify_date DESC,sequence ASC LIMIT #{index,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER};";
		Integer park = Integer.parseInt(para.get("park").toString());
		Integer group = Integer.parseInt(para.get("group").toString());
		Integer status = Integer.parseInt(para.get("status").toString());
		String formatSql="";
		if (park == 0) {
			if(group==0){
				formatSql="";
				}else{
					formatSql="segment_id=#{group,jdbcType=INTEGER} AND ";
				}
		}
		else{
			if(group==0){
			formatSql="park = #{park,jdbcType=INTEGER} AND ";
			}else{
				formatSql="park = #{park,jdbcType=INTEGER} AND segment_id=#{group,jdbcType=INTEGER} AND ";
			}
		}
		if(status==0){
			formatSql = formatSql+" a.is_delete=0 AND";
		}else if(status==1){
			formatSql = formatSql+" a.is_delete<>0 AND";
		}
		sql=String.format(sql, formatSql);
		return sql;
	}

可以看出来SelectProvider类似引用一个方法的作用

你可能感兴趣的:(java)