Hibernate:hibernate环境搭建并实现hibern

2017-08-04  本文已影响0人  Clannad_汐

一、简介:

二、环境搭建:

    <!-- 必须属性 用户名和密码按照自己的来-->
        <property name="connection.username">root</property>
        <property name="connection.password">154451</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>
        
        <!-- 常用属性 -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>

三、实现Hibernate示例(将学生类对象)

package hibernate_001;

import java.util.Date;

import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;

//学生  持久化类
public class Students {
    // 遵从的主要设计原则
    /*
     * 1.公有的类 
     * 2.提供公有的不带参的默认的构造方法 
     * 3.属性私有 
     * 4.属性setter、getter封装
     */

    private int sid; // 学号
    private String sname; // 姓名
    private String gender; // 性别
    private Date birthday; // 出生日期
    private String address; // 地址

    public Students() {

    }

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

    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 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 + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
                + ", address=" + address + "]";
    }

}

这里写图片描述
这里写图片描述
<!-- 将学生类 映射的xml加入到配置文件 -->
        <mapping resource="Students.hbm.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">154451</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>
        
        <!-- 常用属性 -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        
        <!-- 将学生类 映射的xml加入到配置文件 -->
        <mapping resource="Students.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

package hibernate_001;

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 StudentTest {
    private SessionFactory sessionFactory;  //会话工厂对象
    private Session session;
    private Transaction transcction;
    
    //准备工作 在 Test之前执行
    @Before
    public void init() {  //初始化
        //创建配置对象
        Configuration config = new Configuration().configure();
        //创建服务注册对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
                config.getProperties()).buildServiceRegistry();
        //创建会话工厂对象
        sessionFactory = config.buildSessionFactory(serviceRegistry);
        //会话对象
        session = sessionFactory.openSession();
        //开启事务
        transcction = session.beginTransaction();
    }
    
    @After
    public void destory() {
        transcction.commit();  //提交事务
        session.close(); //关闭会话
        sessionFactory.close(); //关闭会话工厂
    }
    
    //测试方法 测试保存学生对象
    @Test
    public void testSaveStudent(){
        //生成学生对象
        Students stu = new Students(1,"疾风剑豪","男",new Date(), "召唤师峡谷");
        session.save(stu);  //保存对象到数据库
    }
    
}

上一篇 下一篇

猜你喜欢

热点阅读