Github項(xiàng)目地址:travel-springboot-demo
博客文章鏈接:以Springboot為框架的預(yù)算管理后端demo
這個(gè)項(xiàng)目實(shí)際上是我服務(wù)計(jì)算概論課的一部分劲厌,題目要求是:編寫簡單的旅行預(yù)算管理服務(wù)的接口并做一個(gè)調(diào)用它的程序。因?yàn)樽罱脤W(xué)習(xí)了springboot框架,就以此框架來完成我的作業(yè)。
功能
按最開始的想法捆交,有兩張表酬蹋,分別記錄預(yù)算和記錄開支身笤。
id | total |
---|---|
int | double |
id,主鍵,自增長 | 預(yù)算金額 |
id | matter | spend |
---|---|---|
int | varchar | double |
id,主鍵斤彼,自增長 | 開支事項(xiàng) | 花費(fèi)金額 |
有四個(gè)接口分瘦,分別能添加開支,更改預(yù)算(實(shí)際寫的過程中是添加預(yù)算記錄琉苇,然后取id最大的條數(shù))嘲玫,獲取預(yù)算,獲取已開支的總金額并扇。
因?yàn)橹皇莇emo去团,重點(diǎn)是想體現(xiàn)著兩類api的實(shí)現(xiàn)過程,所以功能上有些簡陋。
項(xiàng)目說明
SpendController對外提供接口土陪,接收和發(fā)出數(shù)據(jù)昼汗;
SpendMapper:數(shù)據(jù)控制層,和UserMapper.xml一起實(shí)現(xiàn)對數(shù)據(jù)庫的操縱
ItemSpend,TotalSpend:實(shí)體類
SpendService:服務(wù)層鬼雀,數(shù)據(jù)和服務(wù)之間的連接顷窒,并在SpendServiceImpl中實(shí)現(xiàn)
application.properties:springboot配置文件,此處僅用戶配置數(shù)據(jù)庫及更改端口
application.properties
# 數(shù)據(jù)庫連接uri源哩,最后的demo更改為自己的數(shù)據(jù)庫名
spring.datasource.url=jdbc:mysql://localhost:3306/demo
# 數(shù)據(jù)庫用戶名
spring.datasource.username=root
# 數(shù)據(jù)庫密碼
spring.datasource.password=123456
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
spring.jpa.show-sql=true
# 運(yùn)行的端口號
server.port=8098
實(shí)體類
放在entity包下蹋肮,下面以ItemSpend類為例說明:
@Data // 此注解可用于替代getter和setter方法
@Entity // 表明此類為實(shí)體類
@Table(name = "item_spend") // 如果沒有此表會新建一張名為“item_spend”的表
public class ItemSpend {
@Id // 表明此為主鍵
@Column(name = "id") //表的字段名
private Integer id;
@Column(name = "matter")
private String spendMatter;
@Column(name = "spend")
private Float spend;
}
dao
此類下存放操縱數(shù)據(jù)庫的接口,此項(xiàng)目中以Mybatis為例璧疗,你也可以使用Jpa等坯辩。SpendMapper
類和mapper/SpendMapper.xml
配合使用,也可以直接采用@Select
等注解的形式直接寫在SpendMapper中崩侠。以List\<ItemSpend\> getItemSpendList()
為例:
@Mapper //聲明此類為mapper
public interface SpendMapper {
...
List<ItemSpend> getItemSpendList();
...
}
<?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">
<mapper namespace="xyz.somelou.travel.dao.SpendMapper">
<resultMap type="xyz.somelou.travel.entity.ItemSpend" id="spendResultMap">
<id property="id" column="id"/>
<result property="spendMatter" column="matter"/>
<result property="spend" column="spend"/>
</resultMap>
<!-- 返回List型對象漆魔,resultMap如果不在xml里列舉出來會報(bào)錯(cuò),我也不知道為什么-->
<select id="getItemSpendList" resultMap="spendResultMap" parameterType="String">
select * from item_spend;
</select>
</mapper>
Service層
此層常以接口+實(shí)現(xiàn)的方式組織却音,用于處理從數(shù)據(jù)庫中查詢得到的數(shù)據(jù)和前臺發(fā)來的請求改抡,以TotalBudget getTotalBudget();
為例:
@Service // 聲明此類為Service層
public class SpendServiceImpl implements SpendService {
// 調(diào)用mapper
@Autowired // 寫在字段上,那么就不需要再寫setter方法
SpendMapper spendMapper;// 有時(shí)編譯器會報(bào)紅系瓢,不要管它阿纤,實(shí)際沒有錯(cuò)誤
...
@Override
public TotalBudget getTotalBudget() {
return spendMapper.getTotalBudget();
}
}
Controller層
實(shí)現(xiàn)對外的接口,主要是調(diào)用者請求數(shù)據(jù)的解析和被請求數(shù)據(jù)的內(nèi)部獲取和封裝夷陋,以/add/budget
和/get/budget
為例:
@CrossOrigin // 支持跨域
@RestController // 聲明這是Controller,@RestController注解相當(dāng)于@ResponseBody + @Controller合在一起的作用,也可以只使用@Controller,具體我也不是很清楚
@RequestMapping(value = "/api/trip") // 接口(的一部分)
public class SpendController {
// 使用Service接口
@Autowired
private SpendService spendService;
// 補(bǔ)充說明接口
@RequestMapping(value = "/add/budget")
// @RequestBody Map<String,Object> para即為傳來的json
// ModelMap返回一個(gè)json
public ModelMap addBudget(@RequestBody Map<String,Object> param){
ModelMap result=new ModelMap();
TotalBudget totalBudget=new TotalBudget();
totalBudget.setBudget(Float.valueOf(param.get("budget").toString()));
spendService.addBudget(totalBudget);
result.addAttribute("msg","success");
return result;
}
...
}
這樣該方法的對外接口(以在本機(jī)運(yùn)行為例):http://localhost:8098/api/trip/add/budget
已知bug
- 沒有進(jìn)行重復(fù)提交的限制欠拾,會因?yàn)榫W(wǎng)絡(luò)卡頓出現(xiàn)重復(fù)提交;
<hr />