?? usermanagerimpl3.java
字號(hào):
package com.oneedu.busi;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.oneedu.entity.User;
/**
* 聲明式事務(wù)
*
* 不繼承HibernateDaoSupport類,
* 本類,提供setSessionFactory方法
*
* DAO訪問方式的主要優(yōu)勢在于它僅僅依賴于Hibernate API本身
* 而無需引入任何Spring的類。 從無入侵性的角度來看,這一點(diǎn)非常吸引人。
* 同時(shí),對(duì)于Hibernate開發(fā)人員來說也更自然。
*
*
* @author Administrator
*
*/
public class UserManagerImpl3 implements UserManager {
//讓IOC容器,注入sessionFactory
SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(String name) {
//不繼承 HibernateDaoSupport 類
//使用IOC容器中,注入的sessionFactory,
SessionFactory sf = (SessionFactory)this.sessionFactory;
Session session = sf.getCurrentSession();
//如下面方式獲得Session,不生效,因?yàn)镮OC容器中的SessionFactory
//是被事務(wù)管理起來的,自己創(chuàng)建的,并沒有.
// Configuration cfg = new Configuration().configure();
// SessionFactory factory = cfg.buildSessionFactory();
// Session session = factory.getCurrentSession();
//這里創(chuàng)建一個(gè)BeanFactory,Client.java中,又創(chuàng)建一個(gè)BeanFactory,
//兩個(gè)不同的BeanFactory,對(duì)應(yīng)不同的SessionFactory,事務(wù),也不會(huì)起效
// BeanFactory factory
// = new ClassPathXmlApplicationContext("applicationContext*.xml");
// SessionFactory sf = (SessionFactory)factory.getBean("sessionFactory");
// Session session = sf.getCurrentSession();
//業(yè)務(wù)代碼.使用聲明式事務(wù)
User user = new User();
user.setName(name);
session.save(user);
}
public void find(String name){}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -