首页 > 其他分享 >【spring笔记】Spring整合Mybaties

【spring笔记】Spring整合Mybaties

时间:2023-02-28 11:24:17浏览次数:42  
标签:Mybaties Spring spring StudentMapper sqlSession org import public

1、实现方式1

核心思想:利用Spring配置依赖注入创建sqlSessionFactory和sqlSession实例

  • 需要的包如下:

1.1、编写数据源

这个很简单,就是spring在包装了一下配置参数

1.2、创建sqlSessionFactory

对比Mybatis就是不用自己new对象了,框架都写好了,用配置问价注入容器

1.3、创建sqlSessionTemplate

其实就是创建一个sqlSession

1.4、给接口加实现类

为什么这里需要实现类呢?因为Mybatis不能自动生成类对象,所以需要我们手动写一个实现类,然后将上面的sqlSessioinTemplate注入进去

package com.wcy.mapper;

import com.wcy.pojo.Student;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentMapperImpl implements StudentMapper {

private SqlSessionTemplate sqlSession;

public void setSqlSession(SqlSessionTemplate sqlSession) {
    this.sqlSession = sqlSession;
}

@Override
public List<Student> getStudentList() {
    StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
    return studentMapper.getStudentList();
}

}

1.5、将自己写的实现类,注入到Spring中

1.6、测试

public class MyTest_spring_Mybatis {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        StudentMapper studentMapper = context.getBean("studentMapper", StudentMapper.class);
        for(Student student : studentMapper.getStudentList()) {
            System.out.println(student.toString());
        }
    }
}

测试过程还发现一个问题
java.lang.NoSuchMethodError: org.springframework.beans.factory.config.BeanDefinition.get ResolvableType()Lorg/springframework/core/ResolvableType;
最后发现是springweb-mvc和sping-jdbc版本不一致导致的,统一就好了

2、实现方式2

方式二其实就是,sqlSession通过spring提供的类直接get出来

官网描述如下:

2.1、实现接口的类

package com.wcy.mapper;


import com.wcy.pojo.Student;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;


import java.util.List;


public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper{


    @Override
    public List<Student> getStudentList() {
        return getSqlSession().getMapper(StudentMapper.class).getStudentList();
    }
}

配置参数里,因为是继承了SqlSessionDaoSupport类,所以直接注入sqlSessionFactory就好了

SqlSessionDaoSupport源码:

package org.mybatis.spring.support;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.support.DaoSupport;
import org.springframework.util.Assert;


public abstract class SqlSessionDaoSupport extends DaoSupport {
    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionDaoSupport() {
    }

    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        if (this.sqlSessionTemplate == null || sqlSessionFactory != this.sqlSessionTemplate.getSqlSessionFactory()) {
            this.sqlSessionTemplate = this.createSqlSessionTemplate(sqlSessionFactory);
        }
    }
}

<bean id="studentMapper2" class="com.wcy.mapper.StudentMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

标签:Mybaties,Spring,spring,StudentMapper,sqlSession,org,import,public
From: https://www.cnblogs.com/wcyblogs/p/17163328.html

相关文章

  • 【spring笔记】Spring声明事务
    前情提要:事物在Mysql数据库中已经学过,具有ACID的特性1、Spring事物管理分为两类:声明式事物:AOP编程式事物:需要在代码中,进行事物的管理编程式事物还是没有AOP的统一处理......
  • 【spring笔记】Mybaties入门
    1、官方文档https://mybatis.org/mybatis-3/zh/getting-started.html2、搭建一个Mybatis实例2.1首先看配置文件mybatis-config.xml这里面包括获取数据库连接实例的数......
  • springboot处理乱码问题原理
    我们在用spring-springmvc时,需要配置一个过滤器 CharacterEncodingFilterCharacterEncodingFilterfilter=newOrderedCharacterEncodingFilter();filter.setEncodin......
  • 学习springCloud的配置之Swagger的配置
    文章来源于:https://www.bbsmax.com/A/1O5E3VP4z7/本文仅做学习记录用途SpringCloud配置中心采用数据库存储配置内容转自:SpringCloudConfig采用数据库存储配置内容【......
  • springboot集成easyexcel(阿里)
    poi比较占用内存。easyexcel性能优化不少,值得一看。pom.xml中添加:<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.6</......
  • springboot中json参数映射
    文章目录​​json映射不到实体上​​​​问题分析​​​​解决方案​​​​json映射到String查不到数据​​​​问题分析​​​​解决方案​​​​自动映射入参可以多吗,可以......
  • 转载:pageOffice插件 springboot实现服务器上Word文档在线打开编辑保存
    pageOffice插件springboot实现服务器上Word文档在线打开编辑保存需求:在oa系统上,想实现在线,服务器上doc,docx文档,在web打开,编辑。编辑后,可以再同步保存到服务器端。......
  • Spring不同版本的AOP
    1、Spring4、SpringBoot11.1代码实现publicinterfaceCalculator{intdiv(inta,intb);}@ComponentpublicclassCalculatorImplimplementsCalcul......
  • SpringBoot项目结构
    一.总体概述SpringBoot框架简单来说就是一个Web框架,其目的在于快速开发一个Web应用的后端实现主要包括三部分:java文件夹,Resource文件夹,pom.xml二.Java文件夹Co......
  • springboot+mybatis-plus数据库mysql+sybase遇到的一些问题
    我们服务启动时,sybase数据库连接直接创建10个连接。(为什么启动时会创建这么多连接?)有时候可以写入sybase库,大部分写入失败查询sybase库数据可以查出来,没问题尝试的方......