Rhythmk 一步一步學 JAVA(5) Hibernate環境配置

來源:互聯網
上載者:User

配置資訊:

  JDK 1.6

  Hibernate 3.3

  mysql  5.6

項目結構如下:

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"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration>    <session-factory>        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="connection.url">jdbc:mysql://localhost:3306/eftest</property>        <property name="connection.username">root</property>        <property name="connection.password">wangkun</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>        <!--是否在後台顯示Hibernate用到的SQL語句,開發時設定為true,便於差錯,程式運行時可以在Eclipse的控制台顯示Hibernate的執行Sql語句。項目部署後可以設定為false,提高運行效率 -->        <property name="hibernate.show_sql">true </property>        <mapping resource="com/rhythmk/model/NewsImage.hbm.xml" />    </session-factory></hibernate-configuration>

NewsImage:

package com.rhythmk.model;/** * NewsImage entity. @author MyEclipse Persistence Tools */public class NewsImage implements java.io.Serializable {    // Fields    private Long id;    private Integer newsId;    private Short isDefault;    private String imgSrc;    private String imgSrcOrg;    private Short ord;    // Constructors    /** default constructor */    public NewsImage() {    }    /** full constructor */    public NewsImage(Integer newsId, Short isDefault, String imgSrc,            String imgSrcOrg, Short ord) {        this.newsId = newsId;        this.isDefault = isDefault;        this.imgSrc = imgSrc;        this.imgSrcOrg = imgSrcOrg;        this.ord = ord;    }    // Property accessors    public Long getId() {        return this.id;    }    public void setId(Long id) {        this.id = id;    }    public Integer getNewsId() {        return this.newsId;    }    public void setNewsId(Integer newsId) {        this.newsId = newsId;    }    public Short getIsDefault() {        return this.isDefault;    }    public void setIsDefault(Short isDefault) {        this.isDefault = isDefault;    }    public String getImgSrc() {        return this.imgSrc;    }    public void setImgSrc(String imgSrc) {        this.imgSrc = imgSrc;    }    public String getImgSrcOrg() {        return this.imgSrcOrg;    }    public void setImgSrcOrg(String imgSrcOrg) {        this.imgSrcOrg = imgSrcOrg;    }    public Short getOrd() {        return this.ord;    }    public void setOrd(Short ord) {        this.ord = ord;    }}

NewsImage.hbm.xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.rhythmk.model.NewsImage" table="news_image" catalog="eftest">        <id name="id" type="java.lang.Long">            <column name="ID" />            <generator class="native" />        </id>        <property name="newsId" type="java.lang.Integer">            <column name="NewsID" not-null="true" />        </property>        <property name="isDefault" type="java.lang.Short">            <column name="IsDefault" not-null="true" />        </property>        <property name="imgSrc" type="java.lang.String">            <column name="ImgSrc" length="16777215" not-null="true" />        </property>        <property name="imgSrcOrg" type="java.lang.String">            <column name="ImgSrcOrg" length="16777215" not-null="true" />        </property>        <property name="ord" type="java.lang.Short">            <column name="Ord" not-null="true" />        </property>    </class></hibernate-mapping>

測試類別:

package com.rhythmk.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.rhythmk.model.NewsImage;public class testuser {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        SessionFactory sessionfac = new Configuration().configure()                .buildSessionFactory();        Session session = sessionfac.openSession();        NewsImage entity = new NewsImage();        org.hibernate.Transaction tx = session.beginTransaction();        entity.setImgSrc("imgsrc_______");        entity.setImgSrcOrg("imgsrc_ord");        entity.setIsDefault(((short) 1));        entity.setNewsId(100);        entity.setOrd(((short) 1));        session.save(entity);        tx.commit();        System.out.print("輸出成功。");    }}

 

 執行輸出結果:

Hibernate: insert into eftest.news_image (NewsID, IsDefault, ImgSrc, ImgSrcOrg, Ord) values (?, ?, ?, ?, ?)

輸出成功。

  http://files.cnblogs.com/rhythmK/RhythmkHibernate_1.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.