一篇超詳細(xì)的Spring Boot對(duì)jdbc支持的文章
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類庫(kù) --><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-啟動(dòng)器, 默認(rèn)的數(shù)據(jù)源 HikariCP --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- JDBC-啟動(dòng)器, 默認(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: 80spring: datasource: url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false driver-class-name: com.mysql.jdbc.Driver username: root password: 18044229啟動(dòng)類
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/ */@SpringBootApplicationpublic 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/ */@Servicepublic 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;/** * Keafmd * * @ClassName: UserController * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 18:04 * @Blog: https://keafmd.blog.csdn.net/ */@RestControllerpublic class UserController { @Autowired IUserService userService; /** * http://127.0.0.1/userlist * @return */ @RequestMapping('userlist') List UserList(){return userService.list(); }}測(cè)試類測(cè)試
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);} }}
運(yùn)行test方法的效果:
運(yùn)行啟動(dòng)類,測(cè)試效果
運(yùn)行啟動(dòng)類,訪問(wèn):http://127.0.0.1/userlist
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注好吧啦網(wǎng)的更多內(nèi)容!
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼3. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. 淺談python出錯(cuò)時(shí)traceback的解讀6. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解7. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法8. Nginx+php配置文件及原理解析9. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析
