package com.songwie.task.base.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.hibernate.engine.spi.SessionImplementor;
import org.springframework.stereotype.Component;
/**
* 数据访问通用类(适合JPA调用)
* @author sw
*/
@Component
public class CommonDAO
{
@PersistenceContext
transient EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
protected Connection getConnection()
{
SessionImplementor session = this.getEntityManager().unwrap(SessionImplementor.class);
return session.connection();
};
/**
* 执行查询,并按照传入实体类{@code<T>}构造实体列表{@code List<T>}
* @param <T> 实体类模板
* @param strSql 查询SQL语句
* @param entityName 实体类
* @return 返回实体列表{@code List<T>}
*/
public <T> List<T> getEntityList(String strSql, Class<T> entityName)
{
return this.getEntityList(strSql, new ParaList(), entityName);
}
/**
* 使用参数<code>paras</code>执行查询,并按照传入实体类{@code<T>}构造实体列表{@code List<T>}
* @param <T> 实体类模板
* @param strSql 查询SQL语句
* @param paras 查询参数
* @param entityName 实体类
* @return 返回实体列表{@code List<T>}
*/
public <T> List<T> getEntityList(String strSql, List<Object> paras, Class<T> entityName)
{
try
{
Connection conn = this.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try
{
ps = conn.prepareStatement(strSql);
this.setParasToStatement(ps, paras);
rs = ps.executeQuery();
List<T> list = EntityUtils.ListFromRS(rs, entityName);
return list;
}
finally
{
if (ps != null)
{
ps.close();
ps = null;
}
if (rs != null)
{
rs.close();
rs = null;
}
}
}
catch(Exception ex)
{
throw new RuntimeException(ex);
}
}
/**
* 使用参数<code>paras</code>执行查询,并按照传入实体类{@code<T>}构造实体列表{@code List<T>}
* @param <T> 实体类模板
* @param strSql 查询SQL语句
* @param paras 查询参数
* @param entityName 实体类
* @return 返回实体列表{@code List<T>}
*/
public <T> List<T> getEntityList(String strSql, Object[] paras, Class<T> entityName)
{
return this.getEntityList(strSql, new ParaList(paras), entityName);
}
/**
* 设置参数
* @param ps
* @param paras
* @throws Exception
*/
protected void setParasToStatement(PreparedStatement ps, List<Object> paras) throws Exception
{
if (paras == null)
{
return;
}
if (paras.size() <= 0)
{
return;
}
for (int i = 0; i < paras.size(); i++)
{
Object para = paras.get(i);
if (para.getClass().equals(String.class))
{
ps.setString(i + 1, para.toString());
}
else if (para.getClass().equals(int.class))
{
ps.setInt(i + 1, (Integer.parseInt(para.toString())));
}
else if (para.getClass().equals(Integer.class))
{
ps.setInt(i + 1, ((Integer) para).intValue());
}
else if (para.getClass().equals(Long.class))
{
ps.setLong(i + 1, ((Long) para).longValue());
}
else if (para.getClass().equals(long.class))
{
ps.setLong(i + 1, (Long.parseLong(para.toString())));
}
else if (para.getClass().equals(double.class))
{
ps.setDouble(i + 1, (Double.parseDouble(para.toString())));
}
else if (para.getClass().equals(Double.class))
{
ps.setDouble(i + 1, ((Double) para).doubleValue());
}
}
}
protected void setListParasToQuery(Query query, List<Object> paras)
{
for (int i = 0; i < paras.size(); i++)
{
query.setParameter(i + 1, paras.get(i));
}
}
protected void setMapParasToQuery(Query query, Map<String, Object> paras)
{
for (String key : paras.keySet())
{
query.setParameter(key, paras.get(key));
}
}
/**
* 查询总行数
* @param strSql 查询SQL语句
* @return 总行数
*/
public int getTotal(String strSql)
{
return this.getTotal(strSql, new ParaList());
}
/**
* 使用参数<code>paras</code>执行查询总行数
* @param strSql 查询SQL语句
* @param paras 查询参数
* @return 总行数
*/
public int getTotal(String strSql, List<Object> paras)
{
strSql = "select count(1) from (" + strSql + ") t";
return this.querySingleInteger(strSql, paras);
}
/**
* 使用参数<code>paras</code>执行查询总行数
* @param strSql 查询SQL语句
* @param paras 查询参数
* @return 总行数
*/
public int getTotal(String strSql, Object[] paras)
{
return this.querySingleInteger(strSql, new ParaList(paras));
}
/**
* 使用参数<code>paras</code>执行查询总行数
* @param strSql 查询SQL语句
* @param paras 查询参数
* @return 总行数
*/
public int getTotal(String strSql, Map<String, Object> paras)
{
strSql = "select count(1) from (" + strSql + ") t";
return this.querySingleInteger(strSql, paras);
}
/**
* 执行分页查询,构造实体列表{@code List<T>}
* @param <T> 实体类模板
* @param strSql 查询SQL语句
* @param entityName 实体类
* @param start 开始行
* @param limit 页面行数
* @return 返回实体列表{@code List<T>}
*/
public <T> PageData<T> getEntityListPage(String strSql, Class<T> entityName, int start, int limit)
{
return this.getEntityListPage(strSql, new ParaList(), entityName, start, limit);
}
/**
* 使用参数<code>paras</code>执行分页查询,构造实体列表{@code List<T>}
* @param <T> 实体类模板
* @param strSql 查询SQL语句
* @param paras 查询参数
* @param entityName 实体类
* @param start 开始行
* @param limit 页面行数
* @return 返回实体列表{@code List<T>}
*/
public <T> PageData<T> getEntityListPage(String strSql, List<Object> paras, Class<T> entityName, int start, int limit)
{
try
{
Connection conn = this.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try
{
String strSQLQuery = strSql + "limit "+start +" ," + limit;
ps = conn.prepareStatement(strSQLQuery);
this.setParasToStatement(ps, paras);
rs = ps.executeQuery();
Lis
没有合适的资源?快使用搜索试试~ 我知道了~
管理系统系列--统一动态任务管理系统.zip
共668个文件
js:348个
png:82个
css:41个
需积分: 5 0 下载量 25 浏览量
2024-02-24
22:38:44
上传
评论
收藏 3.71MB ZIP 举报
温馨提示
管理系统系列--统一动态任务管理系统
资源推荐
资源详情
资源评论
收起资源包目录
管理系统系列--统一动态任务管理系统.zip (668个子文件)
ZeroClipboardPdf.as 9KB
ZeroClipboard.as 6KB
start.bat 394B
smartadmin-production.css 337KB
bootstrap.min.css 99KB
editor_ie7.css 47KB
editor_iequirks.css 46KB
editor_ie8.css 46KB
editor_ie.css 45KB
editor_gecko.css 45KB
editor.css 44KB
smartadmin-skins.css 32KB
layout.css 26KB
font-awesome.min.css 18KB
dialog_ie7.css 17KB
dialog_ie8.css 16KB
dialog_iequirks.css 16KB
dialog_ie.css 16KB
dialog_opera.css 16KB
dialog.css 15KB
bs_login.css 13KB
TableTools.css 9KB
layer.css 6KB
sample.css 5KB
style.css 4KB
TableTools_JUI.css 4KB
layer.ext.css 3KB
jquery.nouislider.css 3KB
outputxhtml.css 2KB
contents.css 2KB
templates.css 1KB
login.css 1KB
toolbar.css 1KB
wsc.css 1KB
slick.headermenu.css 1KB
slick.headerbuttons.css 903B
slick.pager.css 728B
jquery-jvectormap-1.2.2.css 716B
jquery-jvectormap-1.1.1.css 716B
slick.columnpicker.css 529B
taskmanger.css 526B
llbmain.css 453B
llb.css 447B
sample.css 50B
fontawesome-webfont.eot 37KB
glyphicons-halflings-regular.eot 20KB
.factorypath 28KB
outputforflash.fla 84KB
xubox_loading0.gif 6KB
xubox_loading3.gif 2KB
xubox_loading2.gif 2KB
xubox_loading1.gif 701B
cry_smile.gif 468B
angel_smile.gif 465B
confused_smile.gif 464B
sad_smile.gif 460B
wink_smile.gif 458B
whatchutalkingabout_smile.gif 452B
omg_smile.gif 451B
regular_smile.gif 450B
shades_smile.gif 449B
tongue_smile.gif 446B
tounge_smile.gif 446B
angry_smile.gif 443B
teeth_smile.gif 442B
embarrassed_smile.gif 442B
embaressed_smile.gif 442B
devil_smile.gif 436B
envelope.gif 426B
template3.gif 422B
thumbs_down.gif 408B
thumbs_up.gif 396B
template1.gif 375B
lightbulb.gif 368B
template2.gif 333B
kiss.gif 241B
broken_heart.gif 192B
heart.gif 183B
hiddenfield.gif 105B
pagebreak.gif 54B
spacer.gif 43B
.gitignore 140B
TableTools.min.js.gz 9KB
datatables.html 120KB
datafiltering.html 34KB
inlineall.html 9KB
outputforflash.html 9KB
toolbar.html 8KB
magicline.html 8KB
fullpage.html 8KB
jquery.html 7KB
dialog.html 7KB
outputhtml.html 7KB
api.html 7KB
replacebyclass.html 7KB
replacebycode.html 7KB
xhtmlstyle.html 6KB
inlinebycode.html 6KB
index.html 6KB
inlinetextarea.html 5KB
共 668 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
Kwan的解忧杂货铺
- 粉丝: 1w+
- 资源: 3661
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功