Spring5框架八:整合Mybatis

精心整理了最新的面试资料,有需要的可以自行获取

点击前往百度网盘获取
点击前往夸克网盘获取
1、导入相关的jar包

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
        <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</dependencies>

2、创建一个实体类

import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}

3、创建一个接口

import java.util.List;

public interface UserMapper {
    List<User> selectUser();
}

4、创建接口的实现类

import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{

    private SqlSessionTemplate sqlSession;

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

5、创建一个与接口同名的xml文件

<?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.www.mapper.UserMapper">
    <select id="selectUser" resultType="User">
        select * from mybatis.user
    </select>

</mapper>

6、创建一个spring-config的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">


    <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/mybatis?useSSL=false&amp;serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>

        <property name="configLocation" value="classpath:Mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:com/www/mapper/*.xml"></property>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>
    

</beans>

7、创建一个Mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.www.pojo"/>
    </typeAliases>

    
</configuration>

8、创建一个applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <import resource="spring-dao.xml"></import>
    
    <bean id="userMapper2" class="com.www.mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

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


</beans>

9、创建测试类

import com.www.mapper.UserMapper;
import com.www.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.List;

public class Test {

    @org.junit.Test
    public void test() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = (UserMapper) context.getBean("userMapper");
        List<User> userList = mapper.selectUser();
        for (User user:userList
             ) {
            System.out.println(user);
        }
    }
}

10、结果
在这里插入图片描述

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

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

相关文章

AI学习第一天-什么是AI

AI的发展可以被分为四次浪潮&#xff0c;这包括符号主义、机器学习与神经网络&#xff0c;以及深度学习。在这些发展中&#xff0c;深度学习凭借其在处理非结构化复杂数据、强大的学习能力和可解释性方面的优势备受关注。深度学习技术的应用不仅提升了AI系统的性能&#xff0c;…

redis-bitmap使用场景

bitmap原理 Bitmap&#xff08;位图&#xff09;是一种基于二进制位的数据结构&#xff0c;用于高效地存储和操作大量的布尔值 可以对单个位进行读写操作 demo package org.example;import org.redisson.Redisson; import org.redisson.api.RBitSet; import org.redisson.ap…

华为 网络安全 认证

&#x1f345; 点击文末小卡片 &#xff0c;免费获取网络安全全套资料&#xff0c;资料在手&#xff0c;涨薪更快 华为 网络安全 认证&#xff1a;保障信息安全的重要一环 在数字化时代的今天&#xff0c;网络安全成为了企业和个人都需要高度重视的问题。尤其是在企业信息化的…

ubuntu22.04连接github无法访问的问题

目录 说明安装 说明 此方案只针对虚拟机, 如果是云服务器(毕竟是官方维护, github还是能访问到的)多试几次肯定能够访问到的. 国内我们无法访问外网, 所以我们目前能够访问外网的途径基本上只能开佳速器. 所以我们需要选择一款加速器来帮助我们访问外网, 目前市面上很多佳速器…

【Research Proposal】基于提示词方法的智能体工具调用研究——难点

博客主页&#xff1a; [小ᶻ☡꙳ᵃⁱᵍᶜ꙳] 本文专栏: AIGC | ChatGPT 文章目录 &#x1f4af;前言&#x1f4af;一、难点分析1. 提示词方法的多样性和组合问题2. 智能体工具调用的准确性和效率问题3. 多模态任务中的复杂性问题 &#x1f4af;二、解决思路与策略&#x1f…

Linux搭建Nginx直播流媒体服务RTMP/RTSP转Http-flv视频浏览器在线播放/Vue/Java/ffmpeg

参考文章&#xff1a; https://blog.csdn.net/whatareyouding/article/details/144317654 https://www.cnblogs.com/Gredae/p/18362900 https://www.cnblogs.com/kn-zheng/p/17422707.html https://blog.51cto.com/u_16099344/10281495 https://www.tulingxueyuan.cn/tlzx/jsp…

智慧教室与无纸化同屏技术方案探讨与实现探究

引言 随着教育信息化的不断发展&#xff0c;智慧教室和无纸化同屏技术逐渐成为提升教学效率和质量的重要手段。大牛直播SDK凭借其强大的音视频处理能力和丰富的功能特性&#xff0c;在智慧教室和无纸化同屏领域积累了众多成功案例。本文将深入探讨基于大牛直播SDK的智慧教室、…

自制操作系统前置知识汇编学习

今天要做什么&#xff1f; 为了更好的理解书中内容&#xff0c;需要学习下进制分析和汇编。 汇编语言其实应该叫叫机器指令符号化语言&#xff0c;目前的汇编语言是学习操作系统的基础。 一&#xff1a;触发器 电路触发器的锁存命令默认是断开的&#xff0c;是控制电路触发器…

嘉立创EDA一自画元件及其封装

目录 一、创建元件 &#xff08;1&#xff09;新建元件。 &#xff08;2&#xff09;绘制元件。 二、绘制封装 &#xff08;1&#xff09;新建封装。 &#xff08;2&#xff09;绘制封装。 三、关联元件与封装 四、封装设计注意事项 在嘉立创&#xff08;JLCPCB&#xff…

《深度学习实战》第2集-补充:卷积神经网络(CNN)与图像分类 实战代码解析和改进

以下是对《深度学习实战》第2集中 CIFAR-10 数据集 使用卷积神经网络进行图像分类实战 代码的详细分析&#xff0c;并增加数据探索环节&#xff0c;同时对数据探索、模型训练和评估的过程进行具体说明。所有代码都附上了运行结果配图&#xff0c;方便对比。 《深度学习实战》第…

dataframe如何在末尾添加多行

如果要在pandas的dataframe中添加多行该如何实现&#xff1f;可通过以下常见方式在DataFrame末尾添加&#xff1a; ### 方法一&#xff1a;使用loc索引器 利用loc索引器分两次操作来添加两行数据。假设已有DataFrame对象df&#xff0c;要添加的两行数据分别存储在字典new_row…

使用 DeepSeek 生成流程图、甘特图与思维导图:结合 Typora 和 XMind 的高效工作流

在现代工作与学习中&#xff0c;可视化工具如流程图、甘特图和思维导图能够极大地提升信息整理与表达的效率。本文将详细介绍如何使用 DeepSeek 生成 Mermaid 文本&#xff0c;结合 Typora 快速生成流程图和甘特图&#xff0c;并通过 Markdown 格式生成思维导图&#xff0c;最终…

插入排序(详解)c++

插⼊排序(Insertion Sort)类似于玩扑克牌插牌过程&#xff0c;每次将⼀个待排序的元素按照其关键字⼤⼩插⼊到前⾯已排好序的序列中&#xff0c;按照该种⽅式将所有元素全部插⼊完成即可 算法思想&#xff1a; 把待排序元素插入到已排序的序列中。想象一下一张一张整理扑克牌的…

【大模型】蓝耘智算云平台快速部署DeepSeek R1/R3大模型详解

目录 一、前言 二、蓝耘智算平台介绍 2.1 蓝耘智算平台是什么 2.2 平台优势 2.3 应用场景 2.4 对DeepSeek 的支持 2.4.1 DeepSeek 简介 2.4.2 DeepSeek 优势 三、蓝耘智算平台部署DeepSeek-R1操作过程 3.1 注册账号 3.1.1 余额检查 3.2 部署DeepSeek-R1 3.2.1 获取…

ai-financial-agent - 为金融投资打造的AI代理

探索人工智能在投资研究中的应用。本项目仅用于**教育**目的&#xff0c;不用于真实交易或投资。 作者声明&#xff1a; 本项目仅用于教育和研究目的。 不用于真实交易或投资不提供任何保证或担保过去的表现并不代表未来的结果Creator 对经济损失不承担任何责任咨询财务顾问…

基于keepalived的Nginx高可用架构

一、概述 Keepalived 是一个基于 VRRP&#xff08;Virtual Router Redundancy Protocol&#xff09;协议 的高可用性解决方案&#xff0c;为了解决静态路由器出现的单点故障问题&#xff0c;它能偶保证网络的不间断、稳定的运行。 二、核心功能 IP 漂移&#xff08;VIP&…

学术论文项目网站搭建教程【Github】

本教程使用的是linux系统&#xff0c;ubuntu20.04版本进行学术项目网站搭建 一&#xff1a;创建github的个人组织 我个人习惯使用自己的github组织【Your organizations】来进行学术项目网站的创建&#xff1a; New一个organization&#xff0c;点击Free中的Create a free o…

postman调用ollama的api

按照如下设置&#xff0c;不需要设置key 保持长会话的方法 # 首次请求 curl http://localhost:11434/api/generate -d {"model": "deepseek-r1:32b","prompt": "请永久记住&#xff1a;110&#xff0c;1-12&#xff0c;之后所有数学计算必…

【Linux】多线程 -> 线程同步与基于BlockingQueue的生产者消费者模型

线程同步 条件变量 当一个线程互斥地访问某个变量时&#xff0c;它可能发现在其它线程改变状态之前&#xff0c;它什么也做不了。 例如&#xff1a;一个线程访问队列时&#xff0c;发现队列为空&#xff0c;它只能等待&#xff0c;直到其它线程将一个节点添加到队列中。这…

ChatGPT各模型版本对比分析

文章目录 1. GPT-3.5&#xff08;2022年11月&#xff09;2. GPT-4&#xff08;2023年3月&#xff09;3. GPT-4o&#xff08;2024年5月&#xff09;4. GPT-4o mini&#xff08;2024年7月&#xff09;5. o1系列&#xff08;2024年9月至12月&#xff09;6. o3-mini&#xff08;202…