一篇超详细的Spring Boot对jdbc支持的文章
短信预约 -IT技能 免费直播动态提醒
项目结构
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;@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;
@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;
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;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
UserDao userDao;
@Override
public List list() {
return userDao.userList();
}
}
Controller层
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;
@RestController
public class UserController {
@Autowired
IUserService userService;
@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
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注编程网的更多内容!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341