<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    一.生成后的文件結構如下圖


    各文件內容如下:
    二.配置文件
    1.
    hibernate.cfg.xml
    <?xml?version="1.0"?encoding="UTF-8"?>
    <!DOCTYPE?hibernate-configuration?PUBLIC
    ????????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"
    ????????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
    >
    <hibernate-configuration>
    ????
    <session-factory>
    ????????
    <property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    ????????
    <property?name="hibernate.connection.password">1234</property>
    ????????
    <property?name="hibernate.connection.url">jdbc:mysql://localhost:3306/sample</property>
    ????????
    <property?name="hibernate.connection.username">root</property>
    ????????
    <property?name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    ????????
    <mapping?resource="cn/blogjava/start/TUser.hbm.xml"?/>
    ????
    </session-factory>
    </hibernate-configuration>

    2.
    hibernate.reveng.xml
    <?xml?version="1.0"?encoding="UTF-8"?>
    <!DOCTYPE?hibernate-reverse-engineering?PUBLIC?"-//Hibernate/Hibernate?Reverse?Engineering?DTD?3.0//EN"?"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd"?>

    <hibernate-reverse-engineering>
    ??
    <table-filter?match-catalog="sample"?match-name="t_user"/>
    </hibernate-reverse-engineering>

    3.
    GeneralHbmSettings.hbm.xml
    <?xml?version="1.0"?>
    <!DOCTYPE?hibernate-mapping?PUBLIC?
    ????"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
    ????"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
    >

    <hibernate-mapping>????
    <!--?
    ????Auto-generated?mapping?file?from
    ????the?hibernate.org?cfg2hbm?engine
    ????for?General?Global?Setttings
    -->

    ????
    <import?
    ????????
    class="cn.blogjava.start.TUser"
    ????????rename
    ="cn.blogjava.start.TUser"
    ????
    />
    ????

    </hibernate-mapping>

    4.TUser.hbm.xml
    <?xml?version="1.0"?>
    <!DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
    >
    <hibernate-mapping>
    <!--?
    ????????Auto-generated?mapping?file?from
    ????????the?hibernate.org?cfg2hbm?engine
    -->
    ????
    <class?name="cn.blogjava.start.TUser"?table="t_user"?catalog="sample">
    ????????
    <id?name="id"?type="integer">
    ????????????
    <column?name="id"?/>
    ????????????
    <generator?class="native"?/>
    ????????
    </id>
    ????????
    <property?name="name"?type="string">
    ????????????
    <column?name="name"?length="100"?not-null="true"?/>
    ????????
    </property>
    ????
    </class>
    </hibernate-mapping>

    5.
    log4j.properties

    ###?direct?log?messages?to?stdout?###
    log4j.appender.stdout
    =org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target
    =System.out
    log4j.appender.stdout.layout
    =org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern
    =%d{ABSOLUTE}?%5p?%c{1}:%L?-?%m%n

    ###?direct?messages?to?file?hibernate.log?###
    #log4j.appender.file
    =org.apache.log4j.FileAppender
    #log4j.appender.file.File
    =hibernate.log
    #log4j.appender.file.layout
    =org.apache.log4j.PatternLayout
    #log4j.appender.file.layout.ConversionPattern
    =%d{ABSOLUTE}?%5p?%c{1}:%L?-?%m%n

    ###?set?log?levels?-?for?more?verbose?logging?change?'info'?to?'debug'?###

    log4j.rootLogger
    =info,?stdout

    log4j.logger.org.hibernate
    =error
    #log4j.logger.org.hibernate
    =debug

    ###?log?HQL?query?parser?activity
    #log4j.logger.org.hibernate.hql.ast.AST
    =debug

    ###?log?just?the?SQL
    #log4j.logger.org.hibernate.SQL
    =debug

    ###?log?JDBC?bind?parameters?###
    log4j.logger.org.hibernate.type
    =info
    #log4j.logger.org.hibernate.type
    =debug

    ###?log?schema?export/update?###
    log4j.logger.org.hibernate.tool.hbm2ddl
    =debug

    ###?log?HQL?parse?trees
    #log4j.logger.org.hibernate.hql
    =debug

    ###?log?cache?activity?###
    #log4j.logger.org.hibernate.cache
    =debug

    ###?log?transaction?activity
    #log4j.logger.org.hibernate.transaction
    =debug

    ###?log?JDBC?resource?acquisition
    #log4j.logger.org.hibernate.jdbc
    =debug

    ###?enable?the?following?line?if?you?want?to?track?down?connection?###
    ###?leakages?when?using?DriverManagerConnectionProvider?###
    #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider
    =trace

    三.Java類
    1.POJO類TUser.java
    ?1package?cn.blogjava.start;
    ?2
    ?3
    ?4
    ?5/**
    ?6?*?TUser?generated?by?hbm2java
    ?7?*/

    ?8
    ?9public?class?TUser??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?TUser()?{
    22????}

    23????
    24????/**?constructor?with?id?*/
    25????public?TUser(Integer?id)?{
    26????????this.id?=?id;
    27????}

    28
    29????
    30
    31???
    32????//?Property?accessors
    33
    34????public?Integer?getId()?{
    35????????return?this.id;
    36????}

    37????
    38????public?void?setId(Integer?id)?{
    39????????this.id?=?id;
    40????}

    41
    42????public?String?getName()?{
    43????????return?this.name;
    44????}

    45????
    46????public?void?setName(String?name)?{
    47????????this.name?=?name;
    48????}

    49???
    50
    51
    52
    53
    54
    55
    56
    57
    58}

    2.測試類HibernateTest.java
    ??1package?cn.blogjava.start;
    ??2
    ??3import?java.util.List;
    ??4
    ??5import?junit.framework.Assert;
    ??6import?junit.framework.TestCase;
    ??7
    ??8import?org.hibernate.HibernateException;
    ??9import?org.hibernate.Session;
    ?10import?org.hibernate.SessionFactory;
    ?11import?org.hibernate.Transaction;
    ?12import?org.hibernate.cfg.Configuration;
    ?13
    ?14
    ?15public?class?HibernateTest?extends?TestCase?{
    ?16????
    ?17????Session?session?=?null;
    ?18????/**
    ?19?????*?JUnit中的setUp方法在TestCase初始化的時候會自動調用
    ?20?????*?一般用于初始化公用資源
    ?21?????*/

    ?22????protected?void?setUp()?{
    ?23????????try?{
    ?24????????????/**
    ?25?????????????*?可以采用hibernate.properties或者hibernate.cfg.xml
    ?26?????????????*?配置文件的初始化代碼
    ?27?????????????*?
    ?28?????????????*?采用hibernate.properties
    ?29?????????????*?Configuration?config?=?new?Configuration();
    ?30?????????????*?config.addClass(TUser.class);
    ?31?????????????*/

    ?32????????????
    ?33????????????//采用hibernate.cfg.xml配置文件,與上面的方法對比,兩個差異
    ?34????????????//1.Configuration的初始化方式
    ?35????????????//2.xml
    ?36????????????Configuration?config?=?new?Configuration().configure();
    ?37????????????SessionFactory?sessionFactory?=?config.buildSessionFactory();
    ?38????????????session?=?sessionFactory.openSession();
    ?39????????????
    ?40????????}
    ?catch?(HibernateException?e)?{
    ?41????????????//?TODO:?handle?exception
    ?42????????????e.printStackTrace();
    ?43????????}
    ????????
    ?44????}

    ?45
    ?46????/**
    ?47?????*?JUnit中的tearDown方法在TestCase執行完畢的時候會自動調用
    ?48?????*?一般用于釋放資源
    ?49?????*/
    ????
    ?50????protected?void?tearDown()?{
    ?51????????try?{
    ?52????????????session.close();????????
    ?53????????}
    ?catch?(HibernateException?e)?{
    ?54????????????//?TODO:?handle?exception
    ?55????????????e.printStackTrace();
    ?56????????}
    ????????
    ?57????}
    ????
    ?58????
    ?59????/**
    ?60?????*?對象持久化測試(Insert方法)
    ?61?????*/
    ????????
    ?62????public?void?testInsert()?{
    ?63????????Transaction?tran?=?null;
    ?64????????try?{
    ?65????????????tran?=?session.beginTransaction();
    ?66????????????TUser?user?=?new?TUser();
    ?67????????????user.setName("byf");
    ?68????????????session.save(user);
    ?69????????????session.flush();
    ?70????????????tran.commit();
    ?71????????????Assert.assertEquals(user.getId().intValue()>0?,true);
    ?72????????}
    ?catch?(HibernateException?e)?{
    ?73????????????//?TODO:?handle?exception
    ?74????????????e.printStackTrace();
    ?75????????????Assert.fail(e.getMessage());
    ?76????????????if(tran?!=?null)?{
    ?77????????????????try?{
    ?78????????????????????tran.rollback();
    ?79????????????????}
    ?catch?(Exception?e1)?{
    ?80????????????????????//?TODO:?handle?exception
    ?81????????????????????e1.printStackTrace();
    ?82????????????????}

    ?83????????????}

    ?84????????}

    ?85????}

    ?86????
    ?87????/**
    ?88?????*?對象讀取測試(Select方法)
    ?89?????*/
    ????????????
    ?90????public?void?testSelect(){
    ?91????????String?hql?=?"?from?TUser?where?name='byf'";
    ?92????????try?{
    ?93????????????List?userList?=?session.createQuery(hql).list();
    ?94????????????TUser?user?=?(TUser)userList.get(0);
    ?95????????????Assert.assertEquals(user.getName(),?"byf");
    ?96????????}
    ?catch?(Exception?e)?{
    ?97????????????//?TODO:?handle?exception
    ?98????????????e.printStackTrace();
    ?99????????????Assert.fail(e.getMessage());
    100????????}

    101????}

    102}

    103
    posted on 2006-07-05 14:37 knowhow 閱讀(784) 評論(0)  編輯  收藏 所屬分類: ORM:Hibernate及其他
    主站蜘蛛池模板: 蜜桃AV无码免费看永久| av午夜福利一片免费看久久| 久久午夜夜伦鲁鲁片免费无码| 亚洲伦乱亚洲h视频| a级毛片无码免费真人| 亚洲精品国产专区91在线| 久久久久国色av免费看| 亚洲毛片在线观看| 91av视频免费在线观看| 亚洲成AV人片久久| 波霸在线精品视频免费观看| 国产成人高清亚洲| 青青操免费在线观看| 国产精品亚洲аv无码播放| 污污网站免费观看| 亚洲国产精品成人综合色在线婷婷| 日韩版码免费福利视频| 亚洲乱码av中文一区二区| 中文字幕天天躁日日躁狠狠躁免费| 亚洲视频在线视频| 好爽又高潮了毛片免费下载| 国产精品亚洲综合网站| 一色屋成人免费精品网站| 亚洲偷偷自拍高清| 免费人成网站在线高清| 国产一二三四区乱码免费| 亚洲综合图片小说区热久久| 无限动漫网在线观看免费| 添bbb免费观看高清视频| 在线亚洲97se亚洲综合在线 | 免费无码VA一区二区三区 | 免费可以看黄的视频s色| 亚洲性无码一区二区三区| 亚洲国产婷婷综合在线精品 | 永久免费视频v片www| 一个人看的在线免费视频| 亚洲v高清理论电影| 无码国产精品久久一区免费| 一级特级aaaa毛片免费观看| 亚洲黄色在线观看| 日本黄色免费观看|