1.直接使用HibernateAPI
public class DaoImp implate Dao{
private SessionFactory sessionFactory;
private static String hql = "from User u where u.username=? ";
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory=sessionFactory;
}
public boolean isValidUser(String username) {
try{
List userList =
sessionFactory.getCurrentSession().creatQuery(hql).setParameter(0,username).list();
if (userList.size() > 0) {
return true;
} catch (HibernateException ex){
throw converHibernaterAccessException(ex);
}
}
}
}
优点:与Spring框架完全分离
缺点:(1)无法使用Spring框架封装所提供的额外功能.如,直接使用Hibernate API 需用try...catch()处理HibernateException异常.
(2)需在实现类中加入setSessionFactory(SessionFactory sessionFactory)属性,接收依赖注入的SessionFactory.
2.继承 Spring 的 HibernateDaoSupport 使用 HibernateTemplate (不推荐使用getSession())
public class DaoImp extend HibernateDaoSupport implates Dao{
private static String hql = "from User u where u.username=? ";
public boolean isValidUser(String username) {
List userList = getHibernateTemplate().find(hql,username);
if (userList.size() > 0) {
return true;
}
public boolean isValidUser(String username,String password) throw DataAccessException {
Session session = getSession(); //不推荐使用,用完后需手动关闭
String[] userlist=new String[2];
userlist[0]=username;
userlist[1]=password;
try{
List userList = session.find(hql,userlist); //Hibernate语句;
session.close();
if (userList.size() > 0) {
return true;
} catch (HibernateException ex){
throw converHibernaterAccessException(ex);
}
}
}
}
特点:对HibernateTemplate没有提供的功能,可以直接调用HibernateDaoSuppor对象的getSession()方法(极其不推荐使用)得到Session对象实例用try{ Hibernate API }catch (HibernateException ex )操作
3.对 HibernateTemplate 没有提供的功能, 还可以用HibernateCallback 回调的方法管理数据库. (极其推荐)
/**
* 使用 hql 语句进行操作
* @param hql HSQL 查询语句 * @param offset 开始取数据的下标 * @param length 读取数据记录数 * @return List 结果集 */
public List getListForPage ( final String hql , final int offset , final int length ) {
List list = getHibernateTemplate().executeFind ( new HibernateCallback ( ) {
public Object doInHibernate ( Session session ) throws HibernateException, SQLException {
Query query = session.createQuery ( hql )
query.setFirstResult ( offset )
query.setMaxResults ( length )
query.setCacheable(false);
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
List list = query.list ( )
return list
}
})
return list
}
4.注入jdbcTemplate
先配置好jdbcTemplate
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property>
<bean id="classDao" class="cn.jmu.data.dao.impl.ClassImpl">
<property name="jdbctemplate">
<ref bean="jdbcTemplate" />
</property> </bean>
</bean><bean id="classDao" class="cn.jmu.data.dao.impl.ClassImpl"> <property name="jdbctemplate"> <ref bean="jdbcTemplate" /> </property> </bean>
jdbcTemplate使用String SQL= "select name from table"; List list= jdbctemplate.queryForList(SQL); Hashtable hash = new Hashtable(); jdbctemplate.query(SQL, new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { hash.put(rs.getString(1),rs.getString(2)); } });