All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kukababy.dao.BaseSqlDAO Maven / Gradle / Ivy

The newest version!
package com.kukababy.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.kukababy.pager.Pager;
import com.kukababy.pager.SqlWhere;

public class BaseSqlDAO {
	private static final Logger log = LoggerFactory.getLogger(BaseSqlDAO.class);

	protected JdbcTemplate jdbcTemplate;

	/**
	 * 
	 * 描述: 
* *
	 * 分页查看数据
	 * 
* *
* * @param reqPage * @param cls * @param selectSql * @param countSql * @param args */ public void queryPager(Pager pager, String countSql, String resSql, Class cls, Object... args) { // log.debug("countSql=" + countSql); // log.debug("resSql=" + resSql); if (countSql != null) {// 需要计算总记录数 int total = 0; if (args == null || args.length == 0) { total = jdbcTemplate.queryForObject(countSql, Integer.class); } else { total = jdbcTemplate.queryForObject(countSql, args, Integer.class); } pager.setTotal(total);// 总记录数 } StringBuilder sqlSB = new StringBuilder(resSql); int currPage = pager.getCurrPage() - 1;// 前端第一页为1,需要减1 if (currPage < 0) { currPage = 0; } int first = currPage * pager.getPageSize(); sqlSB.append(SqlWhere.getSorts(pager.getSorts())); sqlSB.append(" limit " + first + "," + pager.getPageSize()); List rows = null; if (cls == null) {// 按原生返回 if (args == null || args.length == 0) { rows = jdbcTemplate.queryForList(sqlSB.toString());// 返回List> }else{ rows = jdbcTemplate.queryForList(sqlSB.toString(),args);// 返回List> } } else { if (args == null || args.length == 0) { rows = jdbcTemplate.query(sqlSB.toString(), new BeanPropertyRowMapper(cls)); }else{ rows = jdbcTemplate.query(sqlSB.toString(),args, new BeanPropertyRowMapper(cls)); } } pager.setRows(rows); } /** * * 描述::
* *
	 *  1、不计算总记录数
	 * 
* *
* * @param pager * @param resSql * @param cls */ public void queryPager(Pager pager, String resSql, Class cls,Object... args) { queryPager(pager, null, resSql, cls,args); } /** * * 描述:返回 List>:
* *
	 * 
* *
* * @param pager * @param countSql * @param resSql */ public void queryPager(Pager pager, String countSql, String resSql) { queryPager(pager, countSql, resSql, null); } /** * * 描述:返回 List> :
* *
	 * 1、不计算总记录数
	 * 
* *
* * @param pager * @param resSql */ public void queryPager(Pager pager, String resSql) { queryPager(pager, null, resSql, null); } /** * * 描述:
* *
	 * 返回列表
	 * 
* *
* * @param sql * @param cls * @param args * @return */ public List queryList(String sql, Class cls, Object... args) { if (args == null || args.length == 0) { return jdbcTemplate.query(sql, new BeanPropertyRowMapper(cls)); } else { return jdbcTemplate.query(sql, new BeanPropertyRowMapper(cls), args); } } /** * * 描述:
* *
	 * 返回總數
	 * 
* *
* * @param sql * @param args * @return */ @Deprecated public int queryCount(String sql, Object... args) { if (args == null || args.length == 0) { return jdbcTemplate.queryForObject(sql, Integer.class); } else { return jdbcTemplate.queryForObject(sql, args, Integer.class); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy