Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.kukababy.plus.dao.SelectImpl Maven / Gradle / Ivy
package com.kukababy.plus.dao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import com.alibaba.fastjson.JSON;
import com.kukababy.plus.dao.db.DbHelper;
import com.kukababy.plus.exception.PlusRuntimeException;
import com.kukababy.plus.pager.BuildWhere;
import com.kukababy.plus.pager.Page;
import com.kukababy.plus.pager.Pager;
import com.kukababy.plus.pager.SqlFilter;
import com.kukababy.plus.pager.SqlRes;
import com.kukababy.plus.pojo.Param;
import com.kukababy.plus.pojo.PlusCols;
import com.kukababy.plus.pojo.PlusPO;
import com.kukababy.plus.pojo.PlusParam;
import com.kukababy.plus.utils.Constant;
import com.kukababy.plus.utils.PlusUtils;
import com.kukababy.plus.utils.StringUtil;
/**
*
* 描述:
*
*
* @author [email protected]
* @date 2019年3月5日 下午10:52:48
*/
public class SelectImpl extends BasePlus implements SelectFace {
private static final Logger log = LoggerFactory.getLogger(SelectImpl.class);
/**
* @param injectParam
*/
public SelectImpl(PlusParam plusParam) {
super(plusParam);
// TODO Auto-generated constructor stub
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.InsertJdbc#get(java.lang.Class,
* java.io.Serializable)
*/
@Override
public T get(Class entityClass, Serializable id) {
PlusCols plusCols = this.getPlusCols(entityClass);
String sql = "select * from " + plusCols.getTableName() + " where " + plusCols.getKey() + " = ?";
try {
T plusPO = entityClass.newInstance();
Map map = plusParam.getJdbcTemplate().queryForMap(sql, id);
for (Map.Entry entry : map.entrySet()) {
String colName = entry.getKey();
for (String col : plusCols.getCols()) {
if (colName.equals(col)) {
PlusUtils.setValue(plusPO, col, entry.getValue());
}
}
}
return plusPO;
} catch (EmptyResultDataAccessException e) {
return null;
} catch (InstantiationException | IllegalAccessException e) {
throw new PlusRuntimeException(e);
}
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#select(java.lang.String,
* com.kukababy.plus.pager.Pager)
*/
@Override
public Page select(String sql, Pager pager, Class entityClass) {
Page resPage = new Page();
Page> page = select(sql, pager);
PlusCols plusCols = this.getPlusCols(entityClass);
for (Map rowMap : page.getRows()) {
T plusPO = PlusUtils.map2PlusPO(plusCols.getCol2Var(), rowMap, entityClass);
resPage.getRows().add(plusPO);
}
resPage.setCurrPage(page.getCurrPage());
resPage.setPageSize(page.getPageSize());
resPage.setTotal(page.getTotal());
return resPage;
}
@Override
public Page> select(String sql, Pager pager) {
SqlRes sqlRes = BuildWhere.getWheres(pager.getSqlFilter());
String sqlWhere = sqlRes.getWhereSql();
Object sqlVals[] = sqlRes.getWhereVals().toArray();
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + sqlRes.getWhereVals());
}
int total = 0;
if (pager.getTotal() == 0) {// 需要计算总记录数
String countSql = StringUtil.getCountSql(sql) + sqlWhere;
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + countSql);
}
if (sqlVals.length == 0) {
total = plusParam.getJdbcTemplate().queryForObject(countSql, Integer.class);
} else {
total = plusParam.getJdbcTemplate().queryForObject(countSql, Integer.class, sqlVals);
}
}
String pageSql = DbHelper.getPageSql(sql, pager, sqlWhere, plusParam);
Page> page = new Page>();
List> rows;
if (sqlVals.length == 0) {
rows = plusParam.getJdbcTemplate().queryForList(pageSql);
} else {
rows = plusParam.getJdbcTemplate().queryForList(pageSql, sqlVals);
}
page.setRows(rows);
page.setCurrPage(pager.getCurrPage());
page.setPageSize(pager.getPageSize());
page.setTotal(total);
return page;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#selectHump(java.lang.String,
* com.kukababy.plus.pager.Pager)
*/
@Override
public Page> selectHump(String sql, Pager pager) {
Page> page = select(sql, pager);
List> humpRows = new ArrayList();
for (Map map : page.getRows()) {
humpRows.add(StringUtil.humpConvert(map));
}
page.setRows(humpRows);
return page;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#getAll(java.lang.Class)
*/
@Override
public List getAll(Class entityClass) {
PlusCols plusCols = this.getPlusCols(entityClass);
String sql = "select * from " + plusCols.getTableName();
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + sql);
}
List> list = plusParam.getJdbcTemplate().queryForList(sql);
List resList = new ArrayList();
for (Map rowMap : list) {
T plusPO = PlusUtils.map2PlusPO(plusCols.getCol2Var(), rowMap, entityClass);
resList.add(plusPO);
}
return resList;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#select(java.lang.String,
* com.kukababy.plus.pager.SqlFilter, java.lang.Class)
*/
@Override
public List select(String sql, SqlFilter sqlFilter, Class entityClass) {
List> rows = select(sql, sqlFilter);
PlusCols plusCols = this.getPlusCols(entityClass);
List resList = new ArrayList();
for (Map rowMap : rows) {
T plusPO = PlusUtils.map2PlusPO(plusCols.getCol2Var(), rowMap, entityClass);
resList.add(plusPO);
}
return resList;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#select(java.lang.String,
* com.kukababy.plus.pager.SqlFilter)
*/
@Override
public List> select(String sql, SqlFilter sqlFilter) {
SqlRes sqlRes = BuildWhere.getWheres(sqlFilter);
String sqlWhere = sqlRes.getWhereSql();
Object sqlVals[] = sqlRes.getWhereVals().toArray();
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + sqlRes.getWhereVals());
}
StringBuilder sqlSB = new StringBuilder(sqlWhere);
sqlSB.append(BuildWhere.getSorts(sqlFilter.getSorts()));
String joinSql = sql + sqlSB.toString();
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + joinSql);
}
List> rows;
if (sqlVals.length == 0) {
rows = plusParam.getJdbcTemplate().queryForList(joinSql);
} else {
rows = plusParam.getJdbcTemplate().queryForList(joinSql, sqlVals);
}
return rows;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#selectHump(java.lang.String,
* com.kukababy.plus.pager.SqlFilter)
*/
@Override
public List> selectHump(String sql, SqlFilter sqlFilter) {
List> rows = select(sql, sqlFilter);
List> humpRows = new ArrayList();
for (Map map : rows) {
humpRows.add(StringUtil.humpConvert(map));
}
return humpRows;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#getTotal(java.lang.Class)
*/
@Override
public int getTotal(Class extends PlusPO> entityClass) {
PlusCols plusCols = this.getPlusCols(entityClass);
String countSql = "select count(1) from " + plusCols.getTableName();
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + countSql);
}
int total = plusParam.getJdbcTemplate().queryForObject(countSql, Integer.class);
return total;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#getTotal(java.lang.Class,
* java.lang.Object[])
*/
@Override
public int getTotal(Class extends PlusPO> entityClass, Param... whereColVals) {
PlusCols plusCols = this.getPlusCols(entityClass);
// 可以支持列的名字,写成变量的名字,此处需要转换为列名
PlusUtils.varConvertCol(plusCols.getCol2Var(), whereColVals);
SqlFilter sqlFilter = PlusUtils.param2SqlFilter(whereColVals);
SqlRes sqlRes = BuildWhere.getWheres(sqlFilter);
String sqlWhere = sqlRes.getWhereSql();
Object sqlVals[] = sqlRes.getWhereVals().toArray();
String countSql = "select count(1) from " + plusCols.getTableName() + sqlWhere;
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + countSql);
}
int total = 0;
if (sqlVals.length == 0) {
total = plusParam.getJdbcTemplate().queryForObject(countSql, Integer.class);
} else {
total = plusParam.getJdbcTemplate().queryForObject(countSql, Integer.class, sqlVals);
}
return total;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#getTotal(java.lang.String,
* com.kukababy.plus.pager.SqlFilter)
*/
@Override
public int getTotal(String sql, SqlFilter sqlFilter) {
SqlRes sqlRes = BuildWhere.getWheres(sqlFilter);
String sqlWhere = sqlRes.getWhereSql();
Object sqlVals[] = sqlRes.getWhereVals().toArray();
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + sqlRes.getWhereVals());
}
String countSql = sql + sqlWhere;
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + countSql);
}
int total = 0;
if (sqlVals.length == 0) {
total = plusParam.getJdbcTemplate().queryForObject(countSql, Integer.class);
} else {
total = plusParam.getJdbcTemplate().queryForObject(countSql, Integer.class, sqlVals);
}
return total;
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#selectBy(java.lang.String,
* java.lang.Object[])
*/
@Override
public List> select(String sql, Param... whereColVals) {
return this.select(false, sql, whereColVals);
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#select(boolean, java.lang.String,
* com.kukababy.plus.pojo.Param[])
*/
@Override
public List> select(boolean wherePre, String sql, Param... whereColVals) {
SqlFilter sqlFilter = PlusUtils.param2SqlFilter(whereColVals);
sqlFilter.setWherePre(wherePre);
return this.select(sql, sqlFilter);
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#select(java.lang.Class,
* com.kukababy.plus.pojo.P[])
*/
@Override
public List select(Class entityClass, Param... whereColVals) {
PlusCols plusCols = this.getPlusCols(entityClass);
// 可以支持列的名字,写成变量的名字,此处需要转换为列名
PlusUtils.varConvertCol(plusCols.getCol2Var(), whereColVals);
SqlFilter sqlFilter = PlusUtils.param2SqlFilter(whereColVals);
String sql = "select * from " + plusCols.getTableName();
return this.select(sql, sqlFilter, entityClass);
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#selectOne(java.lang.Class,
* com.kukababy.plus.pojo.Param[])
*/
@Override
public T selectOne(Class entityClass, Param... whereColVals) {
PlusCols plusCols = this.getPlusCols(entityClass);
// 可以支持列的名字,写成变量的名字,此处需要转换为列名
PlusUtils.varConvertCol(plusCols.getCol2Var(), whereColVals);
SqlFilter sqlFilter = PlusUtils.param2SqlFilter(whereColVals);
String sql = "select * from " + plusCols.getTableName();
Pager pager = new Pager();
pager.setTotal(1);// 不统计总数
pager.setPageSize(1);// 查一条
pager.setSqlFilter(sqlFilter);
Page page = this.select(sql, pager, entityClass);
if (page.getRows().isEmpty()) {
return null;
} else {
return page.getRows().get(0);
}
}
/*
* (non-Javadoc)
*
* @see com.kukababy.plus.dao.SelectFace#selectOne(java.lang.String,
* com.kukababy.plus.pojo.Param[])
*/
@Override
public Map selectOne(String sql, Param... whereColVals) {
return selectOne(false, sql, whereColVals);
}
@Override
public Map selectOne(boolean wherePre, String sql, Param... whereColVals) {
SqlFilter sqlFilter = PlusUtils.param2SqlFilter(whereColVals);
sqlFilter.setWherePre(wherePre);
Pager pager = new Pager();
pager.setTotal(1);// 不统计总数
pager.setPageSize(1);// 查一条
pager.setSqlFilter(sqlFilter);
Page> page = select(sql, pager);
if (page.getRows().isEmpty()) {
return null;
} else {
return page.getRows().get(0);
}
}
}