hlbernate_helloworld

来源:互联网 发布:java类的定义 编辑:程序博客网 时间:2024/06/05 19:11


1.首先(F:\javasoft_chajian\)hibernate-release-4.2.4.Final\lib\required的jar包全部复制到项目的lib目录里面,同时bulid path加入路径中

2.在安装hibernatetools-Update-4.1.1.Final_2013-12-08_01-06-33-B605插件的基础上,在src下建立hibernate.cfg.xml文件,如:


一路next就行


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="connection.username">root</property>    <property name="connection.password">csc</property>    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    <property name="connection.url">jdbc:mysql:///hibernate5</property>        <!-- 配置hibernate的基本信息 -->    <!-- hibernate 所使用的数据库方言 -->    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>        <!-- 执行sql时是否在控制台上打印 -->    <property name="show_sql">true</property>        <!-- 是否对sql进行格式化 -->    <property name="format_sql">true</property>        <!-- 指定自动生成数据表的策略 -->    <property name="hbm2ddl.auto">update</property>        <!-- 指定关联的xxx.hbm.xml文件 -->    <mapping resource="cn/edu/sdut/hibernate/helloworld/News.hbm.xml"/>    </session-factory></hibernate-configuration>


注:数据库方言在F:\javasoft_chajian\hibernate-release-4.2.4.Final\project\etc下面的hibernate.properties里面能够找到。

注:如果hibernate.cfg.xml里面的http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd没有关联就不能提示相关信息。

关联方法:


点击add



将http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd复制到key里面key type选uri,路径是F:\LearnBook\javaweb\资料\开源框架jar包\SSH 框架\hibernate-release-4.2.4.Final\project\hibernate-core\src\main\resources\org\hibernate下面的hibernate-configuration-3.0.dtd


3.建立持久化类

package cn.edu.sdut.hibernate.helloworld;import java.sql.Date;public class News {privateInteger id;private String title;private String author;private Date date;public Date getDate() { return date;}public void setDate(Date date) {this.date = date;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public News() {super();}public News(String title, String author, Date date) {super();this.title = title;this.author = author;this.date = date;}@Overridepublic String toString() {return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";}}


4.建立类关系映射文件(相应的此文件与相应的持久化类在同一个包中)

1).


一路next就行


xxxx.hbm.xml如:News.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"><!-- Generated 2016-10-26 10:37:48 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="cn.edu.sdut.hibernate.helloworld.News" table="NEWS">        <id name="id" type="java.lang.Integer">            <column name="ID" />            <!-- 制定native的方式 ,指本地生成的方式 -->            <generator class="native" />        </id>        <property name="title" type="java.lang.String">            <column name="TITLE" />        </property>        <property name="author" type="java.lang.String">            <column name="AUTHOR" />        </property>        <property name="date" type="java.sql.Date">            <column name="DATE" />        </property>    </class></hibernate-mapping>


5.编写访问数据库的代码

import java.sql.Date;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.Test;import cn.edu.sdut.hibernate.helloworld.News;public class HibernateTest {@Testpublic void test(){//1.创建一个sessionFactory对象SessionFactory sessionFactory = null;try {//1).创建configuration对象,configuration指的是hibernate的基本信息及对象关系映射信息Configuration configuration = new Configuration().configure();//2).创建serviceRegistry对象,任何Hibernate 的任何配置和服务都要在serviceRegistry配置之后才生效ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();//3)。创建一个sessionFactory对象sessionFactory = configuration.buildSessionFactory(serviceRegistry);//2.创建一个session对象Session session = sessionFactory.openSession();//3.开启事务Transaction transaction = session.beginTransaction();//4.执行保存操作News news = new News("java", "csc", new Date(new java.util.Date().getTime()));session.save(news);//News news = (News) session.get(News.class, 1);//System.out.println(news);//news.setAuthor("pppp");//News news2 = (News) session.get(News.class, 1);//System.out.println(news2);//5.提交事务transaction.commit();//6.关闭sessionsession.close();//7.关闭sessionFactorysessionFactory.close();} catch (HibernateException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


小结:mysql数据库中只有hibernate5数据库,但是一开始没有NEWS表,这是根据hibernate生成的。


0 0