Java之Hibernate技术实现向MySql数据库中插入数据

2019-06-15 00:00:00 数据 插入 数据库中

一。建立在Navicat中数据库hibernate,创建数据表Students.如图:
《Java之Hibernate技术实现向MySql数据库中插入数据》

二。在Eclipse中创建项目Hibernate_second。
1.创建实体类:Students.java

package com.entity;

//创建实体类
import java.util.Date;

public class Students {
    private int sid;
    private String name;
    private String gender;
    private Date birthday;
    private String address;

    public Students()
    {

    }

    public Students(int sid, String name, String gender, Date birthday, String address) {
        super();
        this.sid = sid;
        this.name = name;
        this.gender = gender;
        this.birthday = birthday;
        this.address = address;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Students [sid=" + sid + ", name=" + name + ", gender=" + gender + ", birthday=" + birthday
                + ", address=" + address + "]";
    }
}

2.生成:Students.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-8-29 15:04:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.entity.Students" table="STUDENTS">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
    </class>
</hibernate-mapping>

3.生成: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:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>      
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

      <mapping resource="com/entity/Students.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

三。创建测试类
创建测试类:TestStudents.java

package com.entity;

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;

/*
 * Hibernate实现ORM的两大核心技术:XML解析、java的反射
 * 
 * 几个核心类,必须熟记。
 * Configuration
 * ServiceRegistry
 * SessionFactory
 * Session
 * Transaction
 * -----------------------------
 *  * 
 * */
public class TestStudents {
    private Configuration config; // 配置对象;
    private ServiceRegistry serviceRegistry; // 服务注册对象
    private SessionFactory factory; // 会话工厂。
    private Session session; // 会话对象。
    private Transaction tx; // 事务对象

    @Before
    public void init() {
        System.out.println("执行初始化工作....");
        // 加载配置文档。
        config = new Configuration().configure();
        // 获得服务注册对象。
        serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        // 获得会话工厂。
        factory = config.buildSessionFactory(serviceRegistry);
        // 获得会话对象。
        session = factory.openSession(); // Connection conn
        // factory.getCurrentSession();
    }

    @Test
    public void testSaveStudents() {
        System.out.println("执行测试用例....");
        // 开启事务
        tx = session.beginTransaction();

        // 添加一条学生记录到students表。使用hibernate实现,保存对象。
        Students s = new Students(0, "张三丰", "男", new Date(), "湖北武当山");
        session.save(s);

        tx.commit(); // 提交事务。
    }

    @After
    public void destory() {
        session.close(); // 关闭会话
        factory.close(); // 关闭工厂
        System.out.println("执行释放资源工作....");
    }

}

需要导入的jar包有:
《Java之Hibernate技术实现向MySql数据库中插入数据》

项目结构为:
《Java之Hibernate技术实现向MySql数据库中插入数据》

相关文章