前端开发如何速成java,使用java开发网络接口

引言

我是干前端的,闲来没事,也想学学java,下面我会根据我学习java的经历来整理出java的速成之路。

学习路线

按照数字的顺序学下去就行了
1.学习java基础教程:主要听 class集合这两部分吧,这两个部分非常重要,也是开发中用到最多的,还有就是一些基本的数据类型要知道,别的带过一下就行了,jdk建议安装jdk8,不然学习过程中会遇到一些问题。
2.了解并安装maven,了解如何使用maven创建项目,以及pom.xml文件的使用。
3.学习mysql,安装mysql服务和可视化工具,掌握一下基本的增删改查语句就行了,可视化工具建议安装SQLyog ,不建议使用navcat,在公司使用容易收到律师函,别问我怎么知道的,你可以试试。
4.学习spring boot,了解如何创建spring boot项目以及项目的目录结构,学会在spring boot项目整合 mybatis plus,其他的对于入门来说不重要,咋们开发的接口,主要也就是操作数据库然后响应数据给浏览器。

使用spring boot项目开发接口

创建 spring boot项目

因为咋们是前端,自然会爱vscode多一点,用idea咋们估计用不惯。vscode可以写java一点问题没有,因为我一学习过程中直都是用vscode写java的,咋们只需要安装一下这几个插件就行了 Extension Pack for JavaMaven for JavaMaven dependency explorerSpring Initializr Java Support
1.
在这里插入图片描述
2.
在这里插入图片描述3.
在这里插入图片描述4.
在这里插入图片描述
5.版本随便选吧。我选的最新的版本,我用的jdk17
在这里插入图片描述
6.
在这里插入图片描述
7.公司域名倒写
在这里插入图片描述
8.项目名称,不建议带符号和大写,因为我有次带了跑不起来
在这里插入图片描述
9.
在这里插入图片描述
10.选择自己安装的jdk版本
在这里插入图片描述
11.选择依赖,我勾选的是我选的,选好回车,然后选择项目所放置的文件夹
在这里插入图片描述12.
在这里插入图片描述
13.此时编辑器右下角会出现一个提示,咋们点提示中的check detail,是蓝色字样,点了之后,就开始安装依赖,安装依赖较慢,需要耐心等待。因为我安装之前过了,缓存了,所以没给我提示。
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.建立规范的项目目录
在这里插入图片描述

MySQL中创建表

在这里插入图片描述

整合mybatis plus

首先连接数据库,在.yml文件配置如下

spring:
  datasource: 
       driver-class-name: com.mysql.cj.jdbc.Driver   
       url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
       username: username
       password: password

然后创建根据表创建实体类

// 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;   // 可以这样用成员变量来映射表的字段
  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;
  }
}

最后创建接口整合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>{
// 也可以在里面加些自己的查询方法,当然mybatis-plus没有的才加,不然直接用它提供的方法就好。复杂查询我不会,这里就随便加个了
  @Select("select * from emp")  
  public ArrayList<Emp> getAllEmp();
}

创建接口中需要用到的数据类型

![在这里插入图片描述](https://img-blog.csdnimg.cn/9429007b22ce40e6aa749a99c0ddadb7.png

开发接口

// 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="服务器繁忙!";
   }
   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="服务器繁忙!";
    }
    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="服务器繁忙!";
    }
    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="服务器繁忙!";
    }
    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="服务器繁忙!";
     }
     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="服务器繁忙!";
     }
     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="服务器繁忙!";
     }
     return res;
   }
   


  //  分页条件查询
  @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="分页条件查询成功";
     } catch (Exception e) {
      res.code=500;
      res.error=e;
      res.message="服务器繁忙!";
     }
     return res;
   }
}

可以看到mybatis plus已经内置了基本的增删改查方法,不需要我们写什么sql,除非是非常复杂的查询
在这里插入图片描述

开发完后找到应用的入口类,点击run,启动项目
在这里插入图片描述

测试接口

在这里插入图片描述

项目源代码获取

码云仓库:
https://gitee.com/gitee_lw/springboot_test.git
项目介绍:包含token签发和校验,基本的增删改查接口,文件上传和下载接口
sql表我已经导出到 resources/static/tables目录下了,创建表的代码在createTable.md文件,需要的话可以使用这个代码创建表然后导入数据到你的数据库。
![在这里插入图片描述](https://img-blog.csdnimg.cn/915b0868a26748559dabd4cb681ef514.png

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值