使用註解方式進行spring和hibernate整合
阿新 • • 發佈:2019-02-20
整合spring和hibernate需要五個要素,分別包括持久化的類, 資料來源,SessionFactory, TransactionManager和持久化操作的DAO類。
持久化類:
- @Entity
- publicclass Spitter {
- privatelong id;
- private String userName, passWord, fullName;
- public Spitter(long id, String n, String p, String f){
- this.id = id;
- this.userName = n;
- this.passWord = p;
- this.fullName = f;
- }
- public Spitter(){}
- publicvoid setId(long id){
- this.id = id;
- }
- @Id
- publiclong getId(){
- return id;
- }
- public String getUserName(){
- returnthis.userName;
- }
- publicvoid setUserName(String n){
- this.userName = n;
- }
- public String getPassWord(){
- returnthis.passWord;
- }
- publicvoid setPassWord(String p){
- this.passWord = p;
- }
- public String getFullName(){
- returnthis.fullName;
- }
- publicvoid setFullName(String f){
- this
- }
- }
- <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
- <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
- <propertyname="url"value="jdbc:mysql://localhost:3306/spitter"/>
- <propertyname="username"value="root"/>
- <propertyname="password"value="root"/>
- <propertyname="initialSize"value="5"/>
- <propertyname="maxActive"value="10"/>
- </bean>
SessionFactory類(在spring配置檔案中配置):
- <beanid="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <propertyname="dataSource"ref="dataSource"/>
- <propertyname="annotatedClasses">
- <list>
- <value>Spitter.spitterOne.Spitter</value>
- </list>
- </property>
- <propertyname="hibernateProperties">
- <props>
- <propkey="dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
- <propkey="hibernate.show_sql">true</prop>
- <propkey="hibernate.format_sql">true</prop>
- <propkey="hibernate.hbm2ddl.auto">update</prop>
- </props>
- </property>
- </bean>
- <!-- 設定transactionManager -->
- <beanid="txManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <propertyname="sessionFactory"ref="sessionFactory"/>
- </bean>
- <!--啟動spring事務註解功能-->
- <tx:annotation-driventransaction-manager="txManager"/>
進行持久化操作的DAO類:
- @Repository
- publicclass HibernateSpitterDao implements SpitterDAO {
- private SessionFactory sessionFactory;
- @Autowired
- public HibernateSpitterDao(SessionFactory sessionFactory){
- this.sessionFactory = sessionFactory;
- }
- private Session currentSession(){
- returnthis.sessionFactory.getCurrentSession();
- }
- /**
- * 進行持久化的方法需要使用@Transactional進行事務管理
- */
- @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
- publicvoid addSpitter(Spitter spitter){
- this.currentSession().save(spitter);
- }
- public Spitter getSpitterById(long id){
- return (Spitter)this.currentSession().get(Spitter.class, id);
- }
- @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
- publicvoid saveSpitter(Spitter spitter){
- this.currentSession().update(spitter);
- }
- publicstaticvoid main(String [] args){
- Spitter ss = new Spitter(103,"zhang sfdasf454352an", "cccninini", "zhang shan fdasfsdfewe");
- ApplicationContext ctx = new ClassPathXmlApplicationContext("Spitter/spitterOne/spring-idol.xml");
- <span style="color:#ff0000;">SpitterDAO dao = (SpitterDAO) ctx.getBean("hibernateSpitterDao");</span>
- dao.addSpitter(ss);
- }
- }
因為Spring只能對介面進行aop操作,所以紅色程式碼部分只能將hibernateSpitterDao強制轉換成SpitterDAO介面。