• 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();
            }
        }
    }

      

  • 相关阅读:
    [单选题]请求文件“time.inc”,当发生错误时就终止脚本,正确的方式是:
    [单选题]条件语句的时候不应该使用哪一种控制结构
    [高德地图]学习笔记--基本结构
    nodejs实战:小爬虫
    linux实用命令(2016/11/8-永远)
    自适应响应式布局-实现原理
    解决npm安装慢的方法
    git进阶(分支与标签管理)
    git进阶(远程仓库github)
    git入门命令(只涉及本地仓库管理)
  • 原文地址:https://www.cnblogs.com/lipengsheng-javaweb/p/4060194.html
Copyright © 2020-2023  润新知