Hibernate学习笔记----hibernate的helloworld

来源:互联网 发布:什么是知天命 编辑:程序博客网 时间:2024/06/04 08:50

刚刚开始学习hibernate,写一下入坑心得吧,绕了很多弯路,写出来让以后的我不要再走了,也让看我博客的同学们不要再走了

需求:在news表中添加一条记录

贴上代码,要点在代码中写上了

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!--数据库信息-->        <property name="connection.username">root</property>        <property name="connection.password">123456</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql:///hibernate</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>        <!--指定关联的.hbm.xml文件-->        <mapping resource="cn/limbo/hibernate/helloworld/News.hbm.xml"/>    </session-factory></hibernate-configuration>
News.java

package cn.limbo.hibernate.helloworld;import java.sql.Date;/** * Created by Limbo on 16/7/18. */public class News {    private int id;    private String title;    private String author;    private Date date;    public News(String title, String author, Date date) {        this.title = title;        this.author = author;        this.date = date;    }    public int getId() {        return id;    }    public void setId(int 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 Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }    @Override    public boolean equals(Object o) {        if (this == o) return true;        if (o == null || getClass() != o.getClass()) return false;        News news = (News) o;        if (id != news.id) return false;        if (title != null ? !title.equals(news.title) : news.title != null) return false;        if (author != null ? !author.equals(news.author) : news.author != null) return false;        if (date != null ? !date.equals(news.date) : news.date != null) return false;        return true;    }    @Override    public int hashCode() {        int result = id;        result = 31 * result + (title != null ? title.hashCode() : 0);        result = 31 * result + (author != null ? author.hashCode() : 0);        result = 31 * result + (date != null ? date.hashCode() : 0);        return result;    }    @Override    public String toString() {        return "News{" +                "id=" + id +                ", title='" + title + '\'' +                ", author='" + author + '\'' +                ", date=" + date +                '}';    }}

News.hbm.xml

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="cn.limbo.hibernate.helloworld.News" table="news" schema="hibernate">        <id name="id" column="id">            <!--指定主键生成方式,native:使用数据库本地方式-->            <generator class="native" />        </id>        <property name="title" column="title"/>        <property name="author" column="author"/>        <property name="date" column="date"/>    </class></hibernate-mapping>

test.java

package cn.limbo.hibernate.helloworld;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 java.sql.Date;/** * Created by Limbo on 16/7/18. */public class Test {    public static void main(String[] args) {        //1.创建一个SessionFactory对象        SessionFactory sessionFactory = null;            //1).创建Configuration对象:对应hibernate的基本配置信息和对象关系映射信息        Configuration configuration = new Configuration().configure();            //4.0之前这样创建        //sessionFactory = configuration.buildSessionFactory();            //2).创建一个ServiceRegistry对象: hibernate 4.x 新添加对象            //hibernate的任何配置和服务都需要在该对象中注册后才有效        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()                .applySettings(configuration.getProperties())                .buildServiceRegistry();            //3).        sessionFactory = configuration.buildSessionFactory(serviceRegistry);        //2.        Session session = sessionFactory.openSession();        //3.开启事务        Transaction transaction = session.beginTransaction();        //4.执行保存操作        News news = new News("Hello Hibernate","Limbo",new Date(new java.util.Date().getTime()));        session.save(news);        //5.提交事务        transaction.commit();        //6.关闭Session        session.close();        //7.关闭SessionFactory        sessionFactory.close();    }}

好了,到这里第一个例子就好了,但是注意一下一个坑点,就是hibernate4.3和hibernate4.2不一样的地方就是ServiceRegistryBuilder的创建,4.2中这个方法是可以用的,但是4.3中就过时了,4.3的版本我也写了一个,就在下面

test.java

package cn.limbo.hibernatr.helloworld;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import java.sql.Date;/** * Created by Limbo on 16/7/18. */public class test {    public static void main(String[] args) {        SessionFactory sessionFactory = null;                Configuration configuration = new Configuration().configure();                ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()                .applySettings(configuration.getProperties())                .build();                sessionFactory = configuration.buildSessionFactory(serviceRegistry);                Session session = sessionFactory.openSession();                Transaction transaction = session.beginTransaction();                News news = new News("Hello","Limbo", new Date(new java.util.Date().getTime()));                session.save(news);                transaction.commit();                session.close();                sessionFactory.close();    }}
就是一个地方有点不一样而已,其他都一样的


0 0
原创粉丝点击