首页 > 代码库 > hibernate--CRUD初体验

hibernate--CRUD初体验

hibernate的crud操作初体验。

看具体实例

package com.fuwh.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;

@Entity
public class Student {

    @Id
    @GeneratedValue(generator="_native")
    @GenericGenerator(name="_native",strategy="native")
    private int id;
    @Column(name="stuName")
    private String name;
    
    private int age;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
}
<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
    
    <session-factory>
        <!-- 数据库连接设置 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">mysqladmin</property>
        <!--
             指定方言,表明用的是哪种数据库,也可以不指定,hibernate默认会翻译成正确的数据库脚本
            方言可以在 hibernate-release-xx.xx/project/etc/hibernate.properties 中查找
         -->
        <property name="hibernate.dialect">MySQL5</property>
        <!-- 设定时候更新表结构,不存在或自动创建 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 配置 在后台打印出sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
       <!-- 引入实体类和表的映射文件 -->
        <mapping class="com.fuwh.model.Student"/>
    </session-factory>
    
</hibernate-configuration>
package com.fuwh.service;

import static org.junit.Assert.*;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.fuwh.model.Student;

public class StudentTest {
    
    //标准的sessionFactory取得方式
    private SessionFactory sessionFactory;
    
    @Before
    public void setUp() throws Exception {
        final StandardServiceRegistry registry=new StandardServiceRegistryBuilder().configure().build();
        try {
            sessionFactory=new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } catch (Exception e) {
            // TODO: handle exception
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }

    @After
    public void tearDown() throws Exception {
        if(sessionFactory != null){
            sessionFactory.close();
        }
    }

    @Test
    public void testAdd() {
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        Student student=new Student();
        student.setName("李四");
        student.setAge(18);
        session.save(student);
        session.getTransaction().commit();
        session.close();
    }
    
    @Test
    public void testUpdate() {
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        Student student=(Student)session.get(Student.class, 1);
        student.setAge(19);
        session.saveOrUpdate(student);
        session.getTransaction().commit();
        session.close();
    }
    
    @Test
    public void testDelete() {
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        Student student=session.get(Student.class, 1);
        session.delete(student);
        session.getTransaction().commit();
        session.close();
    }
    
    @Test
    public void testQuery() {
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        List result=session.createQuery("from Student").getResultList();
        for(Student stu:(List<Student>)result){
            System.out.println(stu);
        }
        session.getTransaction().commit();
        session.close();
    }

}

 

hibernate--CRUD初体验