hibernate初学helloworld

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">root</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/scott</property>
    <!-- 数据库方言 -->
    <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/ls/vo/News.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

持久化类

package cn.ls.vo;

import java.sql.Date;

public class News {
    private Integer id;
    private String title;
    @Override
    public String toString() {
        return "News [id=" + id + ", title=" + title + ", author=" + author + ", 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 Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    private String author;
    private Date date;
    public News() {
    }

    public News(String title, String author, Date date) {
        super();
        this.title = title;
        this.author = author;
        this.date = date;
    }

}

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 2017-8-4 19:39:30 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="cn.ls.vo.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>

Test类

package cn.ls.vo;

import static org.junit.Assert.*;

import java.util.Date;



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.After;
import org.junit.Before;
import org.junit.Test;

public class HibernateTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() {
        //1.创建一个SessionFactory对象
        SessionFactory sf=null;
            //1)创建configuretion对象,对应hibernate的基本配置信息和对象关系映射信息
        Configuration con= new Configuration().configure();
        //4.0前这样创建
        //sf =con.buildSessionFactory();
            //2)创建一个ServiceRegistry对象,hibernate的任何配置和服务都需要在该对象中注册后有效
        Configuration configuration=new Configuration().configure();
        ServiceRegistry service=
             new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                        .buildServiceRegistry();
        sf=configuration.buildSessionFactory(service);
        //2.创建一个session对象
        Session session=sf.openSession();
        //3.开启事务
        Transaction tr=session.beginTransaction();
        //4.执行保存操作
        News news= new News("java","ATGUIGU",(java.sql.Date) new Date());
        //5.提交事务
        tr.commit();
        //6关闭session对象
        session.close();
        //7.关闭SessionFactory对象
        sf.close();
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值