2016.10.16-hibernate demo

hibernate操作数据库

首先创建对应的类

package demo;
import java.util.Date;

public class Student {
    private int sid;
    private String sname;
    private float score;
    private Date birthday;
    public Student() {
        super();
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public float getScore() {
        return score;
    }
    public void setScore(float score) {
        this.score = score;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public Student(int sid, String sname, float score, Date birthday) {
        super();
        this.sid = sid;
        this.sname = sname;
        this.score = score;
        this.birthday = birthday;
    }   
}

创建Student.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-16 10:07:57 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="demo.Student" table="STUDENT">
        <id name="sid" type="int"> //用id标签包裹的属性代表表的主键
            <column name="SID" />
            <generator class="native" />//assigned手动复制ID,native表示自增长
        </id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" />
        </property>
        <property name="score" type="float">
            <column name="SCORE" />
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        </property>
    </class>
</hibernate-mapping>

创建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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">123456</property>
  <property name="hibernate.connection.url">jdbc:mysql:///test</property>
  <property name="hibernate.connection.username">root</property>
  <!-- 设置方言 -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <!-- 输出sql -->
  <property name="hibernate.show_sql">true</property>
  <!-- 格式化sql输出 -->
  <property name="hibernate.format_sql">true</property>
  <!-- sql执行方式 -->
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- 为了使用getCurrentSession -->
  <property name="hibernate.current_session_context_class">thread</property>
  
  <!-- 资源配置 映射-->
  <mapping resource="demo/Student.hbm.xml"/>
 </session-factory
</hibernate-configuration>

写个test类测试

package demo;

import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
    public static void main(String[] args) {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        Student stu = new Student(4, "test1", 100.0f, new Date());
        session.save(stu);
        transaction.commit();
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,759评论 18 399
  • Hibernate: 一个持久化框架 一个ORM框架 加载:根据特定的OID,把一个对象从数据库加载到内存中OID...
    JHMichael阅读 1,995评论 0 27
  • 本文包括: 1、CRM 项目的整体介绍 2、Hibernate 框架概述 3、Hibernate 快速入门 4、H...
    廖少少阅读 3,485评论 9 66
  • 一. Java基础部分.................................................
    wy_sure阅读 3,834评论 0 11
  • 文||懒小薇 爱情是世间永恒的话题,每个人都希望拥有完美的爱情,愿得一人心,白首不相离。 青春的时候,我们可以为爱...
    38ffe2ae3f02阅读 429评论 4 8