hibernate-helloworld

页面导航:首页 > 软件编程 > Java编程 > hibernate-helloworld

hibernate-helloworld

来源: 作者: 时间:2016-01-18 15:52 【

User javapackage cc hibernate domain;import java util Date;public class User { private int id; private String name; private Date birthday; public

User.java

package cc.hibernate.domain;

import java.util.Date;

public class User {
    private int id;
    private String name;
    private Date birthday;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

}

HibernateUtil.java

package cc.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import cc.hibernate.domain.User;

public final class HibernateUtil {

    private static SessionFactory sessionFactory;

    private HibernateUtil() {}

    static {
        Configuration cfg = new Configuration();
        cfg.configure();            //默认为hibernate.cfg.xml
        sessionFactory = cfg.buildSessionFactory();
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static User getUser(int id) {   //根据id获取记录
        return (User) sessionFactory.openSession().get(User.class, id);
    }

    public static void add(Object obj) {  //增加记录
        Session s = null; 
        Transaction tx = null;
        try {
            s = sessionFactory.openSession();
            tx = s.beginTransaction();
            s.save(obj);
            tx.commit();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            s.close();
        }
    }

    public static void update(Object obj) {  //更新记录
        Session s = null; 
        Transaction tx = null;
        try {
            s = sessionFactory.openSession();
            tx = s.beginTransaction();
            s.update(obj);
            tx.commit();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            s.close();
        }
    }

    public static void delete(Object obj) {  //删除记录
        Session s = null; 
        Transaction tx = null;
        try {
            s = sessionFactory.openSession();
            tx = s.beginTransaction();
            s.delete(obj);
            tx.commit();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            s.close();
        }
    }


    public static void hql_query(String name) {  //查询记录
        Session s = null; 
        try {
            s = sessionFactory.openSession();
            /*String hql = from User as user where user.name=?;   //hql语句
            Query query = s.createQuery(hql);
            query.setString(0, name);*/

            String hql = from User as user where user.name=:name;   //hql语句
            Query query = s.createQuery(hql);
            query.setString(name, name);

            //query.setFirstResult(0);  //分页
            //query.setMaxResults(10);

            List list = query.list(); //得到是User类的对象
            for(User user : list)
                System.out.println(user.getId());
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            s.close();
        }
    }


    public static void cri_query(String name) {  //查询记录
        Session s = null; 
        try {
            s = sessionFactory.openSession();
            Criteria c = s.createCriteria(User.class);
            c.add(Restrictions.eq(name, name)); 
            c.add(Restrictions.lt(id, 2));   //两个add是与的关系
            List list = c.list(); //得到是User类的对象
            for(User user : list)
                System.out.println(user.getId());
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            s.close();
        }
    }
}

User.hbm.xml

 



     
        
            
        
        
        
    

hibernate.cfg.xml

 
    
   
             
       
        
            
          
          com..jdbc.Driver    
           
            jdbc:mysql://localhost/hibernate      
        
          root    
              
          111111   

            
          org.hibernate.dialect.MySQLDialect    

         create 

         true  

          

     
    
package cc.hibernate;

import java.util.Date;

import org.hibernate.Session;

import cc.hibernate.domain.User;

public class Base {
    static Session s = HibernateUtil.getSessionFactory().openSession();

    public static void main(String[] args) {

        User user = new User();
        user.setBirthday(new Date());
        user.setName(chen);
        HibernateUtil.add(user);

        User user1 = new User();
        user1.setBirthday(new Date());
        user1.setName(chen);
        HibernateUtil.add(user1);

        HibernateUtil.cri_query(chen);

        //System.out.println(((User)HibernateUtil.getUser(1)).getName());

        System.out.println(end);
    }
}
Tags:

相关文章

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <