總結:
controller展示 定義介面路徑和呼叫service
service 處理業務邏輯 資料庫資料
mapper定義操作資料庫動作,命名
mapper.xml執行mapper里定義的動作的sql陳述句,與資料庫互動
entity 定義類,與資料庫型別保持一致
https://www.bilibili.com/video/BV16541147s1?from=search&seid=14149306823192602727&spm_id_from=333.337.0.0第一步 new project

修改名字
安裝相關依賴(裝少了的話后期可以自己加,不過比較麻煩)
首先勾上這個工具,方便以后用
web
用上資料庫,勾上框的那三個

命名

第一次下載會有點慢(防火墻可能會阻止,需要允許訪問)
(嫌棄慢的話搜索配一下阿里云的鏡像,確實等了很久)
進來配置資料庫資訊

yml配置
下面用戶名密碼資料庫埠按照自己實際情況來(隨便寫的)
spring:
datasource:
url: jdbc:mysql://localhost:3306/test1?characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.test.elasticsearchdemo.domain
再次運行,分配埠

訪問空白(因為啥也沒寫)

建立controller包,建立類,回傳一些資料(當然,現在是靜態的)


模擬回傳json格式,建立Animal類,右鍵——生成——建構式/get set

設定回傳這個json物件 return new Animal("dog",5);
成功回傳

前端測驗介面,因為埠的不同還是出現了跨域問題
通過@CrossOrigin解決,因為方便,但是不是很好,還是單獨配置一下過濾器比較好


下面當然是需要用到資料庫的呼叫,真正實戰都是呼叫資料庫的嘛

首先建立物體類(跟資料庫保持一致)
entity
package com.test.demo.entity;
public class User {
private int id;
private String name;
private String password;
public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

mapper 操作資料庫動作
package com.test.demo.mapper;
import com.test.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
// 定義操作資料庫為查找所有用戶
List<User> findAll();
}
定義查找所有的資料

建立mapper.xml(sql陳述句)
mapper.xml配置詳解
在resource檔案下
<?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對應哪個mapper檔案 -->
<mapper namespace="com.test.demo.mapper.UserMapper">
<!-- id對應mapper里面的方法名 resultType對應物體類的資料庫類-->
<select id="findAll" resultType="com.test.demo.entity.User">
select * from user;
</select>
</mapper>

service操作匯出資料庫資料
package com.test.demo.service;
import com.test.demo.entity.User;
import com.test.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> findAll(){
return userMapper.findAll();
}
}

mapper里面加注解@mapper

最后controller里面設定介面
package com.test.demo.controller;
import com.test.demo.entity.User;
import com.test.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class userController {
@Autowired
private UserService userService;
@CrossOrigin
@RequestMapping("/abc")
public Animal getName(){
return new Animal("hml",5);
}
@CrossOrigin
@RequestMapping("/abcd")
public List<User> getUser(){
return userService.findAll();
}
}
效果:

開啟谷歌插件json格式化

效果有:

前后端分離測驗呼叫成功

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/402664.html
標籤:其他
上一篇:[ 虛擬專用網 ] IPsce 虛擬局域網(安全的IP協議的虛擬專用網)詳解(二) --- 安全聯盟SA
下一篇:藍橋杯 十六進制轉八進制
