框架介紹
框架:可以完成一部分功能的半成品項目
五種框架:
------------SSH:Spring + Structs2 + Hibernate
------------SSM:Spring + SpringMVC + MyBatis
JAVA三層架構:
--------------------servlet:Structs2或者SpringMVC取代 – 只能單獨使用其中一個,不能一起使用
--------------------dao:由Hibernate或MyBatis取代
--------------------連接三層:Spring 連接三層,包容市面上所有技術
Hibernate框架搭建
1.導入jar包
1.hibernate-release-5.0.7.Final\lib\required中所有jar包
2.數據庫驅動包
3.以及其他項目所需要的jar包
2.準備數據庫/實體類
實體類以Customer爲例:成員變量以及各自get/set方法
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
數據庫以cst_customer爲例:
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)',
`cust_name` varchar(32) NOT NULL COMMENT '客戶名稱(公司名稱)',
`cust_user_id` bigint(32) DEFAULT NULL COMMENT '負責人id',
`cust_create_id` bigint(32) DEFAULT NULL COMMENT '創建人id',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客戶信息來源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別',
`cust_linkman` varchar(64) 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;
3.書寫配置文件
Hibernate有兩種配置文件
1.Hibernate主配置文件:hibernate.cfg.xml - src下
2.對象關係映射配置文件:ORM元數據,位置隨意,文件名推薦:實體類名.hbm.xml
4.代碼測試
1.加載配置文件
Configuration configure = new Configuration().configure();
2.獲得session-factory
SessionFactory factory = configure.buildSessionFactory();
3.獲得session對象,取代connection
Session session = factory.openSession();
4.開啓事務
Transaction transaction = session.beginTransaction();
5.session操作數據庫
//根據cust_id查找customer
Customer customer = session.get(Customer.class,1L);
6.關閉事務 - commit/rollback
// 6.關閉事務 - commit/rollback
transaction.commit();
7.關閉資源
session.close();
factory.close();
Hibernate配置文件
1.Hibernate主配置文件
hibernate.cfg.xml - 導入dtd約束
位置:src下
1.導入dtd約束的步驟:
Eclipse:工具欄 window - preference - XML catalog - Add -導入本地文件
在dtd文件中:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
第一行爲publicID
第二行爲URI/WEB Address
本地無dtd,可以從:
jar包-hibernate-core-5.0.7.Final.jar - hibernate-configuration-3.0.dtd 中找到
書寫hibernate.cfg.xml配置文件:
配置文件鍵值對:hibernate-release-5.0.7.Final\project/etc/hibernate.properties - MYSQL
<!--複製hibernate-configuration-3.0.dtd 中的DOCTYPE信息-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--必填項 連接數據庫的基本信息 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///crm</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 選填 #hibernate.show_sql true 打印執行的sql語句
hibernate.format_sql true 格式化sql語句-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 選填auto schema export
自動導出表結構
#hibernate.hbm2ddl.auto create-drop -每次操作都是創建新表,操作完在刪除表
#hibernate.hbm2ddl.auto create -每次執行都是創建一張新表,原表被刪除
以上兩種選項不建議使用
#hibernate.hbm2ddl.auto update 如果表不存在,創建表;如果存在,在已有的表中操作 - 建議使用
如果表結構改變,同時會更新表結構-僅限增加新的字段
#hibernate.hbm2ddl.auto validate 只在表存在且表結構正確的時候可用
-->
<property name="#hibernate.hbm2ddl.auto">update</property>
<!-- 引入映射文件,使用相對路徑 元數據
resource:從類路徑開始
-->
<mapping resource="beans/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.對象關係映射配置文件
hibernate-mapping.hbm.xml:ORM元數據,位置隨意
文件名推薦:實體類名.hbm.xml
導入dtd約束:
書寫:
<!--複製hibernate-mapping-3.0.dtd 中的DOCTYPE信息-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 表和實體類的關係 -->
<!-- package:如果添加包,那麼其中的所有類名可以寫簡單類名 -->
<hibernate-mapping package="beans">
<class name="Customer" table="cst_customer">
<!--id:由hibernate維護的表,必須存在主鍵
name:主鍵對應的屬性名 - 必填
column:主鍵對應的字段名 - 省略時自動識別
name和column一致時,可以省略
type:字段類型 - 省略時自動識別
java類型:java.lang.Long
數據庫類型:bigint
hibernate:long/string/int
length:字段允許最大長度 - 省略時爲類型允許的最大長度
-->
<id name="cust_id" column="cust_id">
<!-- generator:主鍵生成策略 -->
<generator class="native"></generator>
</id>
<!--propertity: 除了主鍵以外的其他普通屬性/字段
name和column一致時,column可以省略
-->
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
HibernateAPI詳解
Configuration
public void test02() {
// Configuration config = new Configuration();
// // 必須調用,並且就返回對象本身
// config.configure();
Configuration config = new Configuration().configure();
// configure() 無參,默認加載 classpath[src]/hibernate.cfg.xml
// 添加ORM映射文件
// config.addClass(Customer.class);
// config.addResource("beans/Customer.hbm.xml");
// 直接在主配置文件中添加<mapping>,就可以直接調用而不用調用上面兩個方法
}
sessionFactory
public void test03() {
Configuration config = new Configuration().configure();
// SessionFactory: <session-factory>
// 作用簡單來說就是爲了得到操作數據庫的核心對象session
// SessionFactory其中保存了所有的配置信息,非常消耗資源
// SessionFactory設計,本身就是線程安全的
// 結論:項目運行中,SessionFactory只有一個 - 單例模式
SessionFactory factory = config.buildSessionFactory();
// 每次都獲得新的Session對象
factory.openSession();
// 獲得當前Session對象 - 事務 - ThreadLocal
factory.getCurrentSession();
}
Transcation
public void test04() {
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
// 開啓事務兩種方式
// 1.獲得並且直接開啓事務 - 推薦使用
Transaction tx = session.beginTransaction();
// 2.單純的獲得事務對象
// Transaction tx = session.getTransaction();
// tx.begin(); // 單獨開啓事務
tx.commit(); // 提交事務 - try/finally
//tx.rollback();// 回滾事務 - catch
}
數據庫的增刪改查
// 新增對象
public void test01() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 5.Session操作數據庫 增刪改查
// -----------------------
// 新增 - id 自增長,不需要傳入
Customer c = new Customer();
c.setCust_name("百度");
c.setCust_mobile("123456677");
session.save(c);
// -----------------------
// 6.關閉事務
tx.commit();
// 7.關閉資源
session.close();
}
// 根據ID查詢對象
public void test02() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 5.Session操作數據庫 增刪改查
// -----------------------
// 傳參:對象類型,id
// Customer customer = session.get(Customer.class, 1L);
Customer customer = session.load(Customer.class, 2L);
System.out.println(customer);
// -----------------------
// 6.關閉事務
tx.commit();
// 7.關閉資源
session.close();
}
// 修改對象
public void test03() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 5.Session操作數據庫 增刪改查
// -----------------------
// 先根據ID查詢
Customer customer = session.get(Customer.class, 1l);
customer.setCust_name("企鵝");
customer.setCust_level("VVIP");
session.update(customer);
// -----------------------
// 6.關閉事務
tx.commit();
// 7.關閉資源
session.close();
}
// 刪除對象
public void test04() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
// 5.Session操作數據庫 增刪改查
// -----------------------
// 先根據ID查詢
Customer customer = session.get(Customer.class, 1l);
session.delete(customer);
// -----------------------
// 6.關閉事務
tx.commit();
// 7.關閉資源
session.close();
}