基于IOC容器實現(xiàn)管理mybatis過程解析
SqlSessionFactory是mybatis的基礎中的基礎,必須實例!
邏輯思路:
- 減少代碼冗余,需要封裝mybatisAPI。
- 可以注冊SqlSessionFactoryBean,來完成SqlSessionFactory的實例化。
它的實例化需要(依賴)"mybatis-config.xml"文件,
其中有三大抽象:1、數(shù)據(jù)源;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.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實例
<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接口的實現(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java數(shù)據(jù)結構篇之實現(xiàn)二叉搜索樹的核心方法
二叉搜索樹是一種常用的數(shù)據(jù)結構,它是一棵二叉樹,且每個節(jié)點的值都大于其左子樹中任何節(jié)點的值,而小于其右子樹中任何節(jié)點的值,這篇文章主要給大家介紹了關于Java數(shù)據(jù)結構篇之實現(xiàn)二叉搜索樹的核心方法,需要的朋友可以參考下2023-12-12
MyBatis 動態(tài) SQL 優(yōu)化之標簽的實戰(zhàn)與技巧(常見用法)
本文通過詳細的示例和實際應用場景,介紹了如何有效利用這些標簽來優(yōu)化 MyBatis 配置,提升開發(fā)效率,確保 SQL 的高效執(zhí)行和安全性,感興趣的朋友跟隨小編一起看看吧2025-04-04
java使用JDBC動態(tài)創(chuàng)建數(shù)據(jù)表及SQL預處理的方法
這篇文章主要介紹了java使用JDBC動態(tài)創(chuàng)建數(shù)據(jù)表及SQL預處理的方法,涉及JDBC操作數(shù)據(jù)庫的連接、創(chuàng)建表、添加數(shù)據(jù)、查詢等相關實現(xiàn)技巧,需要的朋友可以參考下2017-08-08
Java開發(fā)如何把數(shù)據(jù)庫里的未付款訂單改成已付款
這篇文章主要介紹了Java開發(fā)如何把數(shù)據(jù)庫里的未付款訂單改成已付款,先介紹MD5算法,簡單的來說,MD5能把任意大小、長度的數(shù)據(jù)轉換成固定長度的一串字符,實現(xiàn)思路非常簡單需要的朋友可以參考下2022-11-11

