iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring整合Mybatis具体代码实现流程
  • 559
分享到

Spring整合Mybatis具体代码实现流程

2024-04-02 19:04:59 559人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录原始方式读取mybatis配置文件,获取sqlSession SqlSessionFactory 等 package com.atguigu.rj1192.zyk; import

原始方式读取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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Spring整合Mybatis具体代码实现流程

本文链接: https://www.lsjlt.com/news/148858.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作