使用Springboot结合mybatis搭建最简单的操作mysql教程

废话不说,上代码

pom文件,如果是jdk1.8,请使用如下的SpringBoot版本号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?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.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mybatis</groupId>
    <artifactId>mybatis-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis-service</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</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>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>2.3.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

  项目整体结构

 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
logging.level.com.java=debug
logging.level.web=debug
spring.devtools.add-properties=false
 
ip.port=127.0.0.1:3306
 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://${ip.port}/yourheart?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
#客户端从连接池等待连接的最大毫秒数
spring.datasource.hikari.connection-timeout=20000
#配置最大连接池数大小
spring.datasource.hikari.maximum-pool-size=10
#连接池中维护的最小空闲连接数
spring.datasource.hikari.minimum-idle=10
mybatis.mapper-locations=classpath:mapping/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

 java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.java.mybatisservice;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class MybatisServiceApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MybatisServiceApplication.class, args);
    }
 
}
 
 
package com.java.mybatisservice.mapper;
 
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
 
import java.util.List;
import java.util.Map;
 
/**
 * @Description:
 * @Author: 喵星人
 * @Create: 2023/11/10 10:47
 */
@Mapper
public interface RecyResumerMapper {
 
 
    @Select("SELECT  *  FROM  recy_bank_resume  ORDER  BY update_resume_times DESC")
    List<Map<String,Object>> recyResumeList();
 
}

  测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.java.mybatisservice;
 
import com.java.mybatisservice.mapper.RecyResumerMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest
public class MybatisServiceApplicationTests {
 
    @Autowired
    private RecyResumerMapper recyResumerMapper;
 
    @Test
    public void contextLoads() {
        recyResumerMapper.recyResumeList();
    }
 
}

  运行成功后效果图

 

posted @   不忘初心2021  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2022-11-10 如何进行集合切批
2022-11-10 windows电脑连接oracle显示无监听程序
点击右上角即可分享
微信分享提示