Spring整合Hibernate:1、annotation方式管理SessionFactory

本文介绍如何在Spring环境中使用Hibernate的注解方式配置SessionFactory,并实现User实体类的持久化操作。文中详细展示了applicationContext.xml文件的配置方法,包括数据库连接池、SessionFactory配置及实体类映射等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、在applicationContext.xml文件中初始化SessionFactory(annotation方式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
< 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 >
<!-- annotation方式管理hibernate的sessionFactory -->
< bean id = "mySessionFactory"
     class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
     < property name = "dataSource" ref = "dataSource" />
     < property name = "annotatedClasses" >
         < list >
             < value >com.fz.annotation.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 >
             < prop key = "hibernate.format_sql" >true</ prop >
         </ props >
     </ property >
</ bean >

一个数据库连接,只需要一个SessionFactory即可。通过sessionFactory来获取session,所以让Spring来管理hibernate的SessionFactory比较合适

因为,Spring管理的bean默认就是单例模式。

在使用了annotation方式之后就不需要配置User.hbm.xml文件了,例如,在<property>标签的list中定义的是对象,而不是User.hbm.xml

1.1 使用PackagesToScan来扫描包

查看annotatedClasses这个属性,每一个实体类都要写在这里,比较麻烦。

< property  name = "annotatedClasses" >
         < list >
             < value >com.fz.annotation.model.User</ value >
         </ list >

</property>

所以,可以使用AnnotationSessionFactoryBean的一个packagesToScan属性即可

270945554536423.png

修改后的配置如下:以后的实体都放到相应的包里面即可

1
2
3
4
5
< property name = "packagesToScan" >
     < list >
         < value >com.fz.annotation.model</ value >
     </ list >
</ property >


2、导入hibernate3的jar包

270945565158081.png

3、编写User实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.fz.annotation.model;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
@Entity
public class User {
     private int id;
     private String username;
     @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;
     }
     
}

该实体类属性需要和数据库中字段名对应,不能多

4、使用sessionFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.fz.annotation.dao.impl;
 
import javax.annotation.Resource;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
 
import com.fz.annotation.dao.UserDao;
import com.fz.annotation.model.User;
 
 
@Repository ( "userDao" )
public class UserDaoImpl implements UserDao{
     private SessionFactory sessionFactory;
     public void userAdd(User user) {
         Session session = sessionFactory.openSession();
         session.beginTransaction();
         session.save(user);
         session.getTransaction().commit();
         session.close();
     }
     public SessionFactory getSessionFactory() {
         return sessionFactory;
     }
     @Resource
     public void setSessionFactory(SessionFactory sessionFactory) {
         this .sessionFactory = sessionFactory;
     }
}








转载于:https://www.cnblogs.com/meet/p/4762381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值