Spring整合Hibernate:1、annotation方式管理SessionFactory
来源:互联网 发布:sql注入绕过 concat 0x 编辑:程序博客网 时间:2024/06/03 13:20
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属性即可
修改后的配置如下:以后的实体都放到相应的包里面即可
1
2
3
4
5
<
property
name
=
"packagesToScan"
>
<
list
>
<
value
>com.fz.annotation.model</
value
>
</
list
>
</
property
>
2、导入hibernate3的jar包
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;
}
}
来自为知笔记(Wiz)
0 0
- Spring整合Hibernate:1、annotation方式管理SessionFactory
- Spring3与Hibernate4整合,Spring中配置Hibernate基于XML和annotation的sessionFactory方式。
- spring+hibernate Annotation方式整合
- spring整合Hibernate使用set方式注入sessionFactory问题
- Hibernate将SessionFactory交给Spring来管理/Spring管理Hibernate事务的方式
- springmvc+spring+hibernate 整合 annotation(全注解)方式
- spring hibernate整合时SessionFactory为空
- spring + hibernate + annotation 简单整合
- spring整合hibernate中sessionFactory的两种常用引入方式
- Hibernate集成Spring SessionFactory生成方式
- spring 整合hibernate整合方式
- Spring整合Hibernate管理事务(xml配置方式)
- spring整合hibernate关于 session的管理<三种方式>
- struts2+hibernate+spring整合(annotation版)
- SSH整合中 hibernate托管给Spring得到SessionFactory
- SSH整合中 hibernate托管给Spring得到SessionFactory
- spring与hibernate整合applicatContext.xml配置,得到sessionFactory
- Spring整合hibernate中Datasouce、SessionFactory、Transaction配置
- Annotation方式实现AOP
- AOP的Advice
- 翻硬币(搜索)
- XML方式实现Spring的AOP
- Spring简单与数据库连接
- Spring整合Hibernate:1、annotation方式管理SessionFactory
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
- Spring整合hibernate:3、使用XML进行声明式的事务管理
- @Transactional的readOnly、timeout
- Java发送短信
- 使用为知笔记客户端发布博客到【博客园】
- httpClient 向微信服务器上传临时图片素材
- Vim常用命令
- 启动和停止Oracle服务bat脚本