Spring整合Mybatis具體代碼實現(xiàn)流程
原始方式讀取mybatis配置文件,獲取SqlSession SqlSessionFactory 等
package com.atguigu.rj1192.zyk; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.util.List; public class TestMybatis { public static void main(String[] args) throws IOException { //從配置文件中構建SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //創(chuàng)建一個SqlSession對象(獲取自動事務管理的session) SqlSession session = sqlSessionFactory.openSession(true); //獲取Mapper對象(反射,設計模式之代理模式) UserMapper mapper = session.getMapper(UserMapper.class); User user= new User(); user.setUsername("sdfs"); user.setPassword("123456"); user.setPhone("123456578909"); user.setStatus(1); mapper.insert(user); System.out.println(mapper.selectById(3)); } }
這種方式就是將 SqlSession SqlSessionFactory等類,全部用bean標簽放到ioc容器中,
如果這樣的話,我只能從ioc中獲得到sqlsession,要使用接口的方法,還需要getbean(),不方便,可以再加一個類,它去getbean(),并把這個類放進ioc容器中,使用方法的時候直接從ioc中拿這個類,再.方法名就行了
這個新的類,有mapper生成的sqlsession,sqlsession中有mapper的所有的方法,所以這個類中要有sqlsession的所有方法,即實現(xiàn) 接口(有點繞,就是為了調(diào)用這個類的時候,mapper中的所有方法都可以調(diào)用)
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- dataSource 使用spring的數(shù)據(jù)源替換mybatis的連接池 還可以用c3p0 dbcp--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/account?serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="mysql"></property> </bean> <!-- sqlsessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 綁定mybatis的配置文件--> <!-- 這個文件可以寫別名,和綁定的mapper 但是為了方便管理,這兩項單獨寫個mybatis-config.xml 再導入該文件即可--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- sqlsessionTemplate 就是我們用的sqlsession--> <bean id="sqlsession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 因為沒有set方法,只能使用構造器注入--> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <bean id="accountdaoimpl" class="com.atguigu.rj1192.zyk.dao.AccoountDaoImpl"> <property name="sqlSession" ref="sqlsession"></property> </bean> </beans>
新加的類
package com.atguigu.rj1192.zyk.dao; import com.atguigu.rj1192.zyk.pojo.Account; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class AccoountDaoImpl implements AccountDao { public SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<Account> selectall() { AccountDao accountDao= sqlSession.getMapper(AccountDao.class); return accountDao.selectall(); } }
import com.atguigu.rj1192.zyk.dao.AccountDao; import com.atguigu.rj1192.zyk.pojo.Account; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; import java.io.InputStream; import java.util.List; public class test { @Test public void query() throws IOException { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-dao.xml"); AccountDao accountadoimpl = (AccountDao) applicationContext.getBean("accountdaoimpl"); System.out.println(accountadoimpl.selectall()); } }
第二種方法 那個新的類 繼承SqlSessionDaoSupport,配置文件就可以只配置sqlSessionFactory和datasource,
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- dataSource 使用spring的數(shù)據(jù)源替換mybatis的連接池 還可以用c3p0 dbcp--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/account?serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="mysql"></property> </bean> <!-- sqlsessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 綁定mybatis的配置文件--> <!-- 這個文件可以寫別名,和綁定的mapper 但是為了方便管理,這兩項單獨寫個mybatis-config.xml 再導入該文件即可--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> 將新的類放進ioc容器中 <bean id="AccoountDaoImpl2" class="com.atguigu.rj1192.zyk.dao.AccoountDaoImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
package com.atguigu.rj1192.zyk.dao; import com.atguigu.rj1192.zyk.pojo.Account; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; public class AccoountDaoImpl2 extends SqlSessionDaoSupport implements AccountDao{ @Override public List<Account> selectall() { // 和第一種是一樣的,只是將賦值寫在了 SqlSessionDaoSupport類中,不用自己在xml中賦值了 //getSqlSession();父類中的方法 SqlSession sqlSession=getSqlSession(); AccountDao accountDao=sqlSession.getMapper(AccountDao.class); return accountDao.selectall(); } }
調(diào)用是一樣的
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class test { @Test public void query() throws IOException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml"); AccoountDaoImpl2 accountadoimpl = (AccoountDaoImpl2) applicationContext.getBean("AccoountDaoImpl2"); System.out.println(accountadoimpl.selectall()); } }
到此這篇關于Spring整合Mybatis具體代碼實現(xiàn)流程的文章就介紹到這了,更多相關Spring整合Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- springboot整合mybatis的超詳細過程(配置模式+注解模式)
- 解析整合mybatis-spring需要的maven依賴配置問題
- springboot mybatis druid配置多數(shù)據(jù)源教程
- spring boot下mybatis配置雙數(shù)據(jù)源的實例
- springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決
- SpringBoot配置MyBatis-Plus實現(xiàn)增刪查改
- 使用Spring掃描Mybatis的mapper接口的三種配置
- mybatis spring配置SqlSessionTemplate的使用方式
- SpringBoot集成Mybatis+xml格式的sql配置文件操作
相關文章
一個applicationContext 加載錯誤導致的阻塞問題及解決方法
這篇文章主要介紹了一個applicationContext 加載錯誤導致的阻塞問題及解決方法,需要的朋友可以參考下2018-11-11springboot學習筆記之 profile多環(huán)境配置切換的實現(xiàn)方式
這篇文章主要介紹了springboot profile多環(huán)境配置切換的實現(xiàn)方式,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07深入探究一下Java中不同的線程間數(shù)據(jù)通信方式
這篇文章主要來和大家一起深入探究一下Java中不同的線程間數(shù)據(jù)通信方式,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2023-04-04springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼
這篇文章主要介紹了springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Java?實現(xiàn)使用Comparable按照我們指定的規(guī)則排序
這篇文章主要介紹了Java?如何使用Comparable按照我們指定的規(guī)則排序,通過練習創(chuàng)建TreeSet集合使用無參構造方法,并按照年齡從小到大的順序排序,若年齡相同再按照姓名的字母順序排序展開內(nèi)容,需要的朋友可以參考一下2022-04-04