Spring基本使用:整合hibernate
2014-05-28 15:51
507 查看
一、xml中配置数据源:
其中,jdbc.properties中取出具体的配置数据,dataSource成为了spring的一个bean。
二、配置Hibernate的SessionFactory:
<!--Hibernate的SessionFactory 的配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.ustcinfo.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>又配置了一个spring的bean,实现了原本的hibernate.cfg.xml文件中的配置。属性中包括:
1.注入了dataSource这个bean,完成数据源配置;
2.属性“annotatedClasses”展示了和数据库配对的User类,并且用list标签包括,说明可以有很多很多其他类;
3.属性“hibernateProperties”中的props配置数据连接的一些参数,如方言等。
三、对model类添加注解,(不用在model层中添加source.hbm.xml的配置文件)
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
private int id;
private String username;
private String password;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
四、将配置好的sessionFactory这个bean稳稳地注入所需要使用的实现类中。
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.stereotype.Component;
import com.ustcinfo.dao.UserDao;
import com.ustcinfo.model.User;
@Component
public class UserDaoImpl implements UserDao{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(User user){
Session s = sessionFactory.openSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
}
}五、开心的使用实现类中的方法:
<!-- 配置数据源dataSource --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties"/> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
其中,jdbc.properties中取出具体的配置数据,dataSource成为了spring的一个bean。
二、配置Hibernate的SessionFactory:
<!--Hibernate的SessionFactory 的配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.ustcinfo.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>又配置了一个spring的bean,实现了原本的hibernate.cfg.xml文件中的配置。属性中包括:
1.注入了dataSource这个bean,完成数据源配置;
2.属性“annotatedClasses”展示了和数据库配对的User类,并且用list标签包括,说明可以有很多很多其他类;
3.属性“hibernateProperties”中的props配置数据连接的一些参数,如方言等。
三、对model类添加注解,(不用在model层中添加source.hbm.xml的配置文件)
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
private int id;
private String username;
private String password;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
四、将配置好的sessionFactory这个bean稳稳地注入所需要使用的实现类中。
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.stereotype.Component;
import com.ustcinfo.dao.UserDao;
import com.ustcinfo.model.User;
@Component
public class UserDaoImpl implements UserDao{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(User user){
Session s = sessionFactory.openSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
}
}五、开心的使用实现类中的方法:
public static void main(String[] args){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService userService =(UserService) ctx.getBean("userServiceImpl"); User user = new User(); userService.mySave(user); }
相关文章推荐
- Spring4 整合 Hibernate3 基本使用(通过注入 SessionFactory)
- Spring+Hibernate:使用注释和OSCache整合的简单实例
- 使用MyEclipse集成SSH整合Spring和Hibernate
- Spring整合hibernate的事务属性介绍以及声明式事务管理 (使用Annotation和xml)
- Hibernate注解使用以及Spring整合
- 使用Myeclipse快速整合spring,struts,hibernate
- Hibernate注解使用以及Spring整合
- Hibernate注解使用以及Spring整合
- Tapestry+spring+hibernate整合使用时HQL语句问题!
- Hibernate注解使用以及Spring整合
- 使用MyEclipse集成SSH和DWR(二)整合Spring和Hibernate
- spring与hibernate整合-使用properties文件分离数据库连接设置
- spring整合hibernate使用getHibernateTemplate().save()方法不能输入数据库
- Hibernate数据的lazy问题,以及使用spring整合hibernate lazy的解决办法(好文转载)
- Hibernate+Spring整合使用二级缓存
- Hibernate整合进spring--使用hibernateTemplate.getSessionFactory().getCurrentSession()理解
- Spring整合Hibernate配置(使用hibernate.cfg.xml文件)
- Hibernate注解(Annotation)使用以及Spring整合
- 使用MyEclipse整合Struts+Spring+Hibernate时,在“applicationContext.xml”中提示class"org.apache.commons.dbcp.Basi
- Java程序员从笨鸟到菜鸟之(七十九)细谈Spring(八)spring+hibernate整合基本详解