Spring-mybatis

 怎样通过Spring整合Mybatis来实现业务

目录

1.导入依赖

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.13</version>
        </dependency>
        <!--用Spring操作数据库,还需要一个spring-jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.0.13</version>
        </dependency>
        <!--AOP织入-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.2</version>
        </dependency>
    </dependencies>

2. spring配置文件

spring整合mybatis之后,就不再需要mybatis的配置文件

<?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
		https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--Spring整合Mybatis,省略了Mybatis的核心配置文件,转而在Spring的配置文件中配置Mybatis-->
    <!--datasource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/user?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/sun/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate:就是我们使用的sqlSession,这个是spring提供的-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--我们只能使用构造器注入,因为没有set方法-->
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

 上面的配置文件是固定的,配置文件需要三个bean

  • dataSource                                     用来连接数据库
  • sqlSessionFactory                        用来创建sqlSession
  • sqlSession                                      用来创建sqlSession的bean

 3.编写实体类(原mybatis的步骤)

public class User {
    private int id;
    private String name;
    private int age;

}

4.编写接口(原mybatis的步骤)

public interface UserMapper {

   public List<User> selectUser();

}

5.编写xml映射文件(原mybatis的步骤)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sun.mapper.UserMapper">
    <select id="selectUser" resultType="user">
        select * from user.tb_user
    </select>
</mapper>

6.编写接口实例

public class UserMapperImpl implements UserMapper{

    //之前使用Mybatis时候,我们用的是sqlSession,现在用Spring整合Mybatis,我们使用Spring提供的SqlSessionTemplate(相当于sqlSession)
    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
    @Override
    public List<User> selectUser() {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}

 

7.将实例注册到spring中

<bean id="UserMapperImpl" class="com.sun.mapper.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>

8.测试

直接获取实例的bean即可

public class MyTest {

    @Test
    public void selectTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        UserMapper userMapperImpl = (UserMapper) context.getBean("UserMapperImpl");
        List<User> users = userMapperImpl.selectUser();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

总的来说,使用Spring整合Mybatis,思想还是不变,只不过形式是采用了Spring的ioc容器

9.第二种方式

  • Spring提供了一个SqlSessionDaoSupport类,可以通过getSqlSession方法直接获取SqlSession,原本我们是通过SqlSessionTemplate作为中间人,现在通过这个方法我们不再需要SqlSessionTemplate
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{

    //这个类继承了SqlSessionDaoSupport,可以直接get一个SqlSession
    @Override
    public List<User> selectUser() {
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}

 将这个类放到Spring中:

  <bean id="UserMapperImpl2" class="com.sun.mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

测试: 

   @Test
    public void selectTest2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        UserMapper userMapperImpl2 = (UserMapper) context.getBean("UserMapperImpl2");
        List<User> users = userMapperImpl2.selectUser();
        for (User user : users) {
            System.out.println(user);
        }
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/366152.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

化工企业能源在线监测管理系统,能源管理新利器

化工企业在开展化工生产活动时&#xff0c;能源消耗量较大&#xff0c;其节能潜力空间也较大&#xff0c;因此必须控制能耗强度&#xff0c;促进能效水平的稳步提升。化工企业通过能源现状的分析&#xff0c;能够实现能源使用情况的实时反馈与监管&#xff0c;从而达到节能减排…

MobPush:Android SDK 集成指南

开发工具&#xff1a;Android Studio 集成方式&#xff1a;Gradle在线集成 安卓版本支持&#xff1a;minSdkVersion 19 集成前准备 注册账号 使用PushSDK之前&#xff0c;需要先在MobTech官网注册开发者账号&#xff0c;并获取MobTech提供的AppKey和AppSecret&#xff0c;…

搜狗开源框架Workflow网络模型分析

workflow是一个比较轻量化的后端服务框架&#xff0c;支持Linux/Mac/Windows主流平台&#xff0c;其网络模块是框架的核心。在workflow-windows分支上可以看到对windows的IOCP的封装&#xff0c;对于学习windows IOCP网络编程有很好的启发意义。因此&#xff0c;有必要对该网络…

第二十一回 阎婆大闹郓城县 朱仝义释宋公明-FreeBSD Linux 使用Rsync备份

阎婆状告宋江杀死她女儿阎婆惜&#xff0c;知县有意偏袒宋江&#xff0c;只是一味的拷打唐牛儿&#xff0c;但无奈张三张文远说刀子是宋江的&#xff0c;知县不得已差人拿宋江来审问。第一次没见到人&#xff0c;第二次派朱仝雷横两个人去。 朱仝到地窖里找到了躲藏的宋江&…

数字电源环路补偿(2)

上一篇数字电源环路补偿&#xff08;1&#xff09;-CSDN博客介绍了数字电源的环路设计的基本原理&#xff0c;并用了一个一型补偿器作为例子把LLC控得还行。 那么问题来了&#xff0c;一型补偿器好是好&#xff0c;它设计方便&#xff0c;结构简单&#xff0c;高效粗暴&#x…

2024美赛数学建模E题思路源码

赛题目的 可以将其拆解为以下主要问题&#xff0c;并为每个问题提出解决方案&#xff1a; 如何在极端天气事件越来越多的地区部署财产保险&#xff1f; 保险公司应在何时何地承保保单&#xff1f; 业主如何影响保险公司的承保决定&#xff1f; 如何建立能够评估未来房地产决…

基于Springboot的高校心理教育辅导设计与实现(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的高校心理教育辅导设计与实现(有报告)。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;…

C# 引用同一个dll不同版本的程序集

因为项目需要所以必须在项目中引用不同版本的同一程序集 我要引用的文件是newtonsoft.json.dll 两个版本为12.0.0.0 和4.0.0.0 1.如果已经先引入了newtonsoft.json 12.0.0.0版本的程序集&#xff0c;如果直接引入另一个版本的程序集的话会提示不成功&#xff0c;所以先将另一个…

PyTorch基础-Tensors属性、Tensor的运算

PyTorch的基本概念 Tensor的基本概念 张量高于标量、向量、矩阵 标量说零维的张量&#xff0c;向量是一维的张量&#xff0c;矩阵是二维的张量 Tensor与机器学习的关系 Tensor的创建 函数功能Tensor(*size)基础构造函数Tensor(data)类似np.arrayones(*size)全1Tensorzeros(…

VPP学习-startup.conf配置文件

背景 VPP&#xff08;Vector Packet Processing&#xff0c;矢量报文处理&#xff09;&#xff0c;作为一个开源的高性能数据包处理框架&#xff0c;旨在提供可扩展、灵活且高效的网络数据包处理能力&#xff1b;由于传统Linux 内核协议栈整体网络吞吐性能的局限性&#xff0c;…

【PTA浙大版《C语言程序设计(第4版)》编程题】练习7-4 找出不是两个数组共有的元素(附测试点)

目录 输入格式: 输出格式: 输入样例: 输出样例: 代码呈现 测试点 给定两个整型数组&#xff0c;本题要求找出不是两者共有的元素。 输入格式: 输入分别在两行中给出两个整型数组&#xff0c;每行先给出正整数N&#xff08;≤20&#xff09;&#xff0c;随后是N个整数&a…

SpringMVC请求和响应

文章目录 1、请求映射路径2、请求参数3、五种类型参数传递3.1、普通参数3.2、POJO类型参数3.3、嵌套POJO类型参数3.4、数组类型参数3.5、集合类型参数 4、json数据传递4.1、传递json对象4.2、传递json对象数组 5、日期类型参数传递6、响应6.1、响应页面6.2、文本数据6.3、json数…

Spring速成(三)

文章目录 Spring速成&#xff08;三&#xff09;1&#xff0c;AOP简介1.1 什么是AOP?1.2 AOP作用1.3 AOP核心概念 2&#xff0c;AOP入门案例2.1 需求分析2.2 思路分析2.3 环境准备2.4 AOP实现步骤步骤1:添加依赖步骤2:定义接口与实现类步骤3:定义通知类和通知步骤4:定义切入点…

刨析数据结构(二)

&#x1f308;个人主页&#xff1a;小田爱学编程 &#x1f525; 系列专栏&#xff1a;数据结构————"带你无脑刨析" &#x1f3c6;&#x1f3c6;关注博主&#xff0c;随时获取更多关于数据结构的优质内容&#xff01;&#x1f3c6;&#x1f3c6; &#x1f600;欢迎…

d3dcompiler_47.dll是什么文件,如何解决找不到d3dcompiler_47.dll文件

d3dcompiler_47.dll是一个属于微软DirectX组件的动态链接库文件&#xff0c;它是Direct3D编译器的一部分&#xff0c;主要用于在Windows操作系统中编译和运行Direct3D 11应用程序和游戏。当你在电脑上尝试打开某些依赖Direct3D图形技术的软件或游戏时&#xff0c;如果系统找不到…

Flume搭建

压缩包版本&#xff1a;apache-flume-1.9.0-bin.tar 百度盘链接&#xff1a;https://pan.baidu.com/s/1ZhSiePUye9ax7TW5XbfWdw 提取码&#xff1a;ieks 1.解压 tar -zxvf /opt/software/apache-flume-1.9.0-bin.tar.gz -C /opt/module/ 2. 修改文件名 [rootbigdata1 opt]…

基于SpringBoot Vue超市管理系统

大家好✌&#xff01;我是Dwzun。很高兴你能来阅读我&#xff0c;我会陆续更新Java后端、前端、数据库、项目案例等相关知识点总结&#xff0c;还为大家分享优质的实战项目&#xff0c;本人在Java项目开发领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目&#x…

Codeforces Round 169 (Div. 2) D 异或最大值 二进制

题目&#xff1a; Problem - 276D - Codeforces 思路&#xff1a; l < r 1. l r &#xff0c;异或的结果为0 2. l < r &#xff08;注意&#xff1a;二进制位中&#xff0c;某一位是1,这一位后面的全是1&#xff0c;也没这个1大&#xff0c;比如1000 > 0111&…

HCIP-Datacom(H12-821)81-90题解析

有需要完整题库的同学可以私信博主&#xff0c;博主看到会回复将文件发给你&#xff01;&#xff08;麻烦各位同学给博主推文点赞关注和收藏哦&#xff09; 81、在状态检测防火墙中&#xff0c;开启状态检测机制时&#xff0c;三次握手的第二个报文(SYNACK)到达防火墙的时候如…

最新!2024顶级SCI优化!TTAO-CNN-BiGRU-MSA三角拓扑聚合优化、双向GRU融合注意力的多变量回归预测程序!

适用平台&#xff1a;Matlab 2023版及以上 TTOA三角聚合优化算法&#xff0c;将在2024年3月正式发表在中科院1区顶级SCI期刊《Expert Systems with Applications》上。 该算法提出时间极短&#xff0c;目前以及近期内不会有套用这个算法的文献。新年伊始&#xff0c;尽快拿下…
最新文章