配置資訊:
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