基于IOC容器實(shí)現(xiàn)管理mybatis過程解析
SqlSessionFactory是mybatis的基礎(chǔ)中的基礎(chǔ),必須實(shí)例!
邏輯思路:
- 減少代碼冗余,需要封裝mybatisAPI。
- 可以注冊SqlSessionFactoryBean,來完成SqlSessionFactory的實(shí)例化。
它的實(shí)例化需要(依賴)"mybatis-config.xml"文件,
其中有三大抽象:1、數(shù)據(jù)源;2、別名;3、注冊mapper
可以把依賴(作為屬性)注入(DI)到SqlSessionFactoryBean中,
來完成SqlSessionFactory的實(shí)例化。
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.Driver
jdbc.url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123
3、引入數(shù)據(jù)庫配置文件
<context:property-placeholder location="classpath:db.properties"/>
4、從spring自帶jdbc配置數(shù)據(jù)源
<bean id="dataSource" 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實(shí)例
<bean id="sqlSessionFactory" 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接口的實(shí)現(xiàn)類
<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();
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring data jpa 查詢自定義字段,轉(zhuǎn)換為自定義實(shí)體方式
這篇文章主要介紹了spring data jpa 查詢自定義字段,轉(zhuǎn)換為自定義實(shí)體方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java實(shí)現(xiàn)簡單的郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單的郵件發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07springboot+hutool批量生成二維碼壓縮導(dǎo)出功能
這篇文章主要介紹了springboot+hutool批量生成二維碼壓縮導(dǎo)出功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10java.lang.IllegalStateException異常原因和解決辦法
這篇文章主要給大家介紹了關(guān)于java.lang.IllegalStateException異常原因和解決辦法,IllegalStateException是Java標(biāo)準(zhǔn)庫中的一個異常類,通常表示在不合適或無效的情況下執(zhí)行了某個方法或操作,需要的朋友可以參考下2023-07-07解決cmd運(yùn)行java程序“找不到文件”提示的方案
在本篇文章里小編給大家分享的是關(guān)于解決cmd運(yùn)行java程序“找不到文件”提示的方案,有需要的朋友們可以參考下。2020-02-02Spring實(shí)現(xiàn)聲明式事務(wù)的方法詳解
這篇文章主要介紹了Spring實(shí)現(xiàn)聲明式事務(wù)的方法詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01Springboot詳細(xì)講解RocketMQ實(shí)現(xiàn)順序消息的發(fā)送與消費(fèi)流程
RocketMQ作為一款純java、分布式、隊(duì)列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時消息、消息回溯等,本篇我們了解如何實(shí)現(xiàn)順序消息的發(fā)送與消費(fèi)2022-06-06