继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

霜花似雪
关注TA
已关注
手记 163
粉丝 1.5万
获赞 8507

一。建立在Navicat中数据库hibernate,创建数据表Students.如图:
图片描述

二。在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包有:
图片描述

项目结构为:
图片描述

打开App,阅读手记
12人推荐
发表评论
随时随地看视频慕课网APP

热门评论

写的很好,只是代码太乱,不够清晰,希望大神再简单点

查看全部评论