1.hibernate基礎(chǔ)

百科定義:

hibernate是java語(yǔ)言下的對(duì)象關(guān)系映射解決方案

同類產(chǎn)品:

ibatis:半ORM產(chǎn)品,可以直接寫(xiě)sql
mybatis:這是ibatis的升級(jí)版
springJDBC: 這是spring提供的持久層技術(shù)

hibernate和mybatis各有各的方式,hibernate封裝比較完整,完全是面向?qū)ο蠓绞讲僮鲾?shù)據(jù)庫(kù),而mybatis是可以寫(xiě)sql的,在大多數(shù)互聯(lián)網(wǎng)應(yīng)用中,使用mybatis比較多,但是hibernate還是要學(xué)習(xí)的

安裝配置

配置文件

<!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="hibernate.connection.url">jdbc:mysql://127.0.0.1/test"</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <mapping resource="entity/User.hbm.xml" /> -->
        
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <mapping resource="entity/User.hbm.xml"/>       
        
    </session-factory>
</hibernate-configuration>

sessionFactory工具類(單例模式)

    package util;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.classic.Session;
    
    public class hibernateUtil {
        private static SessionFactory factory;
        
        static{
            try{
                //讀取配置文件hibernate.cfg.xml
                //這里最后的函數(shù)configure,如果不加的話,是讀取.propertys文件
                Configuration cfg = new Configuration().configure();
                factory = cfg.buildSessionFactory();
            }catch(Exception e){
                e.printStackTrace();
            }       
        }
        
        public static Session getSession()
        {
            return factory.openSession();
        }
        
        public static void closeSession(Session session)
        {
            if(session!=null)
            {
                if(session.isOpen())
                {
                    session.close();
                }
            }
        }
        
        public static SessionFactory getSessionFactory()
        {
            return factory;
        }
    }

實(shí)體類:

    package entity;
    
    public class User {
        private int id;
        private String userName;
        private String passWd;
        
        public User(){}
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassWd() {
            return passWd;
        }
    
        public void setPassWd(String passWd) {
            this.passWd = passWd;
        }
        
        
    }

映射文件:

    <?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>
        <class name="entity.User">
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="userName" />        
            <property name="passWd" />
        </class>    
    </hibernate-mapping>

建表代碼:

    package util;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    
    
    public class ExportDb {
        public static void main(String[] args) {
            //讀取配置文件
            Configuration cfg = new Configuration().configure();
            
            SchemaExport export = new SchemaExport(cfg);
            //自動(dòng)生成對(duì)應(yīng)的表
            export.create(true, true);
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容