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

网友投稿 307 2022-10-09


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

目录项目结构pom.xml启动类DaRTxUio层Service层Controller层测试类测试总结

项目结构

pom.xml

pom.xml:

xmlns:xsi="http://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">

spring-boot-02

com.keafmd

1.0-SNAPSHOT

4.0.0

spring-boot-08

org.springframework.boot

spring-boot-starter-web

org.apache.tomcat.embed

tomcat-embed-jasper

jstl

jstl

1.2

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

5.1.46

org.springframework.boot

spring-boot-starter-test

org.springframework.boot

spring-boot-devtools

xmlns:xsi="http://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">

spring-boot-02

com.keafmd

1.0-SNAPSHOT

4.0.0

spring-boot-08

org.springframework.boot

spring-boot-starter-web

org.apache.tomcat.embed

tomcat-embed-jasper

jstl

jstl

1.2

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

5.1.46

org.springframework.boot

spring-boot-starter-test

org.springframework.boot

spring-boot-devtools

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);

}

}

Service层

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();

}

}

Controller层

UserController:

package com.keafmd.chttp://ontroller;

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

总结

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:php随机函数mt_rand()产生的小问题大漏洞(php rand函数)
下一篇:SANS:2018年度事件响应调查报告
相关文章

 发表评论

暂时没有评论,来抢沙发吧~