- 浏览: 602540 次
- 性别:
- 来自: 厦门
文章分类
- 全部博客 (669)
- oracle (36)
- java (98)
- spring (48)
- UML (2)
- hibernate (10)
- tomcat (7)
- 高性能 (11)
- mysql (25)
- sql (19)
- web (42)
- 数据库设计 (4)
- Nio (6)
- Netty (8)
- Excel (3)
- File (4)
- AOP (1)
- Jetty (1)
- Log4J (4)
- 链表 (1)
- Spring Junit4 (3)
- Autowired Resource (0)
- Jackson (1)
- Javascript (58)
- Spring Cache (2)
- Spring - CXF (2)
- Spring Inject (2)
- 汉字拼音 (3)
- 代理模式 (3)
- Spring事务 (4)
- ActiveMQ (6)
- XML (3)
- Cglib (2)
- Activiti (15)
- 附件问题 (1)
- javaMail (1)
- Thread (19)
- 算法 (6)
- 正则表达式 (3)
- 国际化 (2)
- Json (3)
- EJB (3)
- Struts2 (1)
- Maven (7)
- Mybatis (7)
- Redis (8)
- DWR (1)
- Lucene (2)
- Linux (73)
- 杂谈 (2)
- CSS (13)
- Linux服务篇 (3)
- Kettle (9)
- android (81)
- protocol (2)
- EasyUI (6)
- nginx (2)
- zookeeper (6)
- Hadoop (41)
- cache (7)
- shiro (3)
- HBase (12)
- Hive (8)
- Spark (15)
- Scala (16)
- YARN (3)
- Kafka (5)
- Sqoop (2)
- Pig (3)
- Vue (6)
- sprint boot (19)
- dubbo (2)
- mongodb (2)
最新评论
注意:如果业务DAO中的方法名与CommonDAOImpl中的方法名相同 比如save方法 需要使用super.save(Object obj)
1.公共的CommonDAOImpl
2.业务Dao
1.公共的CommonDAOImpl
import java.io.Serializable; import java.math.BigDecimal; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Types; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.hibernate.Filter; import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.Query; import org.hibernate.ReplicationMode; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.DetachedCriteria; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.support.SQLExceptionTranslator; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /** * * 通用dao的hibernate实现 * */ @SuppressWarnings({"unchecked","deprecation"}) @Repository("commonDao") public class CommonDaoImpl extends HibernateDaoSupport implements CommonDao { //下面这个方法挺重要 必须加上 否则报错 @Autowired public void setSessionFactoryOverride(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); } public int executeUpdate(String sql) throws DataAccessException{ return getSessionFactory().getCurrentSession().createSQLQuery(sql).executeUpdate(); } public int bulkUpdate(String queryString) throws DataAccessException { return getHibernateTemplate().bulkUpdate(queryString); } public int bulkUpdate(String queryString, Object value) throws DataAccessException { return getHibernateTemplate().bulkUpdate(queryString, value); } public int bulkUpdate(String queryString, Object[] values) throws DataAccessException { return getHibernateTemplate().bulkUpdate(queryString, values); } public void clear() throws DataAccessException { getHibernateTemplate().clear(); } public void closeIterator(Iterator it) throws DataAccessException { getHibernateTemplate().closeIterator(it); } public boolean contains(Object entity) throws DataAccessException { return getHibernateTemplate().contains(entity); } public void delete(Object entity) throws DataAccessException { getHibernateTemplate().delete(entity); } public void delete(Object entity, LockMode lockMode) throws DataAccessException { getHibernateTemplate().delete(entity, lockMode); } public void deleteAll(Collection entities) throws DataAccessException { getHibernateTemplate().deleteAll(entities); } public Filter enableFilter(String filterName) throws IllegalStateException { return getHibernateTemplate().enableFilter(filterName); } public void evict(Object entity) throws DataAccessException { getHibernateTemplate().evict(entity); } public Object execute(HibernateCallback action) throws DataAccessException { return getHibernateTemplate().execute(action); } public List executeFind(HibernateCallback action) throws DataAccessException { return getHibernateTemplate().executeFind(action); } public List find(String queryString) throws DataAccessException { return getHibernateTemplate().find(queryString); } public List find(String queryString, int rowrows) throws DataAccessException { Session session = this.getSession(); List list = null; try { Query query = session.createQuery(queryString); if(rowrows > 0){ query = query.setMaxResults(rowrows); } list = query.list(); } catch (HibernateException e) { throw e; } finally{ releaseSession(session); } return list; } public List findIntervalData(String hql, int pageNo, int endNo) { Session session = this.getSession(); List list = null; try { Query query = session.createQuery(hql); if((endNo-pageNo) > 0){ query = query.setFirstResult(pageNo).setMaxResults(endNo); } list = query.list(); } catch (HibernateException e) { throw e; } finally{ releaseSession(session); } return list; } public List find(String queryString, Object value) throws DataAccessException { return getHibernateTemplate().find(queryString, value); } public List find(String queryString, Object[] values) throws DataAccessException { return getHibernateTemplate().find(queryString, values); } public List findByCriteria(DetachedCriteria criteria) throws DataAccessException { return getHibernateTemplate().findByCriteria(criteria); } public List findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) throws DataAccessException { return getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults); } public List findByExample(Object exampleEntity) throws DataAccessException { return getHibernateTemplate().findByExample(exampleEntity); } public List findByExample(Object exampleEntity, int firstResult, int maxResults) throws DataAccessException { return getHibernateTemplate().findByExample(exampleEntity, firstResult, maxResults); } public List findByNamedParam(String queryString, String paramName, Object value) throws DataAccessException { return getHibernateTemplate().findByNamedParam(queryString, paramName, value); } public List findByNamedParam(String queryString, String[] paramNames, Object[] values) throws DataAccessException { return getHibernateTemplate().findByNamedParam(queryString, paramNames, values); } public List findByNamedQuery(String queryName) throws DataAccessException { return getHibernateTemplate().findByNamedQuery(queryName); } public List findByNamedQuery(String queryName, Object value) throws DataAccessException { return getHibernateTemplate().findByNamedQuery(queryName, value); } public List findByNamedQuery(String queryName, Object[] values) throws DataAccessException { return getHibernateTemplate().findByNamedQuery(queryName, values); } public List findByNamedQueryAndNamedParam(String queryName, String paramName, Object value) throws DataAccessException { return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramName, value); } public List findByNamedQueryAndNamedParam(String queryName, String[] paramNames, Object[] values) throws DataAccessException { return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramNames, values); } public List findByNamedQueryAndValueBean(String queryName, Object valueBean) throws DataAccessException { return getHibernateTemplate().findByNamedQueryAndValueBean(queryName, valueBean); } public List findByValueBean(String queryString, Object valueBean) throws DataAccessException { return getHibernateTemplate().findByValueBean(queryString, valueBean); } public void flush() throws DataAccessException { getHibernateTemplate().flush(); } public Object get(Class entityClass, Serializable id) throws DataAccessException { return getHibernateTemplate().get(entityClass, id); } public Object get(String entityName, Serializable id) throws DataAccessException { return getHibernateTemplate().get(entityName, id); } public Object get(Class entityClass, Serializable id, LockMode lockMode) throws DataAccessException { return getHibernateTemplate().get(entityClass, id, lockMode); } public Object get(String entityName, Serializable id, LockMode lockMode) throws DataAccessException { return getHibernateTemplate().get(entityName, id, lockMode); } public void initialize(Object proxy) throws DataAccessException { getHibernateTemplate().initialize(proxy); } public Iterator iterate(String queryString) throws DataAccessException { return getHibernateTemplate().iterate(queryString); } public Iterator iterate(String queryString, Object value) throws DataAccessException { return getHibernateTemplate().iterate(queryString, value); } public Iterator iterate(String queryString, Object[] values) throws DataAccessException { return getHibernateTemplate().iterate(queryString, values); } public Object load(Class entityClass, Serializable id) throws DataAccessException { return getHibernateTemplate().load(entityClass, id); } public Object load(String entityName, Serializable id) throws DataAccessException { return getHibernateTemplate().load(entityName, id); } public void load(Object entity, Serializable id) throws DataAccessException { getHibernateTemplate().load(entity, id); } public Object load(Class entityClass, Serializable id, LockMode lockMode) throws DataAccessException { return getHibernateTemplate().load(entityClass, id, lockMode); } public Object load(String entityName, Serializable id, LockMode lockMode) throws DataAccessException { return getHibernateTemplate().load(entityName, id, lockMode); } public List loadAll(Class entityClass) throws DataAccessException { return getHibernateTemplate().loadAll(entityClass); } public void lock(Object entity, LockMode lockMode) throws DataAccessException { getHibernateTemplate().lock(entity, lockMode); } public void lock(String entityName, Object entity, LockMode lockMode) throws DataAccessException { getHibernateTemplate().lock(entityName, entity, lockMode); } public Object merge(Object entity) throws DataAccessException { return getHibernateTemplate().merge(entity); } public Object merge(String entityName, Object entity) throws DataAccessException { return getHibernateTemplate().merge(entityName, entity); } public void persist(Object entity) throws DataAccessException { getHibernateTemplate().persist(entity); } public void persist(String entityName, Object entity) throws DataAccessException { getHibernateTemplate().persist(entityName, entity); } public void refresh(Object entity) throws DataAccessException { getHibernateTemplate().refresh(entity); } public void refresh(Object entity, LockMode lockMode) throws DataAccessException { getHibernateTemplate().refresh(entity, lockMode); } public void replicate(Object entity, ReplicationMode replicationMode) throws DataAccessException { getHibernateTemplate().replicate(entity, replicationMode); } public void replicate(String entityName, Object entity, ReplicationMode replicationMode) throws DataAccessException { getHibernateTemplate().replicate(entityName, entity, replicationMode); } public Serializable save(Object entity) throws DataAccessException { return getHibernateTemplate().save(entity); } public Serializable save(String entityName, Object entity) throws DataAccessException { return getHibernateTemplate().save(entityName, entity); } public void saveOrUpdate(Object entity) throws DataAccessException { getHibernateTemplate().saveOrUpdate(entity); } public void saveOrUpdate(String entityName, Object entity) throws DataAccessException { getHibernateTemplate().saveOrUpdate(entityName, entity); } public void saveOrUpdateAll(Collection entities) throws DataAccessException { getHibernateTemplate().saveOrUpdateAll(entities); } public void update(Object entity) throws DataAccessException { getHibernateTemplate().update(entity); } public void update(Object entity, LockMode lockMode) throws DataAccessException { getHibernateTemplate().update(entity, lockMode); } public void update(String entityName, Object entity) throws DataAccessException { getHibernateTemplate().update(entityName, entity); } public void update(String entityName, Object entity, LockMode lockMode) throws DataAccessException { getHibernateTemplate().update(entityName, entity, lockMode); } public List findByExample(String entityName, Object exampleEntity) throws DataAccessException { return getHibernateTemplate().findByExample(entityName, exampleEntity); } public List findByExample(String entityName, Object exampleEntity, int firstResult, int maxResults) throws DataAccessException { return getHibernateTemplate().findByExample(entityName, exampleEntity, firstResult, maxResults); } public BigDecimal getSequence(String sequenceName) throws DataAccessException { Session session = this.getSession(); Query query = null; BigDecimal result = null; try { query = session.createSQLQuery("select " + sequenceName + ".nextval from dual"); result = (BigDecimal) query.list().get(0); } catch (HibernateException e) { throw e; } finally{ releaseSession(session); } return result; } public void callProcedure(String procedureDefinition, List setParamList, List outParamList) throws DataAccessException { Connection conn = this.getSession().connection(); CallableStatement callableStatement = null; try{ callableStatement = conn.prepareCall(procedureDefinition); if (setParamList != null){ for (int i = 0; i < setParamList.size(); i++){ if (setParamList.get(i) instanceof Long){ callableStatement.setLong(i + 1, (Long) setParamList.get(i)); }else if (setParamList.get(i) instanceof String){ callableStatement.setString(i + 1,(String) setParamList.get(i)); } } } if (outParamList != null){ for (int i = 0; i < outParamList.size(); i++){ if (outParamList.get(i) instanceof Long){ callableStatement.registerOutParameter(setParamList.size() + i + 1, Types.INTEGER); }else if (outParamList.get(i) instanceof String){ callableStatement.registerOutParameter(setParamList.size()+ i + 1, Types.VARCHAR); } } } callableStatement.executeUpdate(); if (outParamList != null){ for (int i = 0; i < outParamList.size(); i++){ if (outParamList.get(i) instanceof Long){ outParamList.set(i, callableStatement.getLong(setParamList.size() + i + 1)); } else if (outParamList.get(i) instanceof String){ outParamList.set(i, callableStatement.getString(setParamList.size() + i + 1)); } } } } catch (SQLException e) { SQLExceptionTranslator translator = getHibernateTemplate().getJdbcExceptionTranslator(); throw translator.translate("", null, e); }finally{ if(callableStatement != null){ try { callableStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public int getrowsByCriteria(final DetachedCriteria detachedCriteria) { return -1; } /** * 通用的调用原生SQL * * @param sql sql语句 * @param obj 所对应的参数 * @return List */ public int countNativeSQL(final String sql, final Object[] obj) throws Exception{ //// System.out.println("sql:"+sql); try{ return (Integer) getHibernateTemplate().executeWithNativeSession( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { PreparedStatement st = null; ResultSet rs = null; int result = 0; try { st = session.connection().prepareStatement(sql); if (obj != null) { for (int i = 0; i < obj.length; i++) { //// System.out.println("obj[" + i + "]=" + obj[i]); st.setObject(i + 1, obj[i]); } } rs = st.executeQuery(); while (rs.next()) { result = rs.getInt(1); } } catch (HibernateException ex) { throw ex; } catch (Exception e) { throw new HibernateException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } return result; } }); } catch (Exception ex) { throw ex; } } /** * 通用的调用原生SQL * * @param sql sql语句 * @param obj 所对应的参数 * @return List */ public boolean execNativeSQL(final String sql, final Object[] obj) throws Exception{ //// System.out.println("sql:"+sql); try{ return (boolean)getHibernateTemplate().executeWithNativeSession( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { PreparedStatement st = null; ResultSet rs = null; boolean result = false; try { st = session.connection().prepareStatement(sql); if (obj != null) { for (int i = 0; i < obj.length; i++) { //// System.out.println("obj[" + i + "]=" + obj[i]); st.setObject(i + 1, obj[i]); } } result = st.execute(); } catch (HibernateException ex) { throw ex; } catch (Exception e) { throw new HibernateException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } return result; } }); } catch (Exception ex) { throw ex; } } /** * 通用的调用原生SQL * * @param sql sql语句 * @param obj 所对应的参数 * @return List */ public List<Map<String, Object>> findNativeSQL(final String sql, final Object[] obj) throws Exception { //// System.out.println("sql:"+sql); try{ return (List) getHibernateTemplate().executeWithNativeSession( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { List resultList = new java.util.ArrayList(); PreparedStatement st=null; ResultSet rs=null; try { st = session.connection() .prepareStatement(sql); if (obj != null) { for (int i = 0; i < obj.length; i++) { //// System.out.println("obj["+i+"]="+obj[i]); st.setObject(i + 1, obj[i]); } } rs = st.executeQuery(); ResultSetMetaData rsm = rs.getMetaData(); if(rs!=null){ while (rs.next()) { Map map = new HashMap(); for (int col = 0; col < rsm.getColumnCount(); col++) { map.put(rsm.getColumnLabel(col + 1) .toLowerCase(), rs .getObject(col + 1)); } resultList.add(map); } } } catch (HibernateException ex) { throw ex; }catch(Exception e){ throw new HibernateException(e); }finally{ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } return resultList; } }); }catch(Exception ex){ throw ex; } } public void delete(String entityName, Object entity) throws DataAccessException { getHibernateTemplate().delete(entityName, entity); } public void delete(String entityName, Object entity, LockMode lockMode) throws DataAccessException { getHibernateTemplate().delete(entityName, entity, lockMode); } public List<Object[]> findBySqlQuery(final String sql,final Object...paras) throws Exception{ List<Object[]> result = null; Session session = this.getSession(); Query query = session.createSQLQuery(sql); if (paras != null && paras.length > 0) { for (int index = 0; index < paras.length; index++) { query.setParameter(index, paras[index]); } } result = query.list(); releaseSession(session); return result; } public Object uniqueResult(final String hql,final Object... paras) { return getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query query = session.createQuery(hql); query.setMaxResults(1); for (int index = 0; index < paras.length; index++) { query.setParameter(index, paras[index]); } return query.uniqueResult(); } }); } public Object uniqueResultByNativeSql(final String sql,final Object... paras) { return getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query query = session.createSQLQuery(sql); query.setMaxResults(1); for (int index = 0; index < paras.length; index++) { query.setParameter(index, paras[index]); } return query.uniqueResult(); } }); } /** * 根据原生sql查询dojo的gridx分页 * * @param sql 查询语句 第一个字段为rownumber_,后面为顺序传入的sql中的字段顺序 * @param paras 参数 * @return page data为object数组 * @throws Exception */ @SuppressWarnings("rawtypes") public List<Map<String, Object>> findGridByNativeSql(String sql,Object[] paras,int page,int rows) throws Exception{ List<Map<String, Object>> list=null; Session session = this.getSession(); /*DB2 写法 * String result = sql.replaceFirst("select", "select * from ( select rownumber() over() as rownumber_, ") + ") as temp_ where rownumber_ between "+(page+1)+" and "+(page+rows)+" "; * */ /*oracle 写法*/ String result = "select * from ( select rownum as rownumber_,tmp_.* from ( "+sql+ ") tmp_ ) temp_ where rownumber_ between "+((page-1)*rows+1)+" and "+(page*rows)+" "; //mysql写法sql //String result = "SELECT * FROM ("+sql+" ) TABLE_PAGE LIMIT "+page+","+rows; //// System.out.println("SQL:"+result); return this.findNativeSQL(result, paras); /*// 查询 List listQuery = query.list(); Iterator it= listQuery.iterator(); while(it.hasNext()){ Object[] obj=(Object[]) it.next(); int i=0; i=obj.length; HashMap map=new HashMap(); for (int j=0;j<i;j++) { map.put(fieldList.get(j), obj[j+1]); } list.add(map); } return list;*/ } }
2.业务Dao
import java.util.List; import org.springframework.stereotype.Repository; @Repository public class LoginDaoImpl extends CommonDaoImpl implements LoginDao{ @Override public List<?> verify(String username, String password) throws Exception { Object[] values = new Object[] {username.trim(), password.trim()}; return this.findNativeSQL(" select t1.*,t2.deptname from Tuscuser t1 left join tuscdept t2 on t1.deptid = t2.deptid where t1.userid=? and t1.userpassword=? and t1.status='1' ", values); //return commonDao.find("from Tuscuser t1 where t1.userid=? and t1.userpassword=? and t1.status='1' ", values); } @Override public List<?> queryMyAuth(String userid) throws Exception { Object[] values = new Object[] {userid}; return this.findNativeSQL(" select t1.autcode from tuscroleaut t1 join tuscroleuser t2 on t1.roleid = t2.roleid and t2.userid=? group by t1.autcode ", values); } @Override public List<?> queryMyMenu(String userid) throws Exception { Object[] values = new Object[] {userid}; return this.findNativeSQL(" select t1.menuid,t1.parentmenuid,t1.menuname,t1.menucode,t1.menutype,t1.urlcode,t1.iconclass from tcfgmenu t1 where t1.menuid in (select t2.menuid from tuscrolemenu t2,tuscroleuser t3 where t2.roleid = t3.roleid and t3.userid=? group by t2.menuid) order by t1.menucode ", values); } @Override public List<?> queryMenuTitle() throws Exception { return this.find("from Tcfgmenu t1 where t1.menutype='0' order by t1.menucode"); } @Override public List<?> queryMyDataAuth(String userid) throws Exception { Object[] values = new Object[] {userid}; return this.findNativeSQL(" select t1.datacode from tuscroledata t1 where t1.roleid in (select roleid from tuscroleuser where userid=? ) group by t1.datacode ", values); } }
发表评论
文章已被作者锁定,不允许评论。
-
Spring BeanFactoryPostProcessor和BeanPostProcessor的区别
2018-11-14 15:40 707链接:https://blog.csdn.net/caihai ... -
spring BeanPostProcessor理解
2018-11-14 11:31 323链接:https://blog.csdn.net/ginkgo ... -
Spring 源码解析之Initializer
2018-11-14 11:27 462链接:https://blog.csdn.net/ktlife ... -
spring transaction同一个类不回滚解决方法
2018-10-11 10:59 7751.修改配置文件 <aop:aspectj-autopr ... -
Spring @Transaction学习
2018-10-08 10:36 2951.考虑有下面这么一个类 public class Foo ... -
spring mvc i18n国际化学习(spring:message)
2018-06-09 09:35 647spring.xml文件中配置: <!-- 存储区域 ... -
Spring Boot Oauth2.0授权服务器
2018-05-11 14:19 1657什么是OAuth? OAuth(Open Authoriza ... -
Spring Boot @Import注解(将指定类实例注入到IOC容器中)
2018-05-09 10:20 1603SpringBoot 的 @Import 用于将指定的类实例注 ... -
Spring Boot @Conditional注解
2018-05-09 10:15 1820Spring Boot的强大之处在于使用了Spring 4框架 ... -
Spring Boot自定义starter pom实例(/META-INFO/spring.factory文件)
2018-05-09 09:48 1148自定义starter pom 自己实现一个简单的例子,当某个类 ... -
Spring Boot自动配置原理(@Conditional @Import)
2018-04-26 14:45 1344Springboot的自动配置是SpringBoot的关键,主 ... -
Spring Boot优缺点总结
2018-04-16 10:25 1540优点: 1.去除了大量的xml配置文件 2.简化 ... -
SpringBoot JPA @Transaction 知识学习
2018-03-16 09:09 765一、事务相关概念 1、事务的特点 原子性:事务是一个原子操 ... -
Sprint @Query注解的用法(nativeQuery=true/false)(Spring Data JPA)
2018-03-15 16:33 38721. 一个使用@Query注解的简单例子 @Query(val ... -
Spring Boot JpaRepository知识学习(Spring Data JPA)
2018-03-14 11:17 18111.Spring Data所解决的问题 Spring Dat ... -
SpringCloud Hystrix知识学习(防止雪崩效应)
2018-03-13 14:57 933一、Hystrix说明 1.服务雪崩效应:是一种因服务提供者的 ... -
SpringCloud @LoadBalanced注解学习
2018-03-13 09:48 2226当时我们说开启负载均衡很简单,只需要在RestTemplate ... -
Spring Boot配置方式(java配置和注解配置)
2018-03-12 15:09 1113Java配置 从Spring 3.x开始,Spring提供了J ... -
java RestTemplate访问restful服务
2018-03-01 15:02 1625REST的基础知识 当谈论REST时,有一种常见的错误就是将其 ... -
SpringCloud | 第七篇: 高可用的服务注册中心
2018-02-26 14:31 489文章 第一篇: 服务的注册与发现(Eureka) 介绍了服务注 ...
相关推荐
NULL 博文链接:https://wxinpeng.iteye.com/blog/1162157
根据给定的信息,我们可以深入探讨Spring框架中与Hibernate集成的相关知识点,特别关注“HibernateDaoSupport”类及其在Spring环境中的应用。以下是对标题、描述以及部分文件内容的详细解析: ### 一、Spring与...
总结,这个示例展示了如何在Spring Boot 2.0中配置多数据源,使用Hibernate作为ORM工具,以及如何在DAO层通过`HibernateDaoSupport`进行数据操作,并在业务逻辑中灵活地切换数据源。这有助于实现更复杂的数据库访问...
SSH整合(其中dao用extends HibernateDaoSupport方式)总结【图解】
- **MyBatis**:Spring整合MyBatis时,可以通过`SqlSessionFactoryBean`配置SqlSessionFactory,并使用`SqlSessionTemplate`或`MyBatisSqlSessionTemplate`进行数据库操作。 3. **iBATIS(现在称为MyBatis)DAO...
5. **实现DAO**:继承`HibernateDaoSupport`,注入`HibernateTemplate`,实现DAO接口的方法。 6. **事务管理**:在Service层,使用Spring的`@Transactional`注解进行事务边界控制。 7. **测试**:编写测试用例,...
在Spring整合Hibernate的场景中,IoC容器可以管理SessionFactory、DAO对象以及其他的业务服务,实现依赖注入,使组件之间的关系更加清晰,便于解耦和测试。 6.5.7 启动web容器读取XML配置文件 在Web应用中,Spring...
- **数据访问对象(DAO)**:Spring管理DAO,提供事务边界,使DAO层与业务逻辑分离。 - **模型-视图-控制器(MVC)设计模式**:Struts2负责控制器,Spring处理业务逻辑,Hibernate处理数据持久化,三者共同实现MVC...
整合Spring和Hibernate时,通常会通过Spring的DataSource、SessionFactory和HibernateTemplate或HibernateDaoSupport来管理数据库连接和事务。Spring通过AOP在数据库操作周围创建事务边界,保证了数据的一致性。同时...
3. **DAO层**:包含使用Hibernate操作数据库的类,可能继承自HibernateDaoSupport。 4. **Service层**:业务逻辑处理层,调用DAO层实现功能。 5. **控制器层**:如Servlet或Controller,处理HTTP请求,调用Service层...
【Spring+Struts+Hibernate整合配置方案】 Spring、Struts和Hibernate是Java Web开发中三大核心框架,它们各自负责不同的职责:Spring作为依赖注入容器和面向切面编程(AOP)框架,提供应用的结构和事务管理;Struts...
4. **HibernateTemplate与HibernateDaoSupport**:Spring提供了HibernateTemplate和HibernateDaoSupport,它们为DAO层提供了便捷的操作方法,如save、update、delete、find等,同时处理了事务和异常。 5. **JPA集成...
我们可以创建一个继承自HibernateDaoSupport的抽象基类,将SessionFactory注入,然后在此基础上实现具体的DAO方法。 5. **Service层设计**:在Service层,我们可以使用Spring的@Autowired注解来注入DAO,然后调用...
下面我们将深入探讨如何整合这两个框架,并展示如何在 Spring 容器中配置 SessionFactory 以及基于 Hibernate 原生 API 实现 DAO。 1. Spring 中配置 SessionFactory 在 Spring 容器中创建 SessionFactory 是整合...
2. **实现 DAO 接口**:创建具体的 DAO 实现类,如 UserDao,继承 Spring 的 HibernateDaoSupport 类来简化 Hibernate 的操作。 ```java public class UserDao extends HibernateDaoSupport implements IDao { ...
1. 创建DAO子类:首先,我们需要创建一个继承自`HibernateDaoSupport`的DAO类,例如`UserDaoImpl`。 ```java public class UserDaoImpl extends HibernateDaoSupport implements UserDao { // ... } ``` 2. 配置...
4. **Spring的HibernateTemplate或HibernateDaoSupport**:Spring提供了HibernateTemplate和HibernateDaoSupport类,用于简化Hibernate操作。在这个Demo中,可能会使用它们来封装数据访问操作。 5. **Entity(实体...
数据库访问层:将对持久层数据库的基本添加,修改,查找等操作提取到BaseDAO中,BaseDAO继承Spring的HibernateDaoSupport,各模块DAO继承BaseDAO实现复杂数据访问。由IOC容器DAO的代理实例注入到业务组件中,业务组件...
例如,我们可以自定义一个DAO类,继承`HibernateDaoSupport`,然后利用`@Autowired`来注入Service层的bean,实现业务逻辑和数据访问的解耦: ```java @Repository public class CustomUserDao extends ...