com.kukababy.dao.BaseSqlDAO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbquery Show documentation
Show all versions of dbquery Show documentation
Unified query of Mongodb and Sql database.
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