亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:3日期: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 數據庫
相關文章:
主站蜘蛛池模板: 国产不卡视频在线播放 | 国产精品成人一区二区1 | 色婷婷色99国产综合精品 | 热久久国产欧美一区二区精品 | 免费一级毛片在线播放 | 任你躁欧美一级在线精品免费 | 日本免费观看95视频网站 | 精品国产一区二区三区www | 亚洲性69影院在线观看 | 在线观看视频色 | 亚洲综合国产精品 | 国产精品成人不卡在线观看 | 亚洲色大成网站www久久九九 | 免费观看黄色大片 | 亚洲成在人线久久综合 | 日本黄色片在线免费观看 | 国产成人黄网在线免 | 又黄又爽又猛午夜性色播在线播放 | 国产麻豆剧看黄在线观看 | 日本久久黄色 | 91啪在线视频 | 国产视频久久久 | 性感视频网站 | 亚洲国产精品久久久久666 | 伊人丁香 | 国产成人激情 | 伊人二区 | 肉色网站 | 久久国内精品自在自线软件 | 亚洲精品乱码久久久久久蜜桃欧美 | 日韩激情成人 | 国产a级三级三级三级中国 国产a级午夜毛片 | 午夜视频1000| 国产激情一区二区三区在线观看 | 免费淫片 | 日韩中文字幕在线播放 | 国产精品第二页 | 特一级黄色 | 全免费a级毛片 | 日韩丝袜在线观看 | 久久国产精品一区二区三区 |