首页>代码>三大框架之hibernate入门学习教程增删改查>/hibernate/hibernate_1/src/com/qm/dao/StudentDao.java
package com.qm.dao;



import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.qm.entity.Student;
/**
 * 持久层
 * @author quanmin
 *
 */
public class StudentDao {

	public static void main(String[] args) {
		testSave();
		//testFind();
		//testUpdate();
		//testDelete();
		//testFindList();
	}
	/**
	 * 查询所有
	 */
	private static void testFindList(){
		Configuration conf=new Configuration();
		conf.configure("hibernate.cfg.xml");
		//创建工厂
		SessionFactory sf=conf.buildSessionFactory();
		//取得session
		Session session=sf.openSession();
		//sql是查询表名 hql是查询类名
		String hql="from Student";
		Query query=session.createQuery(hql);
		List<Student> studentList=query.list();
		for (Student student : studentList) {
			System.out.println(student.getName()+"="+student.getPassword()+"="+student.getHight()+"="+student.getSex());
		}
		session.close();
		sf.close();
	}
	/**
	 * 删除
	 */
	private static void testDelete(){
		Configuration conf=new Configuration();
		conf.configure("hibernate.cfg.xml");
		//创建工厂
		SessionFactory sf=conf.buildSessionFactory();
		//取得session
		Session session=sf.openSession();
		//开始事务
		session.beginTransaction();
		Student student=new Student();
		student.setId(10);
		session.delete(student);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
	/**
	 * 更新
	 */
	private static void testUpdate(){
		Configuration conf=new Configuration();
		conf.configure("hibernate.cfg.xml");
		//创建工厂
		SessionFactory sf=conf.buildSessionFactory();
		//取得session
		Session session=sf.openSession();
		//开始事务
		session.beginTransaction();
		//封装
		Student student=(Student)session.load(Student.class,8);
		System.out.println(student.getName()+"="+student.getPassword()+"="+student.getHight()+"="+student.getSex());
		student.setName("全哥哥");
		session.update(student);
		System.out.println(student.getName()+"="+student.getPassword()+"="+student.getHight()+"="+student.getSex());
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
	/**
	 * 查找一个
	 */
	private static void testFind(){
		Configuration conf=new Configuration();
		conf.configure("hibernate.cfg.xml");
		//创建工厂
		SessionFactory sf=conf.buildSessionFactory();
		//取得session
		Session session=sf.openSession();
		//封装
		Student student=(Student)session.load(Student.class,8);
		System.out.println(student.getName()+"="+student.getPassword()+"="+student.getHight()+"="+student.getSex());
		session.close();
		sf.close();
	}
	/**
	 * 保存
	 */
	private static void testSave() {
		Configuration conf=new Configuration();
		conf.configure("hibernate.cfg.xml");
		//创建工厂
		SessionFactory sf=conf.buildSessionFactory();
		//取得session
		Session session=sf.openSession();
		//开始事务
		session.beginTransaction();
		Student student=new Student("quanmina","123",172,1);
		session.save(student);
		System.out.println("保存成功");
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}
最近下载更多
周敏国  LV9 2022年10月22日
丶右转  LV11 2020年11月18日
名字随便的啦啦  LV7 2020年10月23日
林志勇  LV10 2020年4月28日
a837535114  LV10 2020年2月5日
段朝洪  LV15 2019年11月25日
我的名字是凑得  LV8 2018年12月23日
973445486  LV6 2018年12月12日
nanjing2017  LV15 2018年11月20日
hhit_zh  LV6 2018年10月11日
最近浏览更多
liang85  LV1 2023年6月30日
女王不该在山炮村养花  LV8 2023年4月16日
彩色天空  LV5 2023年4月2日
周敏国  LV9 2022年10月22日
微信网友_6186189978783744 2022年10月22日
暂无贡献等级
cqbscxh  LV11 2022年9月19日
wubinbin  LV11 2021年12月18日
c123456x  LV2 2021年12月9日
Ye55555 2021年12月6日
暂无贡献等级
lndulge  LV6 2021年6月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友