Springboot連接數(shù)據(jù)庫(kù)及查詢數(shù)據(jù)完整流程
Springboot連接數(shù)據(jù)庫(kù)
第一步
springboot繼承Mybatis及數(shù)據(jù)庫(kù)連接依賴(上一篇文章已經(jīng)記錄 )
第二步
resources -> application.properties
application.properties中增加數(shù)據(jù)庫(kù)連接配置
# 增加數(shù)據(jù)庫(kù)連接 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=lvxingchen spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
第三步
domain -> User
創(chuàng)建實(shí)體類,屬性要跟數(shù)據(jù)庫(kù)表字段一致
package com.lxc.springboot.domain; public class User { private int id; private String user; private String name; private int age; private String password; @Override public String toString() { return "User{" + "id=" + id + ", user='" + user + '\'' + ", name='" + name + '\'' + ", age=" + age + ", password='" + password + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
第四步
mapper -> UserMapper
創(chuàng)建UserMapper接口,這也是項(xiàng)目的持久層,與數(shù)據(jù)查詢相關(guān)的,之后我們需要讓sprongboot知道,mapper文件夾就是數(shù)據(jù)持久層接口,所以,在項(xiàng)目入口文件中還要使用@MapperScan注解定義持久層。
package com.lxc.springboot.mapper; import com.lxc.springboot.domain.User; import java.util.List; public interface UserMapper { public List<User> getUserList(); }
在項(xiàng)目入口文件中的配置:
@ComponentScan("com.lxc.springboot") @SpringBootApplication @MapperScan("com.lxc.springboot.mapper") // 讓springboot知道m(xù)apper是這個(gè)項(xiàng)目的持久層 public class BootAndVueProjectApplication { private static final Logger LOG = LoggerFactory.getLogger(BootAndVueProjectApplication.class); public static void main(String[] args) { SpringApplication app = new SpringApplication(BootAndVueProjectApplication.class); // SpringApplication.run(BootAndVueProjectApplication.class, args); Environment env = app.run(args).getEnvironment(); LOG.info("啟動(dòng)成功!"); LOG.info("地址:\thttp://127.0.0.1:{}", env.getProperty("server.port")); } }
然后,創(chuàng)建UserMapper接口的 sql映射文件userMapper.xml,通常我會(huì)把這個(gè)文件放在resources -> mapper文件夾中
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!--接口--> <!-- namespace:對(duì)應(yīng)接口的全路徑; id:對(duì)應(yīng)接口的方法; resultType:結(jié)果類型。 --> <mapper namespace="com.lxc.springboot.mapper.UserMapper" > <select id="getUserList" resultType="com.lxc.springboot.domain.User"> select id, user, name, age, password from user </select> </mapper>
定義完之后,springboot怎么能知道 resources -> mapper -> userMapper.xml是一個(gè)sql映射文件呢,此時(shí)需要在resources -> application.properties 中去配置:
# 配置mybatis所有的Mapper.xml所在的路徑 mybatis.mapper-locations=classpath:/mapper/**/*.xml
第五步
service -> UserService
接口定義完,我們來創(chuàng)建service服務(wù)層,所有的業(yè)務(wù)邏輯的處理在這一層實(shí)現(xiàn),也負(fù)責(zé)調(diào)用持久層接口。
package com.lxc.springboot.service; import com.lxc.springboot.domain.User; import com.lxc.springboot.mapper.UserMapper; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * service層調(diào)用持久層 * @Service // 讓spring掃描到這個(gè)包 * * @Autowired和@Resource * 兩個(gè)注解都可以把一個(gè)類注入進(jìn)來(相當(dāng)于import) * Resource JDK自帶的 * Autowired spring自帶的 */ @Service // 讓spring掃描到這個(gè)包 public class UserService { @Resource public UserMapper userMapper; public List<User> getList() { return userMapper.getUserList(); } }
第六步
controller-> TestController
既然服務(wù)都寫完了,也查詢到數(shù)據(jù)了,那么來定義一個(gè)控制層Controller,負(fù)責(zé)調(diào)用service層,編寫前端api接口,這一層也算是一個(gè)中轉(zhuǎn)層。
這里著重記錄下 ComResponse這個(gè)類,restful接口在返回給前端JSON數(shù)據(jù)時(shí),同時(shí)也會(huì)返回一些公共的數(shù)據(jù),如:狀態(tài)碼(code)、響應(yīng)信息(message)等等,在這里我們統(tǒng)一處理,編寫一個(gè)公共類,里邊有這些公共字段屬性,同時(shí)還需要有一個(gè)data數(shù)據(jù)屬性,類型一般是:List , 之所以要把公共類定義為泛型,因?yàn)?,在setData的時(shí)候,類型不確定,所以需要定義為泛型, 返回給前端的格式如下:
{
code: 200,
message: "查詢成功",
data: [{ name:"lxc", age: 20 }, { name: "123", age: 100 }]
}
package com.lxc.springboot.controller; import com.lxc.springboot.commonResponse.ComResponse; import com.lxc.springboot.service.UserService; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController // 通常返回一個(gè)json或 字符串 //@Controller // 一般是返回一個(gè)頁(yè)面 public class TestController { @Resource // 把service層userService注入進(jìn)來 private UserService userService; // 調(diào)用service層 @RequestMapping(value = "/service") public ComResponse getService() { ComResponse<List<User>> objComResponse = new ComResponse<>(); List<User> userList = userService.getList(); objComResponse.setData(userList); objComResponse.setMsg("返回成功") return objComResponse; } }
公共類:
commonResponse -> ComResponse
package com.lxc.springboot.commonResponse; /** * * @param <T> * ComResponse 是一個(gè)泛型類,返回的是一個(gè)泛型,外界可以傳入任何類型的值 * 理解泛型: * 有點(diǎn)像js方法,你給它傳任何類型都可以,但是最后返回的類型是setData時(shí)候傳的對(duì)象!?。? */ public class ComResponse<T> { private String msg = "返回成功"; private int code = 200; private T data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
第七步
啟動(dòng)項(xiàng)目測(cè)試:
到此這篇關(guān)于Springboot連接數(shù)據(jù)庫(kù)及查詢數(shù)據(jù)完整流程的文章就介紹到這了,更多相關(guān)Springboot連接數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于SpringBoot改動(dòng)后0.03秒啟動(dòng)的問題
這篇文章主要介紹了SpringBoot改動(dòng)后0.03秒啟動(dòng),本文結(jié)合示例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12spring boot攔截器實(shí)現(xiàn)IP黑名單實(shí)例代碼
本篇文章主要介紹了spring boot攔截器實(shí)現(xiàn)IP黑名單實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04分享我的第一次java Selenium自動(dòng)化測(cè)試框架開發(fā)過程
這篇文章主要介紹了分享我的第一次java Selenium自動(dòng)化測(cè)試框架開發(fā)過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Mac配置 maven以及環(huán)境變量設(shè)置方式
這篇文章主要介紹了Mac配置 maven以及環(huán)境變量設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Java面試必考之如何在項(xiàng)目中優(yōu)雅的拋出異常
這篇文章主要為大家詳細(xì)介紹了Java中的幾種異常關(guān)鍵字和異常類相關(guān)知識(shí),本文比較適合剛?cè)肟覬ava的小白以及準(zhǔn)備秋招的大佬閱讀,需要的可以收藏一下2023-06-06