Hibernate 實體類 生成表結構 ---- 領域(Domain)驅動建模
傳統做法,先數據庫設計,生成表,編寫對應類結構
1. 將實體類,複製項目
將 新 BOS 項目資料_day02_01_基礎數據模塊實體類 的 cn 文件夾 複製 src/main/java
JPA 註解使用回顧
@Entity 標明是實體類
@Table 映射表名稱
@Id 主鍵標識
@GeneratedValue 自動生成主鍵的值
@Column 設置映射數據表中列信息
@Temporal 控制數據表保存日期哪個部分(日期、時間、日期時間)
@OneToMany(mappedBy 相當於 inverse ) @ManyToOne 結合 @JoinColumn 添加一列外鍵
@ManyToMany 一方寫 mappedBy 放棄外鍵維護、另一方 @JoinTable 維護中間表
實體類和表 映射
Archive 基礎檔案、 SubArchive 子檔案
Courier 快遞員 、Standard 收派標準 、Vehicle 車輛
Area 區域、 FixedArea 定區、 SubArea 分區
TakeTime 收派時間 --- 快遞員排班功能
2. Oracle 數據庫安裝配置使用
使用 oracle 11g XE 版 (簡版)
正式版 oracle 安裝後,自動建立服務實例 ORCL
XE 版 oracle 安裝後,自動建立服務實例 XE
安裝過程中,設置 SYS 賬號和 SYSTEM 賬號密碼, 密碼 123456
TNS 監聽端口 1521 、 HTTP 端口默認 8081 (建議不要用 8080 )
安裝默認目錄 : C:\oraclexe\app\oracle\product\11.2.0\server
檢查 oracle 兩個服務是否啓動
1、 OracleService 實例名稱
2、 Oracle 實例名稱 TNSListener
先通過 sqlplus 連接 oracle 是否安裝成功
3. PLSQLDeveloper 安裝配置使用
通常企業中,在客戶端通過圖形界面工具去操作 Oracle 服務器
1) PLSQL Developer
2) TOAD
PLSQL Developer 連接 Oracle 是需要 Oracle instantclient (SQLPlus)
1、 在客戶端測試 sqlplus 是否可以基於網絡 連接遠程 oracle
這樣連接 Oracle 並不是通過 oracle 監聽器,基於網絡進行連接
這樣連接 Oracle,基於網絡通過監聽器完成連接
2、 在 PLSQL Developer 啓動,配置 oracle instanceclient 目錄
配置 oracle instanceclient 作爲 oracle 主目錄,配置 ocl 文件
3、 使用萬能連接方式,連接任意電腦 Oracle
4、 默認 PLSQL Developer 連接 Oracle 使用客戶端字符集 utf-8 , 基於工具使用 SQL 插入
中文有亂碼問題,設置客戶端字符集 gb2312
配置 環境變量
NLS_LANG = SIMPLIFIED CHINESE_CHINA.ZHS16GBK
彈出窗口,配置成功 !!
補充: 基於 tnsnames.ora 配置文件,配置 ip、port、sid,下拉列表選作服務連接 oracle
在 oracle instance client 目錄, 創建 network/ADNIN/tnsnames.ora
最簡單做法只需要將安裝 oracle 目錄下方 network 複製 oracleclient 目錄
4. 針對 BOS 系統建立新的用戶
創建 bos/bos 賬戶, 授予角色 connect、resource
-- Create the user
create user BOS
identified by ""
default tablespace SYSTEM
temporary tablespace TEMP
profile DEFAULT
password expire;
-- Grant/Revoke role privileges
grant connect to BOS;
grant resource to BOS;
-- Grant/Revoke system privileges
grant unlimited tablespace to BOS;
5. 搭建項目持久層架構
項目使用 spring + spring data jpa + jpa (hibernate 實現 )
5.1. 項目集成 spring 環境
1、 Maven 座標導入
IOC 導入 context 依賴 beans、core、expression
AOP 導入
測試集成
Web 集成 配置監聽器
整合持久層框架 hibernate、jpa
2、 web.xml 配置 spring 監聽器
3、 src/main/resource 下 建立 applicationContext.xml
4、 src/main/resource 提供 config.properties 外部屬性文件、log4j.properties 日誌配置文件
5.2. Spring 整合 JPA
1、 在 applicationContext.xml 配置數據庫連接池
pom 引入
applicationContext.xml
2、 整合 JPA 配置
pom 導入
applicationContext.xml
<property name="generateDdl" value="true" />
配置自動建表
Hibernate 是 JPA 接口規範實現
SessionFactory --- JPA EntityManagerFactory
Session --- JPA EntityManager
3、 配置聲明式事務管理