色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

基于IOC容器實現管理mybatis過程解析

瀏覽:9日期:2023-10-23 15:16:13

SqlSessionFactory是mybatis的基礎中的基礎,必須實例!

邏輯思路:

減少代碼冗余,需要封裝mybatisAPI。 可以注冊SqlSessionFactoryBean,來完成SqlSessionFactory的實例化。

它的實例化需要(依賴)'mybatis-config.xml'文件,

其中有三大抽象:1、數據源;2、別名;3、注冊mapper

可以把依賴(作為屬性)注入(DI)到SqlSessionFactoryBean中,來完成SqlSessionFactory的實例化。

pom:junit、webmvc、mysql-connector、spring-jdbc、mybatis、mybatis-spring、lombok

1、spring-dao.xml:bean約束

<beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd'></beans>

2、db.properties

jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/數據庫?serverTimezone=GMT%2B8jdbc.username=rootjdbc.password=123

3、引入數據庫配置文件

<context:property-placeholder location='classpath:db.properties'/>

4、從spring自帶jdbc配置數據源

<bean class='org.springframework.jdbc.datasource.DriverManagerDataSource'> <property name='driverClassName' value='${jdbc.driver}'/> <property name='url' value='${jdbc.url}'/> <property name='username' value='${jdbc.username}'/> <property name='password' value='${jdbc.password}'/></bean>

5、利用SqlSessionFactoryBean獲取配置SqlSessionFactory實例

<bean class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='dataSource'/> <property name='mapperLocations' value='classpath*:mapper/*.xml'/> <property name='typeAliasesPackage' value='pojo'/> </bean>

6、掃描dao包,同時生成sqlsessionTemplate和注入mapper接口的實現類

<bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'> <property name='basePackage' value='dao' /> <property name='sqlSessionFactoryBeanName' value='sqlSessionFactory'/></bean>

7、加載spring-dao.xml獲取上下文,從而為dao接口自動裝配

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext('spring/spring-dao.xml');StudentDao studentDao = (StudentDao) context.getBean('studentDao');List<Student> students = studentDao.selectAll();

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 无遮挡一级毛片私人影院 | 黄色日韩网站 | 美女被男人cao的爽视频黄 | 日本视频三区 | mm在线精品视频 | 69视频成人 | 中文 日本 免费 高清 | 久久福利青草精品免费 | 欧美日韩中文一区二区三区 | 一级毛片在播放免费 | 青青热久久国产久精品秒播 | 亚洲精品天堂一区 | 中国胖女人一级毛片aaaaa | 在线免费黄网 | 国产区最新 | 国产精品白浆流出视频 | 亚洲美女aⅴ久久久91 | 日本在线观看免费视频网址 | 手机在线观看亚洲国产精品 | 99久久精品免费观看国产 | 国产欧美日韩精品高清二区综合区 | 日韩一级片播放 | 欧美成人亚洲国产精品 | 精品国产一区二区三区久久 | 亚洲天堂色视频 | 精品午夜久久网成年网 | 在线视频一区二区 | 波多野结衣一区在线观看 | 国产精品在线观看 | 精品国产呦系列在线看 | 精品国产免费人成在线观看 | 国产伦久视频免费观看视频 | 手机看片1024国产基地 | 一本色道久久88亚洲精品综合 | 亚洲午夜a | 免费播放特黄特色毛片 | 欧美成人全部视频 | 亚洲国产一级毛片 | 中文字幕成人免费高清在线 | 欧美不卡一区二区三区 | 欧美一区二区在线 |