国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Spring整合mybatis實現(xiàn)過程詳解

瀏覽:7日期:2023-08-27 09:04:16

增加了用于處理MyBatis的兩個bean:SqlSessionFactoryBean、MapperFactoryBean

1、注冊SqlSessionFactoryBean:

Spring整合mybatis實現(xiàn)過程詳解

(1)實現(xiàn) InitializingBean:調(diào)用其afterPropertiesSet方法(this.sqlSessionFactory = buildSqlSessionFactory())

目的就是對于sqlSessionFactory的初始化。

(2)FactoryBean:getBean方法獲取bean(= 獲取此類的getObject()返回的實例)

if (this.sqlSessionFactory == null) { afterPropertiesSet(); }return this.sqlSessionFactory;

2、注冊MapperFactoryBean:

Spring整合mybatis實現(xiàn)過程詳解

同樣實現(xiàn)FactoryBean和InitializingBean

this.sqlSessionTemplate = createSqlSessionTemplate(sqlSessionFactory);//sqlSession作為根據(jù)接口創(chuàng)建映射器代理的接觸類一定不可以為空,設(shè)定其sqlSessionFactory屬性時完成初始化。

<bean class='org.mybatis.spring.mapper.MapperFactoryBean'> <property name='mapperInterface' value='org.cellphone.uc.repo.mapper.UserMapper'/> <property name='sqlSessionFactory' ref='sqlSessionFactory'/></bean>//接口是映射器的基礎(chǔ),sqlSession會根據(jù)接口動態(tài)創(chuàng)建相應(yīng)的代理類,所以接口必不可少。

1.0:UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

2.0:UserMapper userMapper = (UserMapper) context.getBean('userMapper');

//MyBatis在獲取映射的過程中根據(jù)配置信息為UserMapper類型動態(tài)創(chuàng)建了代理類

3、使用MapperScannerConfigurer:

Spring整合mybatis實現(xiàn)過程詳解

讓它掃描特定的包,自動幫我們成批地創(chuàng)建映射器。不需要我們對于每個接口都注冊一個MapperFactoryBean類型的對應(yīng)的bean,在掃描的過程中通過編碼的方式動態(tài)注冊。

抽象:屏蔽掉了最原始的代碼(userMapper的創(chuàng)建)而增加了MapperScannerConfigurer的配置

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 根河市| 昔阳县| 沛县| 岗巴县| 宝山区| 怀化市| 禄丰县| 凌源市| 梁平县| 西盟| 诏安县| 满城县| 楚雄市| 滦平县| 宁波市| 平武县| 仲巴县| 郴州市| 宿松县| 会东县| 汨罗市| 偃师市| 新乐市| 安化县| 临泉县| 武宣县| 莫力| 铁力市| 许昌县| 新乡县| 博罗县| 蒙山县| 高密市| 吉首市| 确山县| 嘉义市| 微山县| 巴林右旗| 丹凤县| 锡林郭勒盟| 阿城市|