Hibernate-helloWorld

安装eclispe hibernate插件

插件链接-密码:j1ma

创建一个工程导入jar包

hibernate链接-密码:bi6o
导入hibernate-release-5.2.10.Final\hibernate-release-5.2.10.Final\lib\required下面的jar包到工程并添加到BuildPath
加入mysql驱动-点击下载

hibernate.cfg.xml

可用插件建此配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//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">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://xxxx.xxxx.xxxx.xxxx:3306/hibernate</property>

        <!-- 配置hibernate的基本信息 -->
        <!-- hibernate所使用的数据库方言 -->
        <!-- <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> -->
        <!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->
        <property name="dialect">org.hibernate.dialect.MySQLMyISAMDialect</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="com/hgaong/helloworld/News.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

新建bean类

package com.hgaong.helloworld;

import java.sql.Date;

public class News {
    private int nId;
    private String nTitle;
    private String nAuthor;
    private Date createDate;
    public int getnId() {
        return nId;
    }
    public void setnId(int nId) {
        this.nId = nId;
    }
    public String getnTitle() {
        return nTitle;
    }
    public void setnTitle(String nTitle) {
        this.nTitle = nTitle;
    }
    public String getnAuthor() {
        return nAuthor;
    }
    public void setnAuthor(String nAuthor) {
        this.nAuthor = nAuthor;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    @Override
    public String toString() {
        return "News [nId=" + nId + ", nTitle=" + nTitle + ", nAuthor=" + nAuthor + ", createDate=" + createDate + "]";
    }
    public News(String nTitle, String nAuthor, Date createDate) {
        super();
        this.nTitle = nTitle;
        this.nAuthor = nAuthor;
        this.createDate = createDate;
    }
    public News() {
        super();
    }

}

生成 bean.hbm.xml

右键点击上面建的News.java使用插件快速建立 对象-数据表 映射文件

<?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 2017-4-29 0:47:34 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.hgaong.helloworld.News" table="NEWS">
        <id name="nId" type="int" access="field">
            <column name="NID" />
            <!-- 改成naive -->
            <generator class="native" />
        </id>
        <property name="nTitle" type="java.lang.String" access="field">
            <column name="NTITLE" />
        </property>
        <property name="nAuthor" type="java.lang.String" access="field">
            <column name="NAUTHOR" />
        </property>
        <property name="createDate" type="java.sql.Date">
            <column name="CREATEDATE" />
        </property>
    </class>
</hibernate-mapping>

测试类

package com.hgaong.helloworld;

import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class HibernateTest {
    @Test
    public void test() {
        // 1.创建一个SessionFactory对象
        SessionFactory sessionFactory = null;

        // 1).创建Configuration对象:对应hibernate的基本配置信息和对象关系信息
        Configuration configuration = new Configuration().configure();
        sessionFactory = configuration.buildSessionFactory();

        // 2.创建一个Session对象
        Session session = sessionFactory.openSession();

        // 3.开启事务
        Transaction transaction = session.beginTransaction();
        // 4.执行保存操作
        News news = new News("Java", "Hgaong", new Date(new java.util.Date().getTime()));
        session.save(news);

        // 5.提交事务
        transaction.commit();

        // 6.关闭Session
        session.close();

        // 7.关闭SessionFactory对象
        sessionFactory.close();
    }
}

结果

        <!-- 若表不存在会自动建立对应的表,前提是hibernate.cfg.xml需这样配置 -->
        <!-- 指定自动生成数据表的策略 -->
        <property name="hbm2ddl.auto">update</property>

控制台输出

Hibernate: 
    insert 
    into
        NEWS
        (NTITLE, NAUTHOR, CREATEDATE) 
    values
        (?, ?, ?)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值