自定義持久層框架
1.加載組態檔
2.創建兩個javaBean存放組態檔決議出來的內容
3.決議組態檔dom4j
4.創建sqlsession factory介面及實作類
生產sqlSession
5.創建SqlSession介面及實作類
定義資料庫crud操作
6.創建executor介面及實作類(封裝jdbc)
query(configuration,mapped Statement,params)
代碼實作
一、新建test工程
1.新建maven工程(test)
2.引入持久層框架依賴(持久層框架專案,見后文)
3.創建sqlMapConfig.xml
配置 dataSource
配置存放mapper.xml的全路徑
4.創建mapper.xml
創建標簽
二、新建自定義框架模塊
1.新建module工程
(一)加載組態檔
2.創建類resources,撰寫靜態方法,根據組態檔路徑將組態檔加載為位元組輸入流
3.在test工程中新建一個test類,呼叫resource
4.打包自定義模塊工程
5.test中引入自定義持久層框架的依賴
6.test中呼叫測驗,獲取配置位元組流
(二)創建容器物件存放組態檔
1.創建mappedStatement
內容:ID標識、回傳值型別、引數值型別、sql陳述句
statementId:namespace.id
2.創建Configuration
內容:datasource、map集合(存放mapped Statement):key:statementId
(三)決議組態檔
1.創建sqlSessionFactoryBuilder,在該類中創建方法build,回傳值:sqlSessionFactory,引數值:inputStream
2.創建SqlSession介面
3.撰寫build方法
使用dom4j決議,封裝Configuration:
(1)創建xmlConfigBuilder類,創建Configuration物件,創建建構式—>創建方法parseConfig:
方法實作:匯入相關依賴;SAXReader.read獲取Document;獲取根物件document.getRootElement;
獲取資料源element list集合root.selectNodes(“//property”);遍歷list,獲取值element.attributeValue(”name”)同理獲取value值;創建properties物件,存盤組態檔資訊;創建連接池物件new CombopooledDataSource set driver、jdbcurl、user、password,將連接池物件set到configuration;
(2)獲取mapper資訊list集合root.selectNodes(”//mapper”);遍歷list,獲取值“resource”得到mapper路徑;根據mapper路徑獲取配置流;創建xmlMapperBuilder類,宣告configuration物件,撰寫方法parse:
方法實作:
獲取namespace的值,獲取select節點list,遍歷list,獲取屬性值id、resultType、paramterType,將值存入mappedStatement,將mappedStatement put到configuration的map中,key值存namespace.id
回傳configuration到build方法,
test—in—>build方法—in—>parseConfig—in—>parse
創建sqlSessionFactory物件:工廠類:生產sqlSession 會話物件
創建實作類DefaultSqlSessionFactory,有參構造,傳遞configuration;
(四)生產sqlSession
撰寫openSession介面和實作類;
創建DefaultSqlSession類(sqlSession的實作類)
test中sqlSessionFactory.openSession
(五)sqlSession介面方法定義實作
SqlSession介面中新增方法selectList,物體型別不確定寫
test方法中呼叫sqlSession.selectOne,創建物體類user
創建建構式向下傳遞configuration
實作類實作select方法,完成對simpleExecutor的query方法呼叫
(六)創建executor介面及實作類
創建query方法,并實作(jabc實作)
1.注冊驅動,獲取鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/211645.html
標籤:java
