服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 一篇超详细的Spring Boot对jdbc支持的文章

一篇超详细的Spring Boot对jdbc支持的文章

2021-10-25 10:59牛哄哄的柯南 Java教程

JdbcTemplate 是在JDBC API基础上提供了更抽象的封装,并提供了基于方法注解的事务管理能力。 通过使用SpringBoot自动配置功能并代替我们自动配置beans,下面给大家介绍spring boot中使用JdbcTemplate相关知识,一起看看吧

项目结构

一篇超详细的Spring Boot对jdbc支持的文章

pom.xml

pom.xml:

?
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
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-boot-02</artifactId>
        <groupId>com.keafmd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>spring-boot-08</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 解析jsp类库  -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
 
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
 
        <!-- JDBC-启动器, 默认的数据源 HikariCP -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- JDBC-启动器, 默认的数据源 HikariCP -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
 
</project>

application.yml

?
1
2
3
4
5
6
7
8
server:
  port: 80
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 18044229

启动类

package com.keafmd;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Keafmd * * @ClassName: App08 * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 11:48 * @Blog: https://keafmd.blog.csdn.net/ */@SpringBootApplicationpublic class App08 {    public static void main(String[] args) {        SpringApplication.run(App08.class, args);    }}

Dao层

UserDao:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.keafmd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * Keafmd
 *
 * @ClassName: App08
 * @Description:
 * @author: 牛哄哄的柯南
 * @Date: 2021-04-08 11:48
 * @Blog: https://keafmd.blog.csdn.net/
 */
 
@SpringBootApplication
public class App08 {
    public static void main(String[] args) {
        SpringApplication.run(App08.class, args);
    }
}

Service层

IUserService :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.keafmd.service;
import java.util.List;
/**
 * Keafmd
 *
 * @ClassName: IUserService
 * @Description:
 * @author: 牛哄哄的柯南
 * @Date: 2021-04-08 11:59
 * @Blog: https://keafmd.blog.csdn.net/
 */
public interface IUserService {
    List list();
}

UserServiceImpl:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.keafmd.service.impl;
import com.keafmd.dao.UserDao;
import com.keafmd.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * Keafmd
 *
 * @ClassName: UserServiceImpl
 * @Description:
 * @author: 牛哄哄的柯南
 * @Date: 2021-04-08 12:00
 * @Blog: https://keafmd.blog.csdn.net/
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserDao userDao;
    @Override
    public List list() {
        return userDao.userList();
    }
}

Controller层

UserController:

?
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
package com.keafmd.controller;
import com.keafmd.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * Keafmd
 *
 * @ClassName: UserController
 * @Description:
 * @author: 牛哄哄的柯南
 * @Date: 2021-04-08 18:04
 * @Blog: https://keafmd.blog.csdn.net/
 */
@RestController
public class UserController {
    @Autowired
    IUserService userService;
    /**
     * http://127.0.0.1/userlist
     * @return
     */
    @RequestMapping("userlist")
    List UserList(){
        return userService.list();
    }
}

测试类测试

UserDaoTest:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.keafmd.dao;
import com.keafmd.App08;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(classes = App08.class)
class UserDaoTest {
    @Autowired
    UserDao userDao;
    @Test
    public void test(){
        List userlist = userDao.userList();
        for (Object o : userlist) {
            System.out.println(o);
        }
    }
}

运行test方法的效果:

一篇超详细的Spring Boot对jdbc支持的文章

运行启动类,测试效果

运行启动类,访问:http://127.0.0.1/userlist

一篇超详细的Spring Boot对jdbc支持的文章

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/weixin_43883917/article/details/115511007

延伸 · 阅读

精彩推荐