Spring整合Hibernate:1、annotation方式管理SessionFactory
2016-02-19 00:00
639 查看
1、在applicationContext.xml文件中初始化SessionFactory(annotation方式)
一个数据库连接,只需要一个SessionFactory即可。通过sessionFactory来获取session,所以让Spring来管理hibernate的SessionFactory比较合适
因为,Spring管理的bean默认就是单例模式。
在使用了annotation方式之后就不需要配置User.hbm.xml文件了,例如,在<property>标签的list中定义的是对象,而不是User.hbm.xml
1.1 使用PackagesToScan来扫描包
查看annotatedClasses这个属性,每一个实体类都要写在这里,比较麻烦。
修改后的配置如下:以后的实体都放到相应的包里面即可
2、导入hibernate3的jar包
3、编写User实体类
该实体类属性需要和数据库中字段名对应,不能多
4、使用sessionFactory
来自为知笔记(Wiz)
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 > |
因为,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 > |
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; } } |
相关文章推荐
- Java23种设计模式学习笔记【目录总贴】
- java websocket
- springmvc多文件上传
- 六种常用的设计模式java实现(六)策略模式
- Java设计模式之单例模式 double---checked----locking双重检查锁定
- 16-02-18 Java 方法应用 之 打印字母三角形
- 内省调用JavaBean实现getXxx和setXxx的两种方式
- 六种常用的设计模式java实现(五)代理模式
- 蓝桥杯 历届试题 错误票据 java详解
- [java]注解
- javaIO流
- 深入Java虚拟机(2)——Java的平台无关性
- 深入Java虚拟机(2)——Java的平台无关性
- 深入Java虚拟机(2)——Java的平台无关性
- 深入Java虚拟机(2)——Java的平台无关性
- java.lang.VerifyError
- jdk path
- restful规则脑图
- 【JAVA】14、一维数组的声明及分配内存
- [018]Java编程思想——聚合