隨筆 - 119  文章 - 3173  trackbacks - 0
          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          交友莫獨酒,茅臺西鳳游。
          口干古井貢,心徜洋河流。
          稱多情杜康,趟無量雙溝。
          贊中華巍巍,無此不銷愁。

          常用鏈接

          留言簿(68)

          隨筆分類(136)

          隨筆檔案(122)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 526679
          • 排名 - 92

          最新評論

          Derby+spring+hibernate和其它數據庫+spring+hibernate基本一樣。
          下面給出一個例子。
          applicationContext.xml
          大家重點看下這個配置文件的寫法。。。。。
          ?1?<?xml?version="1.0"?encoding="UTF-8"?>
          ?2?<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">
          ?3?
          ?4?<beans>
          ?5?
          ?6?
          ?7?????<bean?id="dataSource"
          ?8?????????class="org.apache.commons.dbcp.BasicDataSource">
          ?9?????????<property?name="driverClassName">
          10?????????????<value>org.apache.derby.jdbc.EmbeddedDriver</value>
          11?????????</property>
          12?????????<property?name="url">
          13?????????????<value>jdbc:derby:c:/TESTDB;create=true</value>
          14?????????</property>
          15?????????<property?name="username">
          16?????????????<value>test</value>
          17?????????</property>
          18?????????<property?name="password">
          19?????????????<value>test</value>
          20?????????</property>
          21?????</bean>
          22?????<bean?id="sessionFactory"
          23?????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          24?????????<property?name="dataSource">
          25?????????????<ref?bean="dataSource"?/>
          26?????????</property>
          27?????????<property?name="hibernateProperties">
          28?????????????<props>
          29?????????????????<prop?key="hibernate.dialect">????????????????????org.hibernate.dialect.DerbyDialect????????????????</prop>
          30?????????????</props>
          31?????????</property>
          32?????????<property?name="mappingResources">
          33?????????????<list>
          34?????????????????<value>UserInfo.hbm.xml</value></list>
          35?????????</property></bean>
          36?????<bean?id="UserInfoDAO"?class="UserInfoDAO">
          37?????????<property?name="sessionFactory">
          38?????????????<ref?bean="sessionFactory"?/>
          39?????????</property>
          40?????</bean></beans>

          UserInfo.java
          ?1?//?default?package
          ?2?
          ?3?
          ?4?
          ?5?/**
          ?6??*?UserInfo?generated?by?MyEclipse?-?Hibernate?Tools
          ?7??*/
          ?8?
          ?9?public?class?UserInfo??implements?java.io.Serializable?{
          10?
          11?
          12?????//?Fields????
          13?
          14??????private?Integer?id;
          15??????private?String?name;
          16?
          17?
          18?????//?Constructors
          19?
          20?????/**?default?constructor?*/
          21?????public?UserInfo()?{
          22?????}
          23?
          24?????/**?minimal?constructor?*/
          25?????public?UserInfo(Integer?id)?{
          26?????????this.id?=?id;
          27?????}
          28?????
          29?????/**?full?constructor?*/
          30?????public?UserInfo(Integer?id,?String?name)?{
          31?????????this.id?=?id;
          32?????????this.name?=?name;
          33?????}
          34?
          35????
          36?????//?Property?accessors
          37?
          38?????public?Integer?getId()?{
          39?????????return?this.id;
          40?????}
          41?????
          42?????public?void?setId(Integer?id)?{
          43?????????this.id?=?id;
          44?????}
          45?
          46?????public?String?getName()?{
          47?????????return?this.name;
          48?????}
          49?????
          50?????public?void?setName(String?name)?{
          51?????????this.name?=?name;
          52?????}
          53????
          54?
          55?
          56?
          57?
          58?
          59?
          60?
          61?
          62?}

          UserInfo.hbm.xml

          ?1?<?xml?version="1.0"?encoding="utf-8"?>
          ?2?<!DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
          ?3?"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
          ?4?<!--?
          ?5?????Mapping?file?autogenerated?by?MyEclipse?-?Hibernate?Tools
          ?6?-->
          ?7?<hibernate-mapping>
          ?8?????<class?name="UserInfo"?table="USER_INFO"?schema="APP">
          ?9?????????<id?name="id"?type="java.lang.Integer">
          10?????????????<column?name="ID"?/>
          11?????????????<generator?class="assigned"?/>
          12?????????</id>
          13?????????<property?name="name"?type="java.lang.String">
          14?????????????<column?name="NAME"?length="10"?/>
          15?????????</property>
          16?????</class>
          17?</hibernate-mapping>
          18?

          UserInfoDAO.java
          ??1?//?default?package
          ??2?
          ??3?import?java.util.List;
          ??4?import?org.apache.commons.logging.Log;
          ??5?import?org.apache.commons.logging.LogFactory;
          ??6?import?org.hibernate.LockMode;
          ??7?import?org.springframework.context.ApplicationContext;
          ??8?import?org.springframework.orm.hibernate3.support.HibernateDaoSupport;
          ??9?
          ?10?/**
          ?11??*?Data?access?object?(DAO)?for?domain?model?class?UserInfo.
          ?12??*?@see?.UserInfo
          ?13??*?@author?MyEclipse?-?Hibernate?Tools
          ?14??*/
          ?15?public?class?UserInfoDAO?extends?HibernateDaoSupport?{
          ?16?
          ?17?????private?static?final?Log?log?=?LogFactory.getLog(UserInfoDAO.class);
          ?18?
          ?19?????protected?void?initDao()?{
          ?20?????????//do?nothing
          ?21?????}
          ?22?????
          ?23?????public?void?save(UserInfo?transientInstance)?{
          ?24?????????log.debug("saving?UserInfo?instance");
          ?25?????????try?{
          ?26?????????????getHibernateTemplate().save(transientInstance);
          ?27?????????????log.debug("save?successful");
          ?28?????????}?catch?(RuntimeException?re)?{
          ?29?????????????log.error("save?failed",?re);
          ?30?????????????throw?re;
          ?31?????????}
          ?32?????}
          ?33?????
          ?34?????public?void?delete(UserInfo?persistentInstance)?{
          ?35?????????log.debug("deleting?UserInfo?instance");
          ?36?????????try?{
          ?37?????????????getHibernateTemplate().delete(persistentInstance);
          ?38?????????????log.debug("delete?successful");
          ?39?????????}?catch?(RuntimeException?re)?{
          ?40?????????????log.error("delete?failed",?re);
          ?41?????????????throw?re;
          ?42?????????}
          ?43?????}
          ?44?????
          ?45?????public?UserInfo?findById(?java.lang.Integer?id)?{
          ?46?????????log.debug("getting?UserInfo?instance?with?id:?"?+?id);
          ?47?????????try?{
          ?48?????????????UserInfo?instance?=?(UserInfo)?getHibernateTemplate()
          ?49?????????????????????.get("UserInfo",?id);
          ?50?????????????return?instance;
          ?51?????????}?catch?(RuntimeException?re)?{
          ?52?????????????log.error("get?failed",?re);
          ?53?????????????throw?re;
          ?54?????????}
          ?55?????}
          ?56?????
          ?57?????
          ?58?????public?List?findByExample(UserInfo?instance)?{
          ?59?????????log.debug("finding?UserInfo?instance?by?example");
          ?60?????????try?{
          ?61?????????????List?results?=?getHibernateTemplate().findByExample(instance);
          ?62?????????????log.debug("find?by?example?successful,?result?size:?"?+?results.size());
          ?63?????????????return?results;
          ?64?????????}?catch?(RuntimeException?re)?{
          ?65?????????????log.error("find?by?example?failed",?re);
          ?66?????????????throw?re;
          ?67?????????}
          ?68?????}????
          ?69?????
          ?70?????public?List?findByProperty(String?propertyName,?Object?value)?{
          ?71???????log.debug("finding?UserInfo?instance?with?property:?"?+?propertyName
          ?72?????????????+?",?value:?"?+?value);
          ?73???????try?{
          ?74??????????String?queryString?=?"from?UserInfo?as?model?where?model."?
          ?75??????????????????????????????????+?propertyName?+?"=??";
          ?76??????????return?getHibernateTemplate().find(queryString,?value);
          ?77???????}?catch?(RuntimeException?re)?{
          ?78??????????log.error("find?by?property?name?failed",?re);
          ?79??????????throw?re;
          ?80???????}
          ?81?????}
          ?82?
          ?83?????public?UserInfo?merge(UserInfo?detachedInstance)?{
          ?84?????????log.debug("merging?UserInfo?instance");
          ?85?????????try?{
          ?86?????????????UserInfo?result?=?(UserInfo)?getHibernateTemplate()
          ?87?????????????????????.merge(detachedInstance);
          ?88?????????????log.debug("merge?successful");
          ?89?????????????return?result;
          ?90?????????}?catch?(RuntimeException?re)?{
          ?91?????????????log.error("merge?failed",?re);
          ?92?????????????throw?re;
          ?93?????????}
          ?94?????}
          ?95?
          ?96?????public?void?attachDirty(UserInfo?instance)?{
          ?97?????????log.debug("attaching?dirty?UserInfo?instance");
          ?98?????????try?{
          ?99?????????????getHibernateTemplate().saveOrUpdate(instance);
          100?????????????log.debug("attach?successful");
          101?????????}?catch?(RuntimeException?re)?{
          102?????????????log.error("attach?failed",?re);
          103?????????????throw?re;
          104?????????}
          105?????}
          106?????
          107?????public?void?attachClean(UserInfo?instance)?{
          108?????????log.debug("attaching?clean?UserInfo?instance");
          109?????????try?{
          110?????????????getHibernateTemplate().lock(instance,?LockMode.NONE);
          111?????????????log.debug("attach?successful");
          112?????????}?catch?(RuntimeException?re)?{
          113?????????????log.error("attach?failed",?re);
          114?????????????throw?re;
          115?????????}
          116?????}
          117?
          118?????public?static?UserInfoDAO?getFromApplicationContext(ApplicationContext?ctx)?{
          119?????????return?(UserInfoDAO)?ctx.getBean("UserInfoDAO");
          120?????}
          121?}


          呵呵,其實用myeclipse開發還是很方面的,上面的這些文件全都是用myeclipse自動生成的
          posted on 2007-01-17 11:26 交口稱贊 閱讀(4930) 評論(2)  編輯  收藏 所屬分類: Java6

          FeedBack:
          # re: 學習Java6(六) 嵌入式數據庫Derby(8)Derby+spring+hibernate[未登錄] 2010-08-03 14:20 111
          我不信你這代碼能跑的起來,那里復制過來的  回復  更多評論
            
          # re: 學習Java6(六) 嵌入式數據庫Derby(8)Derby+spring+hibernate 2011-12-07 18:28 mens moncler coats
          不錯  回復  更多評論
            
          主站蜘蛛池模板: 九龙城区| 定襄县| 会东县| 沙雅县| 都兰县| 汉沽区| 贵阳市| 保德县| 仁怀市| 丹东市| 遂溪县| 特克斯县| 涟源市| 托克逊县| 周口市| 定西市| 怀化市| 大邑县| 库伦旗| 庆元县| 沂水县| 南充市| 云安县| 秦安县| 屯留县| 浦江县| 扎赉特旗| 彭泽县| 武邑县| 江华| 山西省| 万荣县| 屏东市| 九龙县| 新泰市| 广平县| 兴国县| 怀柔区| 利津县| 当雄县| 舟山市|