Rhythmk 一步一步学 JAVA(5) Hibernate环境配置
配置信息:
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 (?, ?, ?, ?, ?)
输出成功。
https://files.cnblogs.com/rhythmK/RhythmkHibernate_1.rar
一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。