目前我接触到的Spring的数据库操作主要包括两种,主要使用JdbcTemplate和HibernateTemplate。后者用于结合Hibernate操作。
两者都提供了持久层访问模板化,只需要获得一个SessionFactory就可以执行持久化操作。可以完成增删改查操作。
获取JdbcTemplate和HibernateTemplate的方式有两种
1. 传统的方式
2. 使用JdbcDaoSupport和HibernateDaoSupport获取,其包含两个方法。
(1) getHibernateTemplate/getJdbcTemplate (2) setSessionFactory
同样需要注入SesssionFactory
一般采用第二种方式
构建DAO层查询
BaseDao
1 package com.jason.centralbank.dao; 2 3 import java.util.List; 4 5 public interface BaseDao<T> { 6 7 List<T> findAll(Class<T> entityClazz); 8 }
BaseDaoHibernate
1 package com.jason.centralbank.dao; 2 3 import org.hibernate.SessionFactory; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.orm.hibernate4.support.HibernateDaoSupport; 6 7 import java.util.List; 8 9 public class BaseDaoHibernate<T> extends HibernateDaoSupport implements BaseDao<T> { 10 11 @Autowired 12 public void setSessionFactoryOverride(SessionFactory sessionFactory) 13 { 14 super.setSessionFactory(sessionFactory); 15 } 16 17 @SuppressWarnings("RedundantCast") 18 @Override 19 public List<T> findAll(Class<T> entityClazz) { 20 return (List<T>) getHibernateTemplate().loadAll(entityClazz); 21 } 22 }
DepositInforBaseDao
package com.jason.centralbank.dao; import com.jason.centralbank.entities.DepositInfor; public interface DepositInforBaseDao extends BaseDao<DepositInfor>{ }
DepositInforDao
1 package com.jason.centralbank.dao; 2 3 import com.jason.centralbank.entities.DepositInfor; 4 import org.springframework.stereotype.Repository; 5 6 @Repository 7 public class DepositInforDao extends BaseDaoHibernate<DepositInfor> implements DepositInforBaseDao{ 8 }
DepositInfor
package com.jason.centralbank.entities; import javax.persistence.*; @Entity @Table(name = "2011包头地区存款金额统计表") public class DepositInfor { @Id @Column(name = "单位") private String Institution; @Column(name = "一月") private String Jan; @Column(name = "二月") private String Feb; @Column(name = "三月") private String Mar; @Column(name = "四月") private String Apr; @Column(name = "五月") private String May; @Column(name = "六月") private String Jun; @Column(name = "上半年") private String FirstHalfYear; public String getInstitution() { return Institution; } public void setInstitution(String institution) { Institution = institution; } public String getJan() { return Jan; } public void setJan(String jan) { Jan = jan; } public String getFeb() { return Feb; } public void setFeb(String feb) { Feb = feb; } public String getMar() { return Mar; } public void setMar(String mar) { Mar = mar; } public String getApr() { return Apr; } public void setApr(String apr) { Apr = apr; } public String getMay() { return May; } public void setMay(String may) { May = may; } public String getJun() { return Jun; } public void setJun(String jun) { Jun = jun; } public String getFirstHalfYear() { return FirstHalfYear; } public void setFirstHalfYear(String firstHalfYear) { FirstHalfYear = firstHalfYear; } @Override public String toString() { return "DepositInfor{" + "Institution='" + Institution + '\'' + ", Jan='" + Jan + '\'' + ", Feb='" + Feb + '\'' + ", Mar='" + Mar + '\'' + ", Apr='" + Apr + '\'' + ", May='" + May + '\'' + ", Jun='" + Jun + '\'' + ", FirstHalfYear='" + FirstHalfYear + '\'' + '}'; } }