SpringBoot整合MyBatis
创建项目
准备工作
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.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>com.example</groupId> <artifactId>SpringBootMybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootMybatis</name> <description>SpringBootMybatis</description> <properties> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.mysql</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
# 数据库配置信息 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis username: root password: 123456
数据库
数据库建表文件
-- 删除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类
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) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
BookDao
文件结构
package com.itheima.dao; import com.example.springbootmybatis.domain.Book; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface BookDao { @Select("select * from tb_brand where id = #{id}") public Book getById(Integer id); }
测试类
Test类
@SpringBootTest class Springboot05MybatisApplicationTests { @Autowired private BookDao bookDao; @Test void contextLoads() { System.out.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,否则输出 No。
每个结果占一行。
数据范围
1≤n,m≤105
输入样例:
4 5
M 1 2
M 3 4
Q 1 2
Q 1 3
Q 3 4
输出样例:
Yes
No
Yes
提交代码
#include<iostream> using namespace std; const int N = 100010; int n, m; int p[N]; 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("No"); } } return 0; }
import java.io.*; public class Main { static int N = 100010; static int n, m; static int [] p = new int [N]; 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 (op.equals("M")) p[find(a)] = find(b); else { if (find(a) == find(b)) System.out.println("Yes"); else System.out.println("No"); } } } }
滑动窗口
给定一个大小为 n≤106 的数组。
有一个大小为 k 的滑动窗口,它从数组的最左边移动到最右边。
你只能在窗口中看到 k 个数字。
每次滑动窗口向右移动一个位置。
以下是一个例子:
该数组为 [1 3 -1 -3 5 3 6 7],k 为 3。
窗口位置 最小值 最大值
[1 3 -1] -3 5 3 6 7 -1 3
1 [3 -1 -3] 5 3 6 7 -3 3
1 3 [-1 -3 5] 3 6 7 -3 5
1 3 -1 [-3 5 3] 6 7 -3 5
1 3 -1 -3 [5 3 6] 7 3 6
1 3 -1 -3 5 [3 6 7] 3 7
你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。
输入格式
输入包含两行。
第一行包含两个整数 n 和 k,分别代表数组长度和滑动窗口的长度。
第二行有 n 个整数,代表数组的具体数值。
同行数据之间用空格隔开。
输出格式
输出包含两个。
第一行输出,从左至右,每个位置滑动窗口中的最小值。
第二行输出,从左至右,每个位置滑动窗口中的最大值。
输入样例:
8 3
1 3 -1 -3 5 3 6 7
输出样例:
-1 -3 -3 -3 3 3
3 3 5 5 6 7
提交代码
C++
#include<iostream> using namespace std; const int N = 1000010; int a[N], q[N], 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
import java.io.*; public class Main { final static int N = 1000010; static int [] a = new int [N]; static int [] q = new int [N]; 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(System.out)); 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(); out.close(); } }






