日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring整合Hibernate图文步骤

發布時間:2025/6/15 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring整合Hibernate图文步骤 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工具:myeclipse9.0

Spring版本:2.5.6

Hibernate版本:3



昨天花了一下午時間把Spring和Hibernate整合到了一起,今天做一個筆記。 ?

首先建立java Project工程


點擊Finish完成


添加Hibernate和Spring所需要的jar包還有Mysql連接的jar包




創建Dao層,Dao層實現,Model層,Service層



DAO層代碼:IUserDao

[java] view plain copy
  • package?org.zhy.demo.dao;??
  • ??
  • import?java.sql.SQLException;??
  • ??
  • import?org.zhy.demo.dao.model.UserInfo;??
  • /**?
  • ?*?DAO層簡單的增刪改查方法?
  • ?*?@author?Administrator?
  • ?*?
  • ?*/??
  • public?interface?IUserDao?{??
  • ??
  • ????public?void?saveUser(UserInfo?user)?throws?SQLException;??
  • ??
  • ????public?void?delUser(UserInfo?user)?throws?SQLException;??
  • ??
  • ????public?void?editUsre(UserInfo?user)?throws?SQLException;??
  • ??
  • ????public?UserInfo?getUserById(int?id)?throws?SQLException;??
  • ??
  • }??


  • IUuserDao層實現(暫時空實現)


    [html] view plain copy
  • package?org.zhy.demo.dao.impl;??
  • ??
  • import?java.sql.SQLException;??
  • ??
  • import?org.zhy.demo.dao.IUserDao;??
  • import?org.zhy.demo.dao.model.UserInfo;??
  • ??
  • /**??
  • ?*?IUserDao實現,配置完Spring及Hibernate文件后實現此類??
  • ?*?@author?Administrator??
  • ?*??
  • ?*/??
  • public?class?IUserDaoImpl?implements?IUserDao?{??
  • ??
  • ????@Override??
  • ????public?void?saveUser(UserInfo?user)?throws?SQLException?{??
  • ??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?delUser(UserInfo?user)?throws?SQLException?{??
  • ??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?editUsre(UserInfo?user)?throws?SQLException?{??
  • ??
  • ????}??
  • ??
  • ????@Override??
  • ????public?UserInfo?getUserById(int?id)?throws?SQLException?{??
  • ????????return?null;??
  • ????}??
  • ??
  • }??


  • Model代碼

    [java] view plain copy
  • package?org.zhy.demo.dao.model;??
  • ??
  • import?javax.persistence.Entity;??
  • import?javax.persistence.GeneratedValue;??
  • import?javax.persistence.Id;??
  • ??
  • @Entity??
  • public?class?UserInfo?{??
  • ??
  • ??????
  • ????private?int?id;??
  • ????private?String?name;??
  • ????private?String?title;??
  • ??
  • ????@Id??
  • ????@GeneratedValue??
  • ????public?int?getId()?{??
  • ????????return?id;??
  • ????}??
  • ??
  • ????public?void?setId(int?id)?{??
  • ????????this.id?=?id;??
  • ????}??
  • ??
  • ????public?String?getName()?{??
  • ????????return?name;??
  • ????}??
  • ??
  • ????public?void?setName(String?name)?{??
  • ????????this.name?=?name;??
  • ????}??
  • ??
  • ????public?String?getTitle()?{??
  • ????????return?title;??
  • ????}??
  • ??
  • ????public?void?setTitle(String?title)?{??
  • ????????this.title?=?title;??
  • ????}??
  • ??
  • }??



  • 下面添加Spring配置文件

    在Src目錄下添加SpringContext.xml


    [html] view plain copy
  • <?xml?version="1.0"?encoding="UTF-8"?>??
  • <beans?xmlns="http://www.springframework.org/schema/beans"??
  • ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  • ????xmlns:aop="http://www.springframework.org/schema/aop"??
  • ????xmlns:tx="http://www.springframework.org/schema/tx"??
  • ????xsi:schemaLocation="http://www.springframework.org/schema/beans??
  • ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd??
  • ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd??
  • ???????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd??
  • ???????????">??
  • ????<aop:config></aop:config>??
  • ??
  • ????<bean?class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"?/>??
  • ??
  • ????<bean?id="datasource"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">??
  • ????????<property?name="driverClassName"?value="com.mysql.jdbc.Driver"?/>??
  • ????????<property?name="url"?value="jdbc:mysql://localhost/student"></property>??
  • ????????<property?name="username"?value="root"?/>???
  • ????????<property?name="password"?value="root"?/>???
  • ????????<!--?數據庫連接池保持的最小連接數?-->??
  • ????????<property?name="minIdle"?value="5"?/>??
  • ????????<!--?數據庫連接池保持的最大連接數?-->???
  • ????????<property?name="maxIdle"?value="30"?/>??
  • ????????<!--??
  • ????????????當數據庫連接因為某種原因斷掉之后,再重新從連接池中拿另外一個連接時實際上這個連接可能??
  • ????????????已經無效,所以為了確保所拿到的連接全都有效需要在獲取連接,返回連接以及連接空閑時進行??
  • ????????????有效性驗證?下面3個設置為ture時進行驗證,默認為false??
  • ?????????-->??
  • ????????<!--?取得連接時是否進行有效性驗證?-->??
  • ????????<property?name="testOnBorrow"?value="true"?/>??
  • ????????<!--?返回連接時是否進行有效性驗證?-->??
  • ????????<property?name="testOnReturn"?value="true"?/>??
  • ????????<!--?連接空閑時是否進行有效性驗證?-->??
  • ????????<property?name="testWhileIdle"?value="true"?/>??
  • ??????????
  • ????</bean>??
  • ??????
  • ????<bean?id="sessionFactory"?class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">??
  • ????????<property?name="dataSource"?ref="datasource"?/>??
  • ????????<!--?注意:我用的是Annotation的方式配置的Hibernate,這里的property的name是annotatedClasses?-->??
  • ????????<property?name="annotatedClasses">??
  • ????????????<list>??
  • ????????????????<value>org.zhy.demo.dao.model.UserInfo</value>??
  • ????????????</list>??
  • ????????</property>??
  • ????????<property?name="hibernateProperties">??
  • ????????????<props>??
  • ????????????????<!--?設置Hibernate方言?-->??
  • ????????????????<prop?key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>??
  • ????????????????<!--?是否打印sql?-->??
  • ????????????????<prop?key="hibernate.show_sql">true</prop>??
  • ????????????????<!--?格式化sql?-->??
  • ????????????????<prop?key="hibernate.format_sql">true</prop>??
  • ????????????????<!--?是否自動更新表?-->??
  • ????????????????<prop?key="hibernate.hbm2ddl.auto">update</prop>??
  • ????????????????<prop?key="hibernate.current_session_context_class">thread</prop>??
  • ????????????????<!--?最大抓取深度,如果為0,則關閉默認的外連接抓取。建議值為0-3?-->??
  • ????????????????<prop?key="hibernate.max_fetch_depth">1</prop>??
  • ????????????????<!--?用于生成有助于調試的注釋信息,默認為關閉?-->??
  • ????????????????<prop?key="hibernate.use_sql_comments">true</prop>??
  • ????????????</props>??
  • ????????</property>??
  • ????</bean>??
  • ??????
  • ????<bean?id="transactionManager"?class="org.springframework.orm.hibernate3.HibernateTransactionManager"?>??
  • ????????<property?name="sessionFactory"?ref="sessionFactory"></property>??
  • ????</bean>??
  • ??????
  • ????<aop:config>??
  • ????????<aop:pointcut?id="txMethod"?expression="execution(*?org.zhy.demo.dao.impl.*DaoImpl.*(..))"?/>??
  • ????????<aop:advisor?advice-ref="txAdvice"?pointcut-ref="txMethod"/>??
  • ????</aop:config>??
  • ????<!--?AOP切面聲明事務管理?-->??
  • ????<tx:advice?id="txAdvice"?transaction-manager="transactionManager">??
  • ????????<tx:attributes>??
  • ????????????<tx:method?name="save*"?propagation="REQUIRED"?/>?<!--?支持當前事務,如果執行到save開頭的任何方法時沒有事務則開啟一個事務?這是最常見的方式-->??
  • ????????????<tx:method?name="update*"?propagation="REQUIRED"?/><!--?支持當前事務,如果執行到save開頭的任何方法時沒有事務則開啟一個事務?這是最常見的方式-->??
  • ????????????<tx:method?name="add*"?propagation="REQUIRED"?/><!--?支持當前事務,如果執行到save開頭的任何方法時沒有事務則開啟一個事務?這是最常見的方式-->??
  • ????????????<tx:method?name="delete*"?propagation="REQUIRED"?/><!--?支持當前事務,如果執行到save開頭的任何方法時沒有事務則開啟一個事務?這是最常見的方式-->??
  • ????????????<tx:method?name="find*"?propagation="SUPPORTS"?read-only="true"/>?<!--?支持當前事務,如果當前沒有事務,就以非事務方式執行。只讀?-->??
  • ????????????<tx:method?name="get*"?propagation="SUPPORTS"?read-only="true"/><!--?支持當前事務,如果當前沒有事務,就以非事務方式執行。只讀?-->??
  • ????????????<tx:method?name="*"?/>??
  • ????????</tx:attributes>??
  • ????</tx:advice>??
  • ??????
  • ??????
  • ????<bean?name="userDao"?class="org.zhy.demo.dao.impl.IUserDaoImpl"?>??
  • ????????<property?name="sessionFactory"?ref="sessionFactory"></property>??
  • ????</bean>??
  • ????<bean?name="userService"?class="org.zhy.demo.service.UserService"?/>??
  • ??
  • </beans>??



  • 修改DAO實現類


    [java] view plain copy
  • package?org.zhy.demo.dao.impl;??
  • ??
  • import?java.sql.SQLException;??
  • ??
  • import?org.hibernate.SessionFactory;??
  • import?org.springframework.orm.hibernate3.HibernateTemplate;??
  • import?org.zhy.demo.dao.IUserDao;??
  • import?org.zhy.demo.dao.model.UserInfo;??
  • ??
  • /**?
  • ?*?IUserDao實現?
  • ?*??
  • ?*?@author?Administrator?
  • ?*??
  • ?*/??
  • public?class?IUserDaoImpl?implements?IUserDao?{??
  • ??
  • ????private?HibernateTemplate?hibernateTemplate;??
  • ??
  • ????public?void?setSessionFactory(SessionFactory?sessionFactory)?{??
  • ????????this.hibernateTemplate?=?new?HibernateTemplate(sessionFactory);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?saveUser(UserInfo?user)?throws?SQLException?{??
  • ????????hibernateTemplate.save(user);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?delUser(UserInfo?user)?throws?SQLException?{??
  • ????????hibernateTemplate.delete(user);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?editUsre(UserInfo?user)?throws?SQLException?{??
  • ????????hibernateTemplate.update(user);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?UserInfo?getUserById(int?id)?throws?SQLException?{??
  • ????????UserInfo?user?=?(UserInfo)?hibernateTemplate.get(UserInfo.class,?id);??
  • ????????return?user;??
  • ????}??
  • ??
  • }??


  • junit測試


    [java] view plain copy
  • @Test??
  • public?void?saveUserTest()?throws?SQLException{??
  • ????ApplicationContext?context=?new?ClassPathXmlApplicationContext("/SpringContext.xml");??
  • ????UserService?service?=(UserService)?context.getBean("userService");??
  • ??????
  • ????UserInfo?user?=?new?UserInfo();??
  • ????user.setName("T`");??
  • ????user.setTitle("CSDN?BLOG");??
  • ????service.addUserInfo(user);??
  • }?
  • 總結

    以上是生活随笔為你收集整理的Spring整合Hibernate图文步骤的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。