1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-3.0.xsd 14 "> 15 16 <!-- 配置基础数据源bean --> 17 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 18 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> 19 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> 20 <property name="username" value="system"></property> 21 <property name="password" value="orcl"></property> 22 </bean> 23 <!-- 配置Session会话工厂bean--> 24 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 25 <property name="dataSource" ref="dataSource"/> 26 <!--第一种方式:添加指定的对象关系映射文件--> 27 <!-- <property name="mappingResources"> 28 <list> 29 <value>com/myCinema/entity/Category.hbm.xml</value> 30 <value>com/myCinema/entity/Movie.hbm.xml</value> 31 <value>com/myCinema/entity/User.hbm.xml</value> 32 </list> 33 </property> --> 34 <!--第二种方式:添加对象关系映射文件的所在路径--> 35 <property name="mappingDirectoryLocations"> 36 <list> 37 <value>classpath:com/musicstore/entity/</value> 38 </list> 39 </property> 40 <property name="hibernateProperties"> 41 <props> 42 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 43 <prop key="hibernate.show_sql">true</prop> 44 </props> 45 </property> 46 </bean> 47 48 <!--配置日志切面--> 49 <bean id="logAdvice" class="com.musicstore.aop.logAdvice"></bean> 50 51 <bean id="iAlbum" class="com.musicstore.daoimpl.AlbumDaoImpl" > 52 <property name="sessionFactory" ref="sessionFactory"></property> 53 </bean> 54 <bean id="iGenre" class="com.musicstore.daoimpl.GenreDaoImpl"> 55 <property name="sessionFactory" ref="sessionFactory"></property> 56 </bean> 57 <bean id="iOrder" class="com.musicstore.daoimpl.OrderDaoImpl"> 58 <property name="sessionFactory" ref="sessionFactory"></property> 59 </bean> 60 <bean id="iOrderDetail" class="com.musicstore.daoimpl.OrderDetailDaoImpl"> 61 <property name="sessionFactory" ref="sessionFactory"></property> 62 </bean> 63 <bean id="iUser" class="com.musicstore.daoimpl.UserDaoImpl"> 64 <property name="sessionFactory" ref="sessionFactory"></property> 65 </bean> 66 <bean id="iUserRole" class="com.musicstore.daoimpl.UserRoleDaoImpl"> 67 <property name="sessionFactory" ref="sessionFactory"></property> 68 </bean> 69 </beans>
5.数据访问层的示例代码
1 package com.musicstore.daoimpl; 2 3 import java.sql.SQLException; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import org.hibernate.HibernateException; 10 import org.hibernate.Query; 11 import org.hibernate.Session; 12 import org.springframework.orm.hibernate3.HibernateCallback; 13 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 14 15 import com.musicstore.dao.IAlbum; 16 import com.musicstore.entity.Album; 17 18 public class AlbumDaoImpl extends HibernateDaoSupport implements IAlbum { 19 20 //获取全部信息 21 @Override 22 public List<Album> getAll(){ 23 String hql="from Album"; 24 return getHibernateTemplate().find(hql); 25 } 26 27 //使用持久化类的导航关系进行查询 28 @Override 29 public List<Album> findByGenreId(int gid) { 30 31 String hql="from Album as a where a.genre.id=?"; 32 Object[] params=new Object[]{gid}; 33 return getHibernateTemplate().find(hql,params); 34 35 36 } 37 38 //根据名称模糊查询,返回一个集合 39 @Override 40 public List<Album> findByTitle(String title) { 41 String hql=" from Album as a where a.title like ?"; 42 return getHibernateTemplate().find(hql, ("%"+title+"%")); 43 } 44 45 //查询单个对象 46 @Override 47 public Album getById(long id) { 48 String hql="from Album as a where a.id=? "; 49 List<Album> list=getHibernateTemplate().find(hql,id); 50 return list.size()>0?list.get(0):null; 51 } 52 53 //添加 54 @Override 55 public Album insert(Album album) throws Exception { 56 Album album2=null; 57 try { 58 getHibernateTemplate().save(album); 59 album2=album; 60 } catch (Exception e) { 61 return null; 62 } 63 return album2; 64 65 } 66 67 //修改 68 @Override 69 public Album update(Album album) throws Exception { 70 Album album2=null; 71 try { 72 getHibernateTemplate().update(album); 73 album2=album; 74 } catch (Exception e) { 75 return null; 76 } 77 return album2; 78 } 79 80 //根据名称模糊查询并进行分页 81 @Override 82 public List<Album> findByIdAndTitlePaging(final int gid, final String title, final int pageIndex, final int pageSize) { 83 return getHibernateTemplate().execute(new HibernateCallback<List<Album>>() { 84 @Override 85 public List<Album> doInHibernate(Session session) throws HibernateException, 86 SQLException { 87 String hql="from Album as a where 1=1 "; 88 Map<String,Object> params=new HashMap<String, Object>(); 89 List<Album> list=new ArrayList<Album>(); 90 if(gid>0){ 91 hql+=" and a.genre.id=:gid"; 92 params.put("gid",gid); 93 } 94 if(title!=null){ 95 hql+=" and a.title like :title "; 96 params.put("title","%" +title+"%"); 97 } 98 Query query=session.createQuery(hql); 99 query.setProperties(params); 100 query.setFirstResult((pageIndex-1)*pageSize); 101 query.setMaxResults(pageSize); 102 list=query.list(); 103 return list; 104 } 105 }); 106 } 107 108 109 110 }
6.使用HibernateTemplate
HibernateTemplate提供持久层访问模板,使用HibernateTemplate无须实现特定接口,它只需要提供一个SessionFactory的引用就可以执行持久化操作。SessionFactory 对象既可通过构造函数传入,也可以通过设值传入。
HibernateTemplate()
HibernateTemplate(org.hibernates.SessionFactory sessionFactory) (这个用得较多一点)
HibernateTemplate(org.hibernates.SessionFactory sessionFactory,boolean allowCreate)
常用方法:
void delete(Object entity):删除指定持久化实例
deleteAll(Collection entities):删除集合内全部持久化类实例
find(String queryString):根据HQL查询字符串来返回实例集合
findByNamedQuery(String queryName):根据命名查询返回实例集合
get(Class entityClass, Serializable id):根据主键加载特定持久化类的实例
save(Object entity):保存新的实例
saveOrUpdate(Object entity):根据实例状态,选择保存或者更新
update(Object entity):更新实例的状态,要求entity是持久状态
setMaxResults(int maxResults):设置分页的大小