欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring整合Mybatis具體代碼實(shí)現(xiàn)流程

 更新時(shí)間:2022年05月16日 09:04:01   作者:仰望星空的快樂(lè)  
這篇文章主要介紹了Spring整合Mybatis實(shí)操分享,文章首先通過(guò)介紹Mybatis的工作原理展開(kāi)Spring整合Mybatis的詳細(xì)內(nèi)容,需要的小伙伴可以參考一下

原始方式讀取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 {
        //從配置文件中構(gòu)建SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //創(chuàng)建一個(gè)SqlSession對(duì)象(獲取自動(dòng)事務(wù)管理的session)
        SqlSession session = sqlSessionFactory.openSession(true);
        //獲取Mapper對(duì)象(反射,設(shè)計(jì)模式之代理模式)
        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等類(lèi),全部用bean標(biāo)簽放到ioc容器中,

如果這樣的話,我只能從ioc中獲得到sqlsession,要使用接口的方法,還需要getbean(),不方便,可以再加一個(gè)類(lèi),它去getbean(),并把這個(gè)類(lèi)放進(jìn)ioc容器中,使用方法的時(shí)候直接從ioc中拿這個(gè)類(lèi),再.方法名就行了

這個(gè)新的類(lèi),有mapper生成的sqlsession,sqlsession中有mapper的所有的方法,所以這個(gè)類(lèi)中要有sqlsession的所有方法,即實(shí)現(xiàn) 接口(有點(diǎn)繞,就是為了調(diào)用這個(gè)類(lèi)的時(shí)候,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的配置文件-->
        <!--      這個(gè)文件可以寫(xiě)別名,和綁定的mapper
           但是為了方便管理,這兩項(xiàng)單獨(dú)寫(xiě)個(gè)mybatis-config.xml 再導(dǎo)入該文件即可-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!--    sqlsessionTemplate  就是我們用的sqlsession-->
    <bean id="sqlsession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--        因?yàn)闆](méi)有set方法,只能使用構(gòu)造器注入-->
        <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>

新加的類(lèi)

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());
    }
}

第二種方法 那個(gè)新的類(lèi) 繼承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的配置文件-->
        <!--      這個(gè)文件可以寫(xiě)別名,和綁定的mapper
           但是為了方便管理,這兩項(xiàng)單獨(dú)寫(xiě)個(gè)mybatis-config.xml 再導(dǎo)入該文件即可-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
     將新的類(lèi)放進(jìn)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() {
//    和第一種是一樣的,只是將賦值寫(xiě)在了 SqlSessionDaoSupport類(lèi)中,不用自己在xml中賦值了
//getSqlSession();父類(lèi)中的方法
        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());
    }
}

到此這篇關(guān)于Spring整合Mybatis具體代碼實(shí)現(xiàn)流程的文章就介紹到這了,更多相關(guān)Spring整合Mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論