代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
hibernate操作数据库
作者:
weinee
/ 发布于
2015/5/19
/
520
package com.coco.util.hibernate; import java.beans.PropertyDescriptor; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.annotation.Resource; import org.apache.logging.log4j.Logger; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Repository; import com.coco.helper.Helper; import com.coco.logging.log4j.LoggerHelper; import com.coco.util.pager.Pager; import com.coco.util.pager.PagerDialect; /** * * @param <E> */ @Repository public class SpringHibernateDaoSupport<E> implements HibernateDao { protected final Logger logger = LoggerHelper.getLogger(getClass()); public SpringHibernateDaoSupport() { super(); getEntityClass(); } @Resource private SessionFactory sessionFactory; @Resource(name = "oraclePagerDialect") private PagerDialect pagerDialect; private Class<E> entityClass; @SuppressWarnings("unchecked") public Class<E> getEntityClass() { if (entityClass == null && !SpringHibernateDaoSupport.class.equals(this.getClass())) { entityClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } return entityClass; } public PagerDialect getPagerDialect() { return pagerDialect; } public void setPagerDialect(PagerDialect pagerDialect) { this.pagerDialect = pagerDialect; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public Session getSession() { return sessionFactory.getCurrentSession(); } @SuppressWarnings("unchecked") public E get(Serializable id) { return (E) getSession().get(getEntityClass(), id); } @SuppressWarnings("unchecked") public <T> T get(Class<T> entityClass, Serializable id) { return (T) getSession().get(entityClass, id); } public Serializable save(Object entity) { Session s=getSession(); return s.save(entity); } public void update(Object entity) { getSession().update(entity); } public void delete(Object entity) { getSession().delete(entity); } public <T> void delete(Class<T> entityClass, Serializable id) { PropertyDescriptor pd = Helper.getPropertyDescriptor(entityClass, "id"); if (pd == null) { getSession().delete(get(entityClass, id)); } else { String hql = "DELETE FROM " + entityClass.getName() + " WHERE id = ?"; execute(hql, id); } } public Query createQuery(String hql, Object... parameters) { Query query = getSession().createQuery(hql); if (parameters != null) { int l = parameters.length; for (int i = 0; i < l; i++) { query.setParameter(i, parameters[i]); } } return query; } public Query createSQLQuery(String sql, Object... parameters) { Query query = getSession().createSQLQuery(sql); if (parameters != null) { int l = parameters.length; for (int i = 0; i < l; i++) { query.setParameter(i, parameters[i]); } } return query; } public int execute(String hql, Object... parameters) { Query query = this.createQuery(hql, parameters); return query.executeUpdate(); } public int executeSQL(String sql, Object... parameters) { Query query = this.createSQLQuery(sql, parameters); return query.executeUpdate(); } public List<?> query(String hql, Object... parameters) { Query query = this.createQuery(hql, parameters); return query.list(); } public List<?> querySQL(String sql, Object... parameters) { Query query = this.createSQLQuery(sql, parameters); return query.list(); } public Object uniqueResult(String hql, Object... parameters) { Query query = this.createQuery(hql, parameters); return query.uniqueResult(); } public Object uniqueResultSQL(String sql, Object... parameters) { Query query = this.createSQLQuery(sql, parameters); return query.uniqueResult(); } public Pager query(Pager pager, String hql, Object... parameters) { return pagerDialect.query(getSession(), pager, hql, parameters); } public Pager query(String hql, int pageIndex, int maxResults, Object... parameters) { Pager pager = new Pager(pageIndex, maxResults); return this.query(pager, hql, parameters); } public Pager querySQL(Pager pager, String sql, Object... parameters) { return pagerDialect.querySQL(getSession(), pager, sql, parameters); } public Pager querySQL(String sql, int pageIndex, int maxResults, Object... parameters) { Pager pager = new Pager(pageIndex, maxResults); return this.querySQL(pager, sql, parameters); } } package com.sinosoft.oa.dao; import org.springframework.stereotype.Repository; import com.sinosoft.oa.entity.Login; import com.sinosoft.util.hibernate.SpringHibernateDaoSupport; /** * Login实体的数据库操作DAO类 */ @Repository public class LoginDAO extends SpringHibernateDaoSupport<Login> { /** * 通过用户名查找登录对象 * @param username * @return */ public Login findLogin(String username){ return (Login)uniqueResult("from Login where username = ?", username); } /** * 通过用户名保存对象 * @param username * @return */ public Login saveLogin(String username, String password){ Login login = new Login(username, password); this.save(login); return login; } } 3. [代码]Pager ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 package com.coco.util.pager; import java.io.Serializable; import java.util.List; /** * 分页 * */ @SuppressWarnings("rawtypes") public class Pager implements Serializable { private static final long serialVersionUID = 7208853976754703078L; public Pager() { super(); total = -1; } public Pager(int pageIndex, int maxResults) { super(); this.pageIndex = pageIndex; this.maxResults = maxResults; } private List list; /** 当前页显示的数量,等于list.size */ private int size; /** 总记录的条数*/ private int total = -1; /** * 当前页序号 */ private int pageIndex; /** 总页数 */ private int pageCount; /** 每页最大记录 */ private int maxResults; public List list() { return list; } public int size() { return size; } public int total() { return total; } public List getList() { return list; } public void setList(List list) { this.list = list; this.size = list == null ? 0 : list.size(); } public int getSize() { return size; } public void setSize(int size) { this.size = size; } /** * * @return * @see #total() */ public int getTotal() { return total; } /** * * @param total */ public void setTotal(int total) { this.total = total; pageCount = (int) Math.ceil((double) total / (double) maxResults); pageIndex = Math.min(pageIndex, pageCount); } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getMaxResults() { return maxResults; } public void setMaxResults(int maxResults) { this.maxResults = maxResults; } /** * * @return */ public int getFirstResult() { return (pageIndex - 1) * maxResults; } }
试试其它关键字
hibernate
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
weinee
贡献的其它代码
(
4
)
.
hibernate操作数据库
.
回溯法解决0-1背包
.
动态规划解决钢条切割问题
.
最大子数组
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3