当前位置 : 主页 > 编程语言 > c++ >

hibernate 入门代码

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt root 445566 com.mysql.jdbc.Driver jdbc:mysql:///test?useUnicode=truecharacterEncoding=UTF-8 org.hibernate.dialect.MySQLDialect true true create gistfile2.txt /* * 创建持久化类 * 持久化类要遵循JavaBean的规范:
gistfile1.txt
 


 
    
   
   
    root
   
    
   
    445566
   
    
   
    com.mysql.jdbc.Driver
   
    
    
   
    jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8
    
   
    org.hibernate.dialect.MySQLDialect
   
    
   
    true
   
    
   
    true
   
    
   
    create
   
    
   
    
  

 
gistfile2.txt
/*
 * 创建持久化类
 * 持久化类要遵循JavaBean的规范:
 * JavaBean 必须申明为 public class 即:必须是公有的类
 * JavaBean 的所有属性必须申明为 private 即:属性必须私有
 * 通过 setter 方法和 getter 方法设值和取值
 * 必须有一个公有无参构造方法
 */
public class Student {
	private int id; //学号
	private String name; // 姓名
	private String sex;// 性别
	private int age;// 年龄
	private String dept; //院系
	private float grade; //成绩
	
	public Student(){}

	public Student(int id, String name, String sex, int age, String dept, float grade) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.dept = dept;
		this.grade = grade;
	}

	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 String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public float getGrade() {
		return grade;
	}

	public void setGrade(float grade) {
		this.grade = grade;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", dept=" + dept + ", grade="
				+ grade + "]";
	}
	
	

}
gistfile3.txt
 


 

 
    
   
    
     
     
    
    
     
    
    
     
    
    
     
    
    
     
    
    
     
    
  

 
gistfile4.txt
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 TestStudent {
	
	SessionFactory sessionFactory;
	Session session;
	Transaction transaction;
	
	@Before
	public void init()  // 初始化
	{
		//创建配置对象
		Configuration config = new Configuration().configure();
		//创建服务注册对象
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
		//创建会话工厂
		sessionFactory = config.buildSessionFactory(serviceRegistry);
		//打开会话
		session = sessionFactory.openSession();
		//开启事务
		transaction = session.beginTransaction();
	}
	
	@After
	public void destory()  //释放资源
	{
		transaction.commit(); //提交事务
		session.close(); //关闭会话
		sessionFactory.close(); //关闭会话工厂
	}
	
	@Test
	public void testSaveStudent()
	{
		Student stu = new Student(2, "chen", "nan", 23, "shuxue", (float) 99.5);
		session.save(stu); // 保存到数据库
	}
}
网友评论