基于Hibernate实现CRUD操作

Hibernate实现CRUD操作

public class MyCRUD {
	public static void main(String[] args) {	
		System.out.println("1.查询");
		System.out.println("2.新增");
		System.out.println("3.更新");
		System.out.println("4.删除");
//		System.out.println("5.关联映射");
		int n;
		Scanner cin = new Scanner(System.in);
		n= cin.nextInt();
		
		switch(n) {
		case 1:{
			System.out.println("输入学号");
			int c = cin.nextInt();
			SelectDao  select = new SelectDao();
			select.testSelect(c);
			break;
		}
		case 2:{
			System.out.println("输入姓名");
			String name = cin.next();
			System.out.println("输入年龄");
			int age = cin.nextInt();
			System.out.println("输入成绩");
			int score = cin.nextInt();
			Student stu = new Student(name,age,score);
			SaveDao save = new SaveDao();
			save.testSave(stu);
			break;
		}
		case 3:{
			System.out.println("输入学号");
			int c = cin.nextInt();
			System.out.println("输入新名字");
			String name = cin.next();
			UpdateDao update = new UpdateDao();
			update.testUpdate(c, name);
			break;
		}
		case 4:{
			System.out.println("输入学号");
			int c = cin.nextInt();
			DeleteDao delete = new DeleteDao();
			delete.testDel(c);
			break;
		}
		default:{
			System.out.println("输入错误");
		}}}

查询方法

public class SelectDao {
public void testSelect(int n_id){
			Configuration configure = new Configuration().configure().addClass(Student.class);
			
			ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
			SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
			Session session = sessionFactory.openSession();
			try{
				Transaction t = session.beginTransaction();
				//Student student = session.load(Student.class,n_id);
				Query query = session.createQuery("select s.name, s.age, s.score from Student s where s.id=?");
				query.setParameter(0, n_id);
				List<Object[]> ss = query.list();
				for(Object[] s: ss) {
					System.out.println("姓名是:"+s[0]);
					System.out.println("年龄是:"+s[1]);
					System.out.println("分数是:"+s[2]);
				}
				t.commit();
			}catch(Exception e){
				e.printStackTrace();
				session.getTransaction().markRollbackOnly();
			}finally {
				session.close();
				sessionFactory.close();
			}}}
		

保存方法

public class SaveDao {
	public void testSave(Student stu){
			Configuration configure = new Configuration().configure().addClass(Student.class);
			ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
			SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
			Session session = sessionFactory.openSession();
			try{
				Transaction t = session.beginTransaction();	
				session.save(stu);//持久态
				System.out.println("保存成功");
				t.commit();
			}catch(Exception e){
				e.printStackTrace();	
				session.getTransaction().markRollbackOnly();
			}finally {
				session.close();
				sessionFactory.close();
			}}}

更新方法

public class UpdateDao {
	public void testUpdate(int n_id,String new_name){
		Configuration configure = new Configuration().configure().addClass(Student.class);
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
		SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
		Session session = sessionFactory.openSession();
		//Student student = new Student("Levi",18,100);//瞬时态
		try{
			Transaction t = session.beginTransaction();
			Student student = session.get(Student.class,n_id);
			student.setName(new_name);
			session.saveOrUpdate(student);//保存并更新
			t.commit();
			System.out.println("姓名更新成功");
		}catch(Exception e){
			e.printStackTrace();
	
			session.getTransaction().markRollbackOnly();
		}finally {
			session.close();
			sessionFactory.close();
		}}}

删除方法

public class DeleteDao {
	public void testDel(int n_id){			
		Configuration configure = new Configuration().configure().addClass(Student.class);
		
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
		SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
		Session session = sessionFactory.openSession();
		try{

			Transaction t = session.beginTransaction();
			
			Student student = session.get(Student.class, n_id);//持久态
			session.delete(student);
			
			t.commit();
			System.out.println("删除成功");
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().markRollbackOnly();
		}finally {
			session.close();
			sessionFactory.close();
		}}}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值