您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“SpringBoot對jdbc的支持問題如何解決”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“SpringBoot對jdbc的支持問題如何解決”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
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-啟動器, 默認(rèn)的數(shù)據(jù)源 HikariCP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- JDBC-啟動器, 默認(rèn)的數(shù)據(jù)源 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;
@SpringBootApplication
public class App08 {
public static void main(String[] args) {
SpringApplication.run(App08.class, args);
}
}
Dao層
UserDao:
package com.keafmd.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List userList(){
String sql ="select * from user";
return jdbcTemplate.queryForList(sql);
}
}
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;
/**
* 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);
}
}
}
讀到這里,這篇“SpringBoot對jdbc的支持問題如何解決”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。