您现在的位置是:首页 > 编程 > 

SpringBoot整合MyBatis

2025-07-20 00:36:13
SpringBoot整合MyBatis SpringBoot整合MyBatis创建项目准备工作pom.xml代码语言:javascript代码运行次数:0运行复制<?xml version="1.0" encoding="UTF-8"?> <project xmlns=".0.0" xmlns:xsi=";

SpringBoot整合MyBatis

SpringBoot整合MyBatis

创建项目

准备工作

pom.xml
代码语言:javascript代码运行次数:0运行复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0" xmlns:xsi=";
         xsi:schemaLocation=".0.0 .0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId></groupId>
    <artifactId>SpringBootMybatis</artifactId>
    <version>0.0.1-SAPSHOT</version>
    <name>SpringBootMybatis</name>
    <description>SpringBootMybatis</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>.0.0</version>
        </dependency>

        <dependency>
            <groupId></groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
yml

application.yml

代码语言:javascript代码运行次数:0运行复制
# 数据库配置信息
spring:
  datasource:
    driver-class-name: .cj.jdbc.Driver
    url: jdbc:mysql://localhost:06/mybatis
    username: root
    password: 12456
数据库

数据库建表文件

代码语言:javascript代码运行次数:0运行复制
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(
    -- id 主键
    id           int primary key auto_increment,
    -- 品牌名称
    brand_name   varchar(20),
    -- 企业名称
    company_name varchar(20),
    -- 排序字段
    ordered      int,
    -- 描述信息
    description  varchar(100),
    -- 状态:0:禁用  1:启用
    status       int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
       ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
       ('小米', '小米科技有限公司', 50, 'are you ok', 1);
Book类
代码语言:javascript代码运行次数:0运行复制
package com.itheima.domain;

public class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
         = type;
    }

    public String getame() {
        return name;
    }

    public void setame(String name) {
         = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
BookDao

文件结构

代码语言:javascript代码运行次数:0运行复制
package com.itheima.dao;

import .springbootmybatis.domain.Book;
import org.apache.ibatis.annotati.Mapper;
import org.apache.ibatis.annotati.Select;

@Mapper
public interface BookDao {
    @Select("select * from tb_brand where id = #{id}")
    public Book getById(Integer id);
}
测试类

Test类

代码语言:javascript代码运行次数:0运行复制
@SpringBootTest
class Springboot05MybatisApplicationTests {

    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        println(bookDao.getById(1));
    }

}

运行结果

合并集合

一共有 n 个数,编号是 1∼n,最开始每个数各自在一个集合中。

现在要进行 m 个操作,操作共有两种:

M a b,将编号为 a 和 b 的两个数所在的集合合并,如果两个数已经在同一个集合中,则忽略这个操作; Q a b,询问编号为 a 和 b 的两个数是否在同一个集合中; 输入格式 第一行输入整数 n 和 m。

接下来 m 行,每行包含一个操作指令,指令为 M a b 或 Q a b 中的一种。

输出格式 对于每个询问指令 Q a b,都要输出一个结果,如果 a 和 b 在同一集合内,则输出 Yes,否则输出 o。

每个结果占一行。

数据范围 1≤n,m≤105 输入样例: 4 5 M 1 2 M 4 Q 1 2 Q 1 Q 4 输出样例: Yes o Yes

提交代码

代码语言:javascript代码运行次数:0运行复制
#include<iostream>
using namespace std;

ct int  = 100010;
int n, m;
int p[];

int find(int x)                 // 到x的祖先节点
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) p[i] = i;
    
    while (m--)
    {
        char op;
        int a, b;
        scanf (" %c%d%d", &op, &a, &b);
        if (op == 'M') p[p[find(a)]] = find(b);        // 让a的祖先节点指向b的祖先节点
        else
        {
            if (find(a) == find(b)) puts("Yes");
            else puts("o");
        }
    }
    return 0;
}
代码语言:javascript代码运行次数:0运行复制
import java.io.*;

public class Main
{
    static int  = 100010;
    static int n, m;
    static int [] p = new int [];
    
    static int find(int x)
    {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
    
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));   
        String [] str = reader.readLine().split(" ");
        n = Integer.parseInt(str[0]);
        m = Integer.parseInt(str[1]);
        
        for (int i = 1; i <= n; ++ i) p[i] = i;
        while (m -- > 0)
        {
            String op;
            int a, b;
            str = reader.readLine().split(" ");
            op = str[0];
            a = Integer.parseInt(str[1]);
            b = Integer.parseInt(str[2]);
            if (("M")) p[find(a)] = find(b);
            else 
            {
                if (find(a) == find(b)) println("Yes");
                else println("o");
            }
        }
    }
}
滑动窗口

给定一个大小为 n≤106 的数组。

有一个大小为 k 的滑动窗口,它从数组的最左边移动到最右边。

你只能在窗口中看到 k 个数字。

每次滑动窗口向右移动一个位置。

以下是一个例子:

该数组为 [1 -1 - 5 6 7],k 为 。

窗口位置 最小值 最大值 [1 -1] - 5 6 7 -1 1 [ -1 -] 5 6 7 - 1 [-1 - 5] 6 7 - 5 1 -1 [- 5 ] 6 7 - 5 1 -1 - [5 6] 7 6 1 -1 - 5 [ 6 7] 7 你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。

输入格式 输入包含两行。

第一行包含两个整数 n 和 k,分别代表数组长度和滑动窗口的长度。

第二行有 n 个整数,代表数组的具体数值。

同行数据之间用空格隔开。

输出格式 输出包含两个。

第一行输出,从左至右,每个位置滑动窗口中的最小值。

第二行输出,从左至右,每个位置滑动窗口中的最大值。

输入样例: 8 1 -1 - 5 6 7 输出样例: -1 - - - 5 5 6 7

提交代码

C++

代码语言:javascript代码运行次数:0运行复制
#include<iostream>
using namespace std;

ct int  = 1000010;
int a[], q[], hh, tt = -1;

int main()
{
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; ++ i)    // 这个题要注意的是 q队列里面存放的是位置
    {
        scanf ("%d", &a[i]);        // 先求的是最小值
        if (i - k + 1 > q[hh]) ++hh;  // 如果最小值的位置已经滑出窗口了 然后就
                                    // ++ hh代表这个数已经没了
        while (hh <= tt && a[i] <= a[q[tt]]) -- tt; // 先确保队列里面有数字
                                    // 然后如果新来的数字要小于 队列里面的最小值
                                    // 那么--tt 就代表当前队列的最小值去掉
        q[++ tt] = i;  // 把新来的数字放到队列中
        if (i + 1 >= k) printf ("%d ", a[q[hh]]); // 当前队列的长度已经满足k了
                                    // 就可以把对首的元素输出出来
    }
    puts("");
    int hh = 0, tt = -1;
    for (int i = 0; i < n; ++ i)
    {
        if (i - k + 1 > q[hh]) ++ hh;
        while (hh <= tt && a[i] >= a[q[tt]]) -- tt;
        q[++ tt] = i;
        if (i + 1 >= k) printf("%d ", a[q[hh]]);
    }
    return 0;
}

Java

代码语言:javascript代码运行次数:0运行复制
import java.io.*;

public class Main
{
    final static int  = 1000010;
    static int [] a = new int [];
    static int [] q = new int [];
    static int hh = 0, tt = -1;
    public static void main(String[] args) throws IOException
    {
        int n, k;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter());
        
        String [] str = reader.readLine().split(" ");
        n = Integer.parseInt(str[0]);
        k = Integer.parseInt(str[1]);
        
        str = reader.readLine().split(" ");
        for (int i = 0; i < n; ++ i) a[i] = Integer.parseInt(str[i]);
        
        // for (int i = 0; i < n; ++ i)
        // {
        //     if (hh <= tt && i - k + 1 > q[hh])  ++ hh;
        //     while (hh <= tt && a[i] <= a[q[hh]]) -- tt;
        //     q[++ tt] = i;
        //     if (i + 1 >= k) out.write(a[q[hh]]+" ");
        // }
        
        for(int i = 0; i < n; i ++)
        {
            if(hh <= tt && i - q[hh] + 1 > k) hh++;//判断队头是否已经滑出窗口
            while(hh <= tt && a[q[tt]] >= a[i]) tt--;//出队

            q[++tt] = i;//入队
            if(i >= k - 1) out.write(a[q[hh]]+" ");
        }
        
        out.write("\n");
        hh = 0;
        tt = -1;
        // for (int i  = 0; i < n; ++ i)
        // {
        //     if (hh <= tt && i - k + 1 > q[hh]) ++ hh;
        //     while (hh <= tt && a[i] >= a[q[hh]]) -- tt;
        //     q[++ tt] = i;
        //     if (i + 1 >= k) out.write(a[q[hh]]+" ");
        // }
        for(int i = 0; i < n; i ++)
        {
            if(hh <= tt && i - q[hh] + 1 > k) hh++;//判断队头是否已经滑出窗口
            while(hh <= tt && a[q[tt]] <= a[i]) tt--;//出队

            q[++tt] = i;//入队
            if(i >= k - 1) out.write(a[q[hh]]+" ");
        }
        out.flush();
        ();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2022-12-29,如有侵权请联系 cloudcommunity@tencent 删除mybatisint队列集合数组

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1149751.html

相关标签:无
上传时间: 2025-07-19 20:26:55
留言与评论(共有 10 条评论)
本站网友 陈虎简历
17分钟前 发表
则忽略这个操作; Q a b
本站网友 新华书店九月网
10分钟前 发表
如有侵权请联系 cloudcommunity@tencent 删除前往查看mybatisint队列集合数组
本站网友 kk棋牌
17分钟前 发表
//localhost
本站网友 淄川二手房
21分钟前 发表
窗口中的最大值和最小值
本站网友 北方往事
3分钟前 发表
'华为技术有限公司'
本站网友 上海晨光文具
0秒前 发表
'华为技术有限公司'
本站网友 心跳一百
19分钟前 发表
datasource
本站网友 破铜钱草
5分钟前 发表
分享自作者个人站点/博客
本站网友 台式机重装系统
7分钟前 发表
schemaLocation=".0.0 .0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId></groupId> <artifactId>SpringBootMybatis</artifactId> <version>0.0.1-SAPSHOT</version> <name>SpringBootMybatis</name> <description>SpringBootMybatis</description> <properties> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>.0.0</version> </dependency> <dependency> <groupId></groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>yml application.yml代码语言:javascript代码运行次数:0运行复制# 数据库配置信息 spring