Intellij idea 配置hibernate web項(xiàng)目

1.創(chuàng)建項(xiàng)目的時(shí)候勾選Hibernate,這里第三布一種是可以選擇download從網(wǎng)上下載,也可以自己從網(wǎng)上下載hibernate的源碼,然后把其lib/required的包都導(dǎo)入到intellij中,可以看到兩種方法導(dǎo)入的包是一樣的,只不過(guò)一種從網(wǎng)上下載,一種直接本地添加

image
image

可以看到idea 為我們生成了一個(gè)hibernate配置文件

image

2.在WEB-INF下新建兩個(gè)文件夾lib 和classes,將工程所需要的jar包都放到lib目錄下

image

3. 更改output路徑和添加依賴

image
image
image
image
image

3.配置數(shù)據(jù)庫(kù)源

[圖片上傳中...(image-741903-1510642137647-4)]

image

之后點(diǎn)擊可顯示數(shù)據(jù)庫(kù)相關(guān)信息

image

接下來(lái)根據(jù)相應(yīng)的表來(lái)建立相應(yīng)的映射類(lèi)及hbm映射配置文件。

image
image

生成的CstCustomer.hbm.xml文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.gentleni.domain.CstCustomer" table="cst_customer" schema="hibernate_demo">
        <id name="custId">
            <column name="cust_id" sql-type="bigint(32)"/>
        </id>
        <property name="custName">
            <column name="cust_name" sql-type="varchar(32)" length="32"/>
        </property>
        <property name="custUserId">
            <column name="cust_user_id" sql-type="bigint(32)" not-null="true"/>
        </property>
        <property name="custCreateId">
            <column name="cust_create_id" sql-type="bigint(32)" not-null="true"/>
        </property>
        <property name="custSource">
            <column name="cust_source" sql-type="varchar(32)" length="32" not-null="true"/>
        </property>
        <property name="custIndustry">
            <column name="cust_industry" sql-type="varchar(32)" length="32" not-null="true"/>
        </property>
        <property name="custLevel">
            <column name="cust_level" sql-type="varchar(32)" length="32" not-null="true"/>
        </property>
        <property name="custLinkman">
            <column name="cust_linkman" sql-type="varchar(64)" length="64" not-null="true"/>
        </property>
        <property name="custPhone">
            <column name="cust_phone" sql-type="varchar(64)" length="64" not-null="true"/>
        </property>
        <property name="custMobile">
            <column name="cust_mobile" sql-type="varchar(16)" length="16" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>

修改 hibernate.cfg.xml文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--配置數(shù)據(jù)庫(kù)連接-->
        <!--比說(shuō)明字符編碼為utf-8-->
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate_demo?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8</property>
        <!--若驅(qū)動(dòng)導(dǎo)入報(bào)錯(cuò),請(qǐng)檢查是否倒入了相關(guān)數(shù)據(jù)庫(kù)連接的Jar包-->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <!--相關(guān)的屬性-->
        <property name="hbm2ddl.auto">update</property>
        <!--格式化控制臺(tái)顯示的MySQL語(yǔ)句-->
        <property name="format_sql">true</property>
        <!--在控制臺(tái)顯示執(zhí)行的MySQL語(yǔ)句-->
        <property name="show_sql">true</property>
        <!--使用MySQL數(shù)據(jù)庫(kù)的方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!--相關(guān)的映射-->
        <mapping resource="com/gentleni/domain/CstCustomer.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

測(cè)試代碼

public class Test1 {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;
    @Before
    public void Init() {
        Configuration configuration = new Configuration().configure();
        sessionFactory = configuration.buildSessionFactory();
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }
    @Test
    public void test() {
        CstCustomer cstCustomer = new CstCustomer();
        cstCustomer.setCustName("張三");
        session.save(cstCustomer);
    }
    @After
    public void destroy() {
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
}

控制臺(tái)輸出


image.png

數(shù)據(jù)添加成功


image.png
最后編輯于
?著作權(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)容

  • 本文包括: 1、CRM 項(xiàng)目的整體介紹 2、Hibernate 框架概述 3、Hibernate 快速入門(mén) 4、H...
    廖少少閱讀 3,531評(píng)論 9 66
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評(píng)論 6 342
  • Hibernate中配置主要分為兩種:一種包含了Hibernate與數(shù)據(jù)庫(kù)的基本連接信息,在Hibernate工作...
    FTOLsXD閱讀 2,217評(píng)論 0 10
  • 誰(shuí)的凝望燙醒你原始的流水和芬芳盤(pán)旋而來(lái) 翩翩而舞我愿把所有精魂撒到你的身上 把愿意和拋棄和情緒種進(jìn)體內(nèi)用我赤裸的情...
    1a43998ab2a4閱讀 274評(píng)論 1 6

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