Hibernate入門(第一天)
阿新 • • 發佈:2018-12-09
注意:一定要把hibernate.hbm.xml放置在src目錄下,因為系統預設位置是在src下,否則就會出現載入不到配置檔案 sql語句:
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)',
`cust_name` varchar(32) NOT NULL COMMENT '客戶名稱(公司名稱)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客戶資訊來源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定電話',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '行動電話',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
實體類:
public class Customer {
private long customerId;
private String customerName;
private String customerSource;
private String customerIndusrtry;
private String customerLevel;
private String customerPhone;
private String customerMobile;
}
匯入JAR包,我們首先建個lib的資料夾,把從官網下載的hibernate中lib中的required中的檔案全部匯入eclipse中的lib資料夾,以及日誌jar包和資料庫連結jar包
## MySQL
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-----------------------------------------------------
<hibernate-configuration>
<session-factory name="foo">
<property name="show_sql">true</property>
<mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>
<class-cache
class="org.hibernate.test.legacy.Simple"
region="Simple"
usage="read-write"/>
</session-factory>
</hibernate-configuration>
真正配置檔案:hibernate.cfg.xml檔案
<hibernate-configuration>
<session-factory >
<!-- 資料庫配置(必選):基本引數配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url"> jdbc:mysql:///test1</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 資料庫方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--可選配置,列印sql語句和格式化sql語句 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 引進對映檔案-->
<mapping resource="com/unistrong/demo/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Customer.hbm.xml檔案配置
<hibernate-mapping>
<!--name實體類,table資料庫表名,把實體類和資料表進行對映 -->
<class name="com.unistrong.demo.Customer" table="cst_customer">
<!-- 主鍵進行對映以及生成策略 -->
<id name="customerId" column="cust_id">
<generator class="native" />
</id>
<property name="customerName" column="cust_name" />
<property name="customerSource" column="cust_source" />
<property name="customerIndusrtry" column="cust_industry" />
<property name="customerLevel" column="cust_level" />
<property name="customerPhone" column="cust_phone" />
<property name="customerMobile" column="cust_mobile" />
<!-- <property name="cust_name" column="cust_name"/> 如果實體類的屬性和資料庫中的欄位一樣,就可以不寫column,比如這條 -->
</class>
</hibernate-mapping>
測試方法:
@Test
@Ignore
public void save() {
// 1.載入核心配置檔案Hibernate.cfg.xml檔案
Configuration configuration = new Configuration().configure();
// 2.得到SessionFactory相當於資料庫連線池
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 3.得到session物件,相當於開啟資料庫連線池
Session session = sessionFactory.openSession();
// 4.手動開啟事務
Transaction ts = session.beginTransaction();
// 5.新增資料
Customer customer = new Customer();
customer.setCustomerName("李四");
session.save(customer);
// 6.提交事務
ts.commit();
// 7.關閉資源
session.close();
}
@Test
@Ignore
public void query() {
// 1.載入核心配置檔案Hibernate.cfg.xml檔案
Configuration configuration = new Configuration().configure();
// 2.得到SessionFactory相當於資料庫連線池
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 3.得到session物件,相當於開啟資料庫連線池
Session session = sessionFactory.openSession();
// 4.查詢資料,get查詢及時查詢,不使用也會查詢出來,loader延時查詢只有使用的時候,才會去查詢
Customer customer = session.get(Customer.class, 1l);
Customer customer1 = session.load(Customer.class, 1l);
System.out.println(customer);
System.out.println(customer1);
// 5.關閉資源
session.close();
}
@Test
public void update() {
// 1.載入核心配置檔案Hibernate.cfg.xml檔案
Configuration configuration = new Configuration().configure();
// 2.得到SessionFactory相當於資料庫連線池
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 3.得到session物件,相當於開啟資料庫連線池
Session session = sessionFactory.openSession();
// 4.手動開啟事務
Transaction ts = session.beginTransaction();
// 5.新增資料
Customer customer =session.get(Customer.class, 1l);
customer.setCustomerName("外星人");
session.update(customer);
// 6.提交事務
ts.commit();
// 7.關閉資源
session.close();
}
}