package cn.com.tiros.jdbc;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository("jdbcTemplates")
public class JdbcTemplates {
@Resource
private JdbcTemplate jdbcTemp;
/**
* 增删改操作
* @param sql
* @return
* @throws Exception
*/
public int execute(String sql) throws Exception {
return jdbcTemp.update(sql);
}
/**
* 增删改操作
* @param sql
* @return
* @throws Exception
*/
public int execute(String sql, Object[] params) throws Exception {
return jdbcTemp.update(sql, params);
}
/**
* 获得查询的总数
* @param sql
* @return
* @throws Exception
*/
public int queryGetCount(String sql) throws Exception {
try {
return jdbcTemp.queryForInt(sql);
} catch (Exception e) {
throw e;
}
}
/**获得查询的总数
* @param sql
* @param params
* @return
* @throws Exception
*/
public int queryGetCount(String sql, Object[] params)
throws Exception {
return jdbcTemp.queryForInt(sql, params);
}
/**
* 获得查询的结果集 返回List<Map<String, Object>>
* @param sql
* @param params
* @return
* @throws Exception
*/
public List<Map<String, Object>> queryGetList(String sql, Object[] params) throws Exception {
return jdbcTemp.queryForList(sql, params);
}
/**
* 获得查询的结果集 返回List<Map<String, Object>>
* @param sql
* @return
* @throws Exception
*/
public List<Map<String, Object>> queryGetList(String sql) throws Exception {
return jdbcTemp.queryForList(sql);
}
/**
* 获得一行的值 返回 MAP
* @param sql
* @param params
* @return
* @throws Exception
*/
public Map<String, Object> queryForMap(String sql, Object[] params) throws Exception {
List<Map<String, Object>> list=jdbcTemp.queryForList(sql,params);
if(list==null||list.size()==0){
return null;
}else{
return (Map<String, Object>)list.get(0);
}
}
/**
* 获得一行的值 返回 MAP
* @param sql
* @param params
* @return
* @throws Exception
*/
public Map<String, Object> queryForMap(String sql) throws Exception {
List<Map<String, Object>> list=jdbcTemp.queryForList(sql);
if(list==null||list.size()==0){
return null;
}else{
return (Map<String, Object>)list.get(0);
}
}
/**
* 批量更新(小批量)
* @param sql
* @return
* @throws Exception
*/
public int[] batchUpdate(String[] sql) throws Exception {
return jdbcTemp.batchUpdate(sql);
}
}