日期:2014-05-19 浏览次数:20813 次
public void create() { Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session s = sf.openSession(); System.out.println("增加"); s = sf.openSession(); Transaction tx = s.beginTransaction();// 开启事务 User user = new User(); user.setName("张三"); user.setPassword("1234"); s.save(user); tx.commit(); s.close(); } //这是添加的代码
public void selectId() { Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); User user = new User(); user.setName("张三"); user.setPassword("1234"); System.out.println("查找"); Session s = sf.openSession(); System.out.println("打印一下user.getID():"+user.getId()); User use1 = (User) s.get(User.class, user.getId());// 调用get方法 if (use1 == null) { System.out.println("三蛋你妹啊,这怎么还是空的啊"); } else { System.out.println("ID:" + use1.getId());// 获取其中的属性 System.out.println("NAME:" + use1.getName()); System.out.println("AGE:" + use1.getPassword()); } s.close(); } //这是查找的代码
public void selectId() { Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session s = sf.openSession(); System.out.println("增加"); s = sf.openSession(); Transaction tx = s.beginTransaction();// 开启事务 User user = new User(); user.setName("张三"); user.setPassword("1234"); s.save(user); tx.commit(); s.close(); System.out.println("查找"); s = sf.openSession(); System.out.println("打印一下user.getID():" + user.getId()); User use1 = (User) s.get(User.class, user.getId());// 调用get方法 if (use1 == null) { System.out.println("三蛋你妹啊,这怎么还是空的啊"); } else { System.out.println("ID:" + use1.getId());// 获取其中的属性 System.out.println("NAME:" + use1.getName()); System.out.println("AGE:" + use1.getPassword()); } s.close(); } //这样的话 增加 和查看在一起 了 就没有空异常了 这是为什么呢 我想了2天没想明白。。=