增加
crud.excecuteQuery(getSession()); Session session= getSession(); while(true){ System. out. println(' CRUD开始---请输入你的操作"): System. out. println(" 1.添加 2.制除 3.修改4.查找 5.退出”):Scanner sc=new Scanner (System. in):int choice=sc. nextInt(); switch (choice){ case 1: crud.doRead(session); break; case 2: main.doDelete(session); break; case 3: main.doUpdate(session) ; break; case 4: main.doCreat(session) ; break: case 5: return; } } public void doRead (Session session) { Transaction tx=session. beginTransaction(); Scanner sc=new Scanner(System.in); User user=new User(); System.out.println("请输入用户姓名");user.setUserName(sc.next()); System.out,println("请输入用户年龄");user.setUserAge(sc.next()); System.out.println("请输入用户性别,男女”); System.out.println("确认添加--y,退出--n"); if(sc.next().equals(" y")){ session.save(user); tx.commit(); return; } else { return; } }
运行结果
添加语句
Hibernate: insert into t_ user (username, age, gender,uid) values tuser0_ .uid as uid0_0_ 0_, tuser0_.username as username0_1_ 0_ , tuser0_.age as age0_2_ 0_, tuser0_.gender as gender0_3_ 0_, from hibernate.t_user tuser0_ left outer join t_ user tuserentitl_ on tuser0_ . uid=tusertl . uid where tuser0_ . uid=?
运行结果
删除
public void doDelete(Session session){ Transaction tx = session.beginTransactiom(); Scanner sc=new Scanner (System.in): Query query = session.createQuery("from User "); List<User>. user=query.list(); for(User user:users) { System.out.println( user.getUid() + "-----" + user.getUsername() + "-----" + user.getAge() + "-----" + user.getGender()); } System.out.println("请选择要删除的用户"); User Duser=session.get(User.class.sc.nestInt()); System.out.printin("确认要删除? "); if(sc.next().equals("y"){ session.remove(Duser); tx.commit(); } else { return; } }
运行结果
Hibernate: select tuser0_ .uid as uid0_0_ 0_, tuser0_.username as username0_1_ 0_ , tuser0_.age as age0_2_ 0_, tuser0_.gender as gender0_3_ 0_, from hibernate.t_user tuser0_ where tuser0_ . uid=?
运行结果
修改
public void doUpdate(Session session){ Transaction tx=session.beginTransaction(); Scanner sc=new Scanner(System.in); Query query = session.createQuery( s:"from User"); List<User> users=query.list(); for(User user:users) { System.out.println(user.getUid() + "-----" + user.getUsername() + "-----" + user.getAge() + "-----" + user.getGender()); } System.out.println("请选择你要修改的用户"); User Uuser=session.get(User.class.sc.nextInt()); System.out.println("请输入修改的姓名"); Uuser.setUserName(sc.next()); System.out.println("请输入年龄"); Uuser.setAge(sc.next()); System.out.printIn("请输入性别"); Uuser.setGender(sc.next()); System.out.printIn("确认要修改?"); if(sc.next() equals("y")) { session.update(Uuser); tx. commit(); } else { return; } }
运行结果
查询
public void doRead(Session session) { Transaction tx=session.beginTransaction(); Scanner sc=new Scanner(System.in) ; Query query = session. createQuery(s:" from User"); List<User> users=query.list(); tx.commit(); for(User user:users) { System.out.println(user.getUid() + "-----" + user.getUsername() + "-----" + user.getAge() + "-----" + user.getGender()); System.out.println("退出请输入0"); while(sc. nextInt()==0) { return; } }
查询语句
Hibernate: select tusert0_ .uid as uid0_0_, tuser0_.username as username0_1_ 0_ , tuser0_.age as age0_2_ 0_, tuser0_.gender as gender0_3_ 0_, from hibernate.t_user tuser0_
运行结果