Spring整合Mybatis具體代碼實現流程

原始方式讀取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);
        //創建一個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的所有方法,即實現 接口(有點繞,就是為瞭調用這個類的時候,mapper中的所有方法都可以調用)

<?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的數據源替換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的數據源替換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();
    }
}

調用是一樣的

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具體代碼實現流程的文章就介紹到這瞭,更多相關Spring整合Mybatis內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: