• 菜鸟学习Hibernate——简单的增、删、改、查操作


    讲述如何利用Hinbernate框架实现简单的数据库操作。

    1、加入junit.jar

    2、新建一个工具类HibernateUtils.Java把一些对session的公共操作封装起来


    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtils {
    /*
      *读取Hibernate.cfg.xml文件
    */
    	private static SessionFactory factory;
    	
    	static {
    		try {
    			//读取hibernate.cfg.xml文件
    			Configuration cfg = new Configuration().configure();
    			
    			//建立SessionFactory
    			factory = cfg.buildSessionFactory();
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    	/*
    	  *打开Session
    	*/
    	
    	public static Session getSession() {
    		return factory.openSession();
    	} 
    	/*
    	  *关闭Session
    	*/
    	
    	public static void closeSession(Session session) {
    		if (session != null) {
    			if (session.isOpen()) {
    				session.close();
    			}
    		}
    	}
    	
    	public static SessionFactory getSessionFactory() {
    		return factory;
    	}
    }
    

    3、新建一个test类继承TestCase(Junit包里面的)然后下面就利用test类对User对象的增、删、改、查。

    import java.util.Date;
    
    import org.hibernate.Session;
    
    import com.bjpowernode.hibernate.HibernateUtils;
    
    import com.bjpowernode.hibernate.User;
    
    import junit.framework.TestCase;
    
    public class test extends TestCase {
    /*
     * 增加User
     */
    	public void testSave() {
    		
    		Session session = null;
    		try {
    			session = HibernateUtils.getSession();
    			session.beginTransaction();
    			
    			User user = new User();
    			user.setName("李四");
    			user.setPassword("123");
    			user.setCreateTime(new Date());
    			user.setExpireTime(new Date());
    			session.save(user);
    			session.getTransaction().commit();
    		}catch(Exception e) {
    			e.printStackTrace();
    			session.getTransaction().rollback();
    		}finally {
    			HibernateUtils.closeSession(session);
    		}
    	}
    	/**
    	 * 删除
    	 */
    	public void testDel(){
    		Session session = null;
    		try {
    			session = HibernateUtils.getSession();
    			session.beginTransaction();
    			
    			User user=(User)session.load(User.class, "4028989545a244640145a24466300001");
    			
    			session.delete(user);
    			session.getTransaction().commit();
    		}catch(Exception e) {
    			e.printStackTrace();
    			session.getTransaction().rollback();
    		}finally {
    			HibernateUtils.closeSession(session);
    		}
    	}
    	
    	/**
    	 * 修改
    	 */
    	public void testUpdate(){
    		Session session = null;
    		try {
    			session = HibernateUtils.getSession();
    			session.beginTransaction();
    			
    			User user=(User)session.load(User.class, "4028989545a243ca0145a243cbf80001");
    			user.setName("王五");
    			session.update(user);
    			session.getTransaction().commit();
    		}catch(Exception e) {
    			e.printStackTrace();
    			session.getTransaction().rollback();
    		}finally {
    			HibernateUtils.closeSession(session);
    		}
    	}
    	
    	/**
    	 * 查询
    	 */
    	public void testLoad(){
    		Session session = null;
    		try {
    			session = HibernateUtils.getSession();
    			session.beginTransaction();
    			
    			User user=(User)session.load(User.class, "4028989545a243ca0145a243cbf80001");
    			System.out.println(user.getName());
    			System.out.println(user.getId());
    			System.out.println(user.getPassword());
    			System.out.println(user.getClass());
    			System.out.println(user.getCreateTime());
    			System.out.println(user.getExpireTime());
    			session.getTransaction().commit();
    		}catch(Exception e) {
    			e.printStackTrace();
    			session.getTransaction().rollback();
    		}finally {
    			HibernateUtils.closeSession(session);
    		}
    	}
    }
    


  • 相关阅读:
    linux c编程操作数据库(sqlite3应用)
    Silverlight-MEF-DEMO
    微软最有价值专家分享校园招聘心得体会
    C# IOCP服务器项目(学习)
    ASIHTTPRequest类库简介和使用说明
    用Python下载美国国家气候数据中心(NCDC)的气候数据
    20个大家应该知道的大数据资源
    分布式文件系统HDFS,大数据存储实战(一)
    vmware虚拟的ubuntu18.04死机,尝试关闭时关机报“虚拟机xxx繁忙”
    ubuntu下hadoop0.20.2报错/dfs/name is in an inconsistent state
  • 原文地址:https://www.cnblogs.com/baobeiqi-e/p/9884827.html
Copyright © 2020-2023  润新知