jdbc 에 대한 아주 상세 한 Spring Boot 지원 글

7087 단어 SpringBootjdbc
프로젝트 구조
在这里插入图片描述
pom.xml
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 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

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:

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);
    }
}
서비스 층
IUserService :

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:

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();
    }
}
컨트롤 러 층
UserController:

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:

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 방법 을 실행 하 는 효과:
在这里插入图片描述
시작 클래스 실행,테스트 효과
시작 클래스 실행,접근:http://127.0.0.1/userlist
在这里插入图片描述
총결산
이 글 은 여기까지 입 니 다.당신 에 게 도움 을 줄 수 있 기 를 바 랍 니 다.또한 당신 이 우리 의 더 많은 내용 에 관심 을 가 져 주 실 수 있 기 를 바 랍 니 다!

좋은 웹페이지 즐겨찾기