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&useUnicode=true&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