引言
我是干前端的,閑來(lái)沒(méi)事,也想學(xué)學(xué)java,下面我會(huì)根據(jù)我學(xué)習(xí)java的經(jīng)歷來(lái)整理出java的速成之路。
學(xué)習(xí)路線
按照數(shù)字的順序?qū)W下去就行了
1.學(xué)習(xí)java基礎(chǔ)教程:主要聽 class
和集合
這兩部分吧,這兩個(gè)部分非常重要,也是開發(fā)中用到最多的,還有就是一些基本的數(shù)據(jù)類型要知道,別的帶過(guò)一下就行了,jdk建議安裝jdk8
,不然學(xué)習(xí)過(guò)程中會(huì)遇到一些問(wèn)題。
2.了解并安裝maven
,了解如何使用maven創(chuàng)建項(xiàng)目,以及pom.xml
文件的使用。
3.學(xué)習(xí)mysql
,安裝mysql服務(wù)和可視化工具,掌握一下基本的增刪改查語(yǔ)句就行了,可視化工具建議安裝SQLyog
,不建議使用navcat
,在公司使用容易收到律師函
,別問(wèn)我怎么知道的,你可以試試。
4.學(xué)習(xí)spring boot
,了解如何創(chuàng)建spring boot項(xiàng)目以及項(xiàng)目的目錄結(jié)構(gòu),學(xué)會(huì)在spring boot項(xiàng)目整合 mybatis plus
,其他的對(duì)于入門來(lái)說(shuō)不重要,咋們開發(fā)的接口,主要也就是操作數(shù)據(jù)庫(kù)然后響應(yīng)數(shù)據(jù)給瀏覽器。
使用spring boot項(xiàng)目開發(fā)接口
創(chuàng)建 spring boot項(xiàng)目
因?yàn)檎兪乔岸?,自然?huì)愛vscode
多一點(diǎn),用idea
咋們估計(jì)用不慣。vscode可以寫java
一點(diǎn)問(wèn)題沒(méi)有,因?yàn)槲乙粚W(xué)習(xí)過(guò)程中直都是用vscode
寫java的,咋們只需要安裝一下這幾個(gè)插件就行了 Extension Pack for Java
,Maven for Java
,Maven dependency explorer
,Spring Initializr Java Support
。
1.
2.3.
4.
5.版本隨便選吧。我選的最新的版本,我用的jdk17
6.
7.公司域名倒寫
8.項(xiàng)目名稱,不建議帶符號(hào)和大寫,因?yàn)槲矣写螏Я伺懿黄饋?lái)
9.
10.選擇自己安裝的jdk版本
11.選擇依賴,我勾選的是我選的,選好回車,然后選擇項(xiàng)目所放置的文件夾12.
13.此時(shí)編輯器右下角會(huì)出現(xiàn)一個(gè)提示,咋們點(diǎn)提示中的check detail
,是藍(lán)色字樣,點(diǎn)了之后,就開始安裝依賴,安裝依賴較慢,需要耐心等待。因?yàn)槲野惭b之前過(guò)了,緩存了,所以沒(méi)給我提示。
14.在pom.xml
添加mybatis plus
及其他需要用到的依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.19</version>
</dependency>
15.建立規(guī)范的項(xiàng)目目錄
MySQL中創(chuàng)建表
整合mybatis plus
首先連接數(shù)據(jù)庫(kù),在.yml
文件配置如下
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username: username
password: password
然后創(chuàng)建根據(jù)表創(chuàng)建實(shí)體類
// Emp.java
package com.example.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
@TableName(value="emp")
public class Emp {
public Integer id;
@TableField(value="emp_name") public String name; // 可以這樣用成員變量來(lái)映射表的字段
public int age;
public double salary;
public double reward;
public double asset;
@TableField(value="dep_id") public Integer depId;
Emp(){}
public Emp(Integer id,String name,int age,double salary,double reward,double asset,Integer depId){
this.id=id;
this.name=name;
this.age=age;
this.salary=salary;
this.reward=reward;
this.asset=asset;
this.depId=depId;
}
}
最后創(chuàng)建接口整合mybatis plus
// empDao.java
package com.example.dao;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Emp;
// mybatis plus
@Mapper
public interface empDao extends BaseMapper<Emp>{
// 也可以在里面加些自己的查詢方法,當(dāng)然mybatis-plus沒(méi)有的才加,不然直接用它提供的方法就好。復(fù)雜查詢我不會(huì),這里就隨便加個(gè)了
@Select("select * from emp")
public ArrayList<Emp> getAllEmp();
}
創(chuàng)建接口中需要用到的數(shù)據(jù)類型
開發(fā)接口
// empController.java
package com.example.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.empDao;
import com.example.entity.Emp;
import com.example.pojo.deleteParams;
import com.example.pojo.listSearchCdt;
import com.example.pojo.listSearchResult;
import com.example.pojo.resultType;
@RestController
@RequestMapping("/emp")
public class empController {
@Autowired empDao empDao;
@GetMapping("/test")
public String test(){
return "hhhh";
}
@GetMapping("/all")
public resultType<List<Emp>> getAllEmp(){
resultType<List<Emp>> res=new resultType<>();
List<Emp> data=empDao.selectList(null);
try {
res.code=data!=null?200:201;
res.data=data;
res.message=data!=null?"查詢所有記錄成功":"查詢所有記錄失敗";
} catch (Exception e) {
res.code=500;
res.error=e;
res.message="服務(wù)器繁忙!";
}
return res;
}
@PostMapping
public resultType<Boolean> addEmp(@RequestBody Emp emp){
resultType<Boolean> res=new resultType<>();
Boolean isSuccess=empDao.insert(emp)>0;
try {
res.code=isSuccess?200:201;
res.data=isSuccess;
res.message=isSuccess?"新增成功":"新增失敗";
} catch (Exception e) {
res.code=500;
res.error=e;
res.message="服務(wù)器繁忙!";
}
return res;
}
@DeleteMapping
public resultType<Boolean> deleteEmp(@RequestBody deleteParams params){
resultType<Boolean> res=new resultType<>();
Boolean isSuccess=empDao.deleteById(params.id)>0;
try {
res.code=isSuccess?200:201;
res.data=isSuccess;
res.message=isSuccess?"刪除成功":"刪除失敗";
} catch (Exception e) {
res.code=500;
res.error=e;
res.message="服務(wù)器繁忙!";
}
return res;
}
@PostMapping("/deleteMany")
public resultType<Boolean> deleteEmps(@RequestBody deleteParams params){
resultType<Boolean> res=new resultType<>();
Boolean isSuccess=empDao.deleteBatchIds(params.ids)>0;
try {
res.code=isSuccess?200:201;
res.data=isSuccess;
res.message=isSuccess?"批量刪除成功":"批量刪除失敗";
} catch (Exception e) {
res.code=500;
res.error=e;
res.message="服務(wù)器繁忙!";
}
return res;
}
@PutMapping
public resultType<Boolean> updateEmp(@RequestBody Emp emp){
resultType<Boolean> res=new resultType<>();
Boolean isSuccess=empDao.updateById(emp)>0;
try {
res.code=isSuccess?200:201;
res.data=isSuccess;
res.message=isSuccess?"更新成功":"更新失敗";
} catch (Exception e) {
res.code=500;
res.error=e;
res.message="服務(wù)器繁忙!";
}
return res;
}
@GetMapping("/{id}")
public resultType<Emp> getEmpById(@PathVariable Integer id){
resultType<Emp> res=new resultType<>();
Emp data=empDao.selectById(id);
try {
res.code=data!=null?200:201;
res.data=data;
res.message=data!=null?"查詢單條記錄成功":"查詢單條記錄失敗";
} catch (Exception e) {
System.out.println(e);
res.code=500;
res.error=e;
res.message="服務(wù)器繁忙!";
}
return res;
}
@GetMapping("/detail")
public resultType<Emp> _getEmpById(Integer id){
resultType<Emp> res=new resultType<>();
Emp data=empDao.selectById(id);
try {
res.code=data!=null?200:201;
res.data=data;
res.message=data!=null?"查詢單條記錄成功":"查詢單條記錄失敗";
} catch (Exception e) {
System.out.println(e);
res.code=500;
res.error=e;
res.message="服務(wù)器繁忙!";
}
return res;
}
// 分頁(yè)條件查詢
@PostMapping("/page")
public resultType<listSearchResult<Emp>> getSearchEmpList(@RequestBody listSearchCdt cdt){
// System.out.println(cdt);
resultType<listSearchResult<Emp>> res=new resultType<>();
IPage<Emp> page=new Page<Emp>(cdt.page.page, cdt.page.size, true);
QueryWrapper<Emp> filterOptions=
// new QueryWrapper<Emp>(cdt.filterOpions);
new QueryWrapper<Emp>();
filterOptions.like(cdt.filterOptions.name!=null,"emp_name",cdt.filterOptions.name).eq(cdt.filterOptions.age>0,"age", cdt.filterOptions.age).eq(cdt.filterOptions.salary>0, "salary", cdt.filterOptions.salary);
IPage<Emp> ipage=empDao.selectPage(page,filterOptions);
try {
listSearchResult<Emp> data= new listSearchResult<Emp>();
data.list=ipage.getRecords();
data.total=ipage.getTotal();
res.data=data;
res.message="分頁(yè)條件查詢成功";
} catch (Exception e) {
res.code=500;
res.error=e;
res.message="服務(wù)器繁忙!";
}
return res;
}
}
可以看到mybatis plus
已經(jīng)內(nèi)置了基本的增刪改查方法,不需要我們寫什么sql
,除非是非常復(fù)雜的查詢
開發(fā)完后找到應(yīng)用的入口類,點(diǎn)擊run,啟動(dòng)項(xiàng)目
測(cè)試接口
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-460881.html
項(xiàng)目源代碼獲取
碼云倉(cāng)庫(kù):
https://gitee.com/gitee_lw/springboot_test.git
項(xiàng)目介紹:包含token簽發(fā)和校驗(yàn),基本的增刪改查接口,文件上傳和下載接口
sql表我已經(jīng)導(dǎo)出到 resources/static/tables
目錄下了,創(chuàng)建表的代碼在createTable.md
文件,需要的話可以使用這個(gè)代碼創(chuàng)建表然后導(dǎo)入數(shù)據(jù)到你的數(shù)據(jù)庫(kù)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-460881.html
到了這里,關(guān)于前端開發(fā)如何速成java,使用java開發(fā)網(wǎng)絡(luò)接口的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!