????4.??????????(Session Factory)
???????Hibernate??????????org.hibernate.Session??Session?????????????????????籣?楨??????????????????????????????
?????????????Hibernate??SessionFactory?????Session????SessionFactory???????Hibernate Session????????????????
??????????xml???????Bean???????£?
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<!--?????????????? -->
<list>
<value>com.blog.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql"> true</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
</bean>
????5.????????????DAO??
????Spring???????Spring??Hibernate??????????????????????????????Hibernate?????????????й?????????????????????????????????Hibernate Session????????DAO???С?
/**
* ????dao
*
* @author ckz
*
* @param <T>
*/
public abstract class BaseDAO<T> {
private Class<T> baseEntity;
protected BaseDao(Class<T> baseEntity) {
this.baseEntity = baseEntity;
}
/**
* ???sessionFactory
*/
@Autowired
private SessionFactory sessionFactory;
/**
* ???session
*
* @return
*/
protected Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
/**
* ????
*
* @param entity
* @throws Exception
*/
public void add(T entity) throws Exception {
getCurrentSession().save(entity);
}
/**
* ????洢????
*
* @param proName
* @return
*/
public CallableStatement citePro(final String proName){
Session session = getCurrentSession();
CallableStatement proc=session.doReturningWork(
new ReturningWork<CallableStatement>() {
@Override
public CallableStatement execute(Connection connection) throws SQLException{
CallableStatement resultSet = connection.prepareCall(proName);
return resultSet;
}
}
);
return proc;
}
/**
* ????
*
* @param entity
* @throws Exception
*/
public void update(T entity) throws Exception {
getCurrentSession().update(entity);
}
/**
* ????????
*
* @param entity
* @throws Exception
*/
public void saveOrUpdate(T entity) throws Exception {
getCurrentSession().saveOrUpdate(entity);
}
/**
* ???
*
* @param entity
* @throws Exception
*/
public void delete(T entity) throws Exception {
getCurrentSession().delete(entity);
}
/**
* ????id???????
*
* @param Id
* @return
*/
@SuppressWarnings("unchecked")
public T getById(final Serializable Id) {
return (T) getCurrentSession().get(this.baseEntity?? Id);
}
}
???????
????1.T??????????????????
????2.T - ??? Class ???????????????
???????磬String.class ???????? Class<String>??
???????軔?????????δ???????? Class<?>;