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

Hibernate基础笔记(一)

来源:互联网 收集:自由互联 发布时间:2021-07-03
Hibernate基础配置和简单的通过session进行对数据的操作 -------------实体类 Girl------------------package org.tlgg.bean;public class Girl {private Integer id;private String name;private Integer age;public Integer getId() {
Hibernate基础配置和简单的通过session进行对数据的操作
-------------实体类 Girl------------------
package org.tlgg.bean;

public class Girl {
	private Integer id;
	private String name;
	private Integer age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Girl(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Girl() {
		super();
	}
	@Override
	public String toString() {
		return "Girl [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
}

------------实体类的配置mapper文件org.tlgg.mapper.Girl.hbm.xml-----------------------


 


 
	
  
	
   
    
    
     
     
     girl_seq 
     
    
    
    
  

 


-------------hibernate.cfg.xml---------------------------------

 


 
	
   
    
   
    jdbc:oracle:thin:@localhost:1521:orcl
    
    
   
    system
    
    
   
    123456
    
    
   
    oracle.jdbc.driver.OracleDriver
    
    
   
    org.hibernate.dialect.Oracle10gDialect
    
    
   
    update
    
    
   
    true
    
    
   
    true
    
     
  

 


--------测试类---------------------
package org.tlgg.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.tlgg.bean.Girl;

public class Test {
	//new一个config对象,然后从配置中拿到sesssion工厂(默认从hibernate.cfg.xml中获取配置)
	private static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
	//如果配置文件命名为其他,则 new Configuration().configure("其他.xml").buildSessionFactory();
	//@org.junit.Test
	/**
	 * 通过session来对数据进行操作
	 * get(类,主键)查询
	 * save(对象) 添加
	 * update(对象) 修改
	 * delete(对象) 删除
	 * 被session所管理的对象被称为持久对象
	 * 在commit的时候,会自动匹配Hibernate内存中的对象字段和数据库的数据,
	 * 多删,少加,不同就改,一切以Hibernate的持久层对象为主
	 * */
	public void add() {
		
		Session session =  sessionFactory.openSession();
		//通过session拿到当前事务,然后开始操作
		session.getTransaction().begin();
		session.save(new Girl(101, "弱气乙女", 17));
		Girl first = (Girl) session.get(Girl.class, 1);
		System.out.println(first);
		first.setName("御姐");
		//提交
		session.getTransaction().commit();
		System.out.println(first);
	}
	
	public void update() {
		//只能修改原本存在的id的数据
		Session session = sessionFactory.openSession();
		session.getTransaction().begin();
		Girl girl = (Girl) session.get(Girl.class, 2);
		System.out.println(girl);
		Girl badGirl = new Girl(1, "Ariana", 24);
		session.update(badGirl);
		session.getTransaction().commit();
		System.out.println(session.get(Girl.class, 3));
	}
	@org.junit.Test
	public void delete() {
		//只能修改原本存在的id的数据
		Session session = sessionFactory.openSession();
		session.getTransaction().begin();
		Girl girl = (Girl) session.get(Girl.class, 2);
		session.delete(girl);
		session.getTransaction().commit();
		System.out.println(session.get(Girl.class, 2));
	}

}
上一篇:java基础笔记(九)
下一篇:build.gradle
网友评论