• hibernate 基础知识


     1.hibernate的配置文件,一般放在classpath的根目录下,默认命名为hibernate.cfg.xml,代码例子如下:

    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
        
    <hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver  
        </property>
        <property name="connection.url">jdbc:mysql:///test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>
        
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hbm2ddl.auto">create-drop</property>
        <property name="show_sql">true</property>
        <mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" />
    </session-factory>
    </hibernate-configuration>

     2.hibernate的映射文件,User.hbm.xml,代码例子如下:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="cn.itcast.hibernate.domain">
    
        <class name="User">
            <id name="id">
                <generator class="native" />
            </id>
            <property name="name"/>
            <property name="birthday"/>
        </class>
    </hibernate-mapping>

     3.数据操作类,代码例子:

    package cn.itcast.hibernate;
    
    import java.io.Serializable;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    public final class HibernateUtil {
        private static SessionFactory sessionFactory;
        private static ThreadLocal session = new ThreadLocal();
    
        private HibernateUtil() {
        }
    
        static {
            Configuration cfg = new Configuration();
            cfg.configure();                          //如果配置文件路径或名字发生改变,那么就是cfg.configure("配置文件路径及名称");
            sessionFactory = cfg.buildSessionFactory();
        }
    
        public static Session getThreadLocalSession() {
            Session s = (Session) session.get();
            if (s == null) {
                s = getSession();
                session.set(s);
            }
            return s;
        }
    
        public static void closeSession() {
            Session s = (Session) session.get();
            if (s != null) {
                s.close();
                session.set(null);
            }
        }
    
        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        public static Session getSession() {
            return sessionFactory.openSession();
        }
    
        public static void add(Object entity) {
            Session s = null;
            Transaction tx = null;
            try {
                s = HibernateUtil.getSession();
                tx = s.beginTransaction();
                s.save(entity);
                tx.commit();
            } finally {
                if (s != null)
                    s.close();
            }
        }
    
        public static void update(Object entity) {
            Session s = null;
            Transaction tx = null;
            try {
                s = HibernateUtil.getSession();
                tx = s.beginTransaction();
                s.update(entity);
                tx.commit();
            } finally {
                if (s != null)
                    s.close();
            }
        }
    
        public static void delete(Object entity) {
            Session s = null;
            Transaction tx = null;
            try {
                s = HibernateUtil.getSession();
                tx = s.beginTransaction();
                s.delete(entity);
                tx.commit();
            } finally {
                if (s != null)
                    s.close();
            }
        }
    
        public static Object get(Class clazz, Serializable id) {
            Session s = null;
            try {
                s = HibernateUtil.getSession();
                Object obj = s.get(clazz, id);
                return obj;
            } finally {
                if (s != null)
                    s.close();
            }
        }
    }

      

  • 相关阅读:
    html5 laboratory
    Lind.DDD.LindAspects方法拦截的介绍
    MongoDB学习笔记~对集合属性的操作
    MongoDB学习笔记~数据模型属性为集合时应该为它初始化
    异步与并行~List<T>是线程安全的吗?
    基础才是重中之重~Emit动态构建方法(参数和返回值)
    XML和DTD的简单介绍和入门
    一些常用的Intent及intent-filter的信息
    最新县及县以上行政区划代码(截止2013年1月18日) 全国省市县数据库 之省市数据
    CRC32 vs Java.HashCode
  • 原文地址:https://www.cnblogs.com/lipengsheng-javaweb/p/4060194.html
Copyright © 2020-2023  润新知