最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

Springboot web 项目开发流程梳理总结|世界讯息

来源:博客园

项目开发流程梳理总结


(资料图)

1.环境准备1.准备数据库表(user,order);2.创建springboot 工程,引入对应的起步依赖(web,mybatis,mybatisx,mysql驱动,lombok);3.配置文件application.properties中引入mybatis的配置信息,准备对应的实体类;4.准备对应的mapper,service(接口,实现类),controller基础构建;

5.前后端统一交互响应结果Result

@Data@NoArgsConstructor@AllArgsConstructorpublic class Result{

private Integer code; //响应码:1代表成功;0代码失败;private String msg; // 响应描述信息private Object data; // 返回的数据

public static Result success(){return new Rresult(1,"success",null);}

public static Result success(Object data){return new Rresult(1,"success",data);}

public static Result error(String msg){return new Rresult(0,msg,null);}

}

6.日志小技巧@Slf4j

log.info("查询全部部门数据");

7--1.controller.java---------------@GetMapping@DeleteMapping@PostMapping@UpdateMapping

package com.itheima.controller;

import com.itheima.pojo.Dept;import com.itheima.pojo.Result;import com.itheima.service.DeptService;import lombok.extern.slf4j.Slf4j;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

  • 部门管理Controller*/@Slf4j@RequestMapping("/depts")@RestControllerpublic class DeptController {

    //private static Logger log = LoggerFactory.getLogger(DeptController.class);@Autowiredprivate DeptService deptService;

    /**

    • 查询部门数据
    • @return*///@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET@GetMappingpublic Result list(){log.info("查询全部部门数据");//调用service查询部门数据ListdeptList = deptService.list();return Result.success(deptList);}

    /**

    • 删除部门
    • @return*/@DeleteMapping("/{id}")public Result delete(@PathVariable Integer id){log.info("根据id删除部门:{}",id);//调用service删除部门deptService.delete(id);return Result.success();}

    /**

    • 新增部门
    • @return*/@PostMappingpublic Result add(@RequestBody Dept dept){log.info("新增部门: {}" , dept);//调用service新增部门deptService.add(dept);return Result.success();}}

7-2--------DeptServiceImpl.javapackage com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;import com.itheima.pojo.Dept;import com.itheima.service.DeptService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;

import java.time.LocalDateTime;import java.util.List;

@Servicepublic class DeptServiceImpl implements DeptService {

@Autowiredprivate DeptMapper deptMapper;@Overridepublic List list() {    return deptMapper.list();}@Overridepublic void delete(Integer id) {    deptMapper.deleteById(id);}@Overridepublic void add(Dept dept) {    dept.setCreateTime(LocalDateTime.now());    dept.setUpdateTime(LocalDateTime.now());    deptMapper.insert(dept);}

}

7-3------------------interface DeptServicepackage com.itheima.service;

import com.itheima.pojo.Dept;

import java.util.List;

/**

  • 部门管理/public interface DeptService {/*

    • 查询全部部门数据
    • @return*/Listlist();

    /**

    • 删除部门
    • @param id*/void delete(Integer id);

    /**

    • 新增部门
    • @param dept*/void add(Dept dept);}

7-4----------interface DeptMapperpackage com.itheima.mapper;

import com.itheima.pojo.Dept;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;

import java.util.List;

/**

  • 部门管理/@Mapperpublic interface DeptMapper {/*

    • 查询全部部门
    • @return*/@Select("select * from dept")Listlist();

    /**

    • 根据ID删除部门
    • @param id*/@Delete("delete from dept where id = #{id}")void deleteById(Integer id);

    /**

    • 新增部门
    • @param dept*/@Insert("insert into dept(name, create_time, update_time) values(#{name},#{createTime},#{updateTime})")void insert(Dept dept);}

7-5-------------------interface EmpMapper ========reources.com.alex.mapper.EmpMapper.xml---------------------package com.itheima.mapper;

import com.itheima.pojo.Emp;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;import java.util.List;

/**

  • 员工管理*/@Mapperpublic interface EmpMapper {

    /**

    • 查询总记录数
    • @return///@Select("select count() from emp")//public Long count();

    /**

    • 分页查询,获取列表数据
    • @param start
    • @param pageSize
    • @return*///@Select("select * from emp limit #{start},#{pageSize}")//public Listpage(Integer start, Integer pageSize);

    /**

    • 员工信息查询
    • @return*///@Select("select * from emp")public Listlist(String name, Short gender,LocalDate begin,LocalDate end);

    /**

    • 批量删除
    • @param ids*/void delete(Listids);}

7-6.--------EmpMapper.xml 映射文件--------------------

delete from emp where id in #{id}

8.PageHelper分页插件

PageHelper.startPage(page,pagesize)引入依赖:com.github.pagehelperpagehelper-spring-boot-starter1.4.2

@Overridepublic PageBean page(Integer page, Integer pageSize,String name, Short gender,LocalDate begin,LocalDate end) {//1. 设置分页参数PageHelper.startPage(page,pageSize);

//2. 执行查询    List empList = empMapper.list(name, gender, begin, end);    Page p = (Page) empList;    //3. 封装PageBean对象    PageBean pageBean = new PageBean(p.getTotal(), p.getResult());    return pageBean;}

10.文件上传----------------------------------------------前端3要素:form post; enctype=multipart/form-data,input type=file

10.1上传到服务器本地:

MultipartFile imageString originalFilename = image.getOriginalFilename(); //获取文件的文件名image.transferTo(new File("E:\images\"+newFileName));getSize();//获取文件的大小,单位字节getBytes();//获取文件内容的字节数组InputStream getInputStream();//获取接收到的文件内容的输入流

10.2 上传到云服务器:OSS 云对象存储服务@PostMapping("/upload")public Result upload(MultipartFile image) throws IOException {log.info("文件上传, 文件名: {}", image.getOriginalFilename());

//调用阿里云OSS工具类进行文件上传    String url = aliOSSUtils.upload(image);    log.info("文件上传完成,文件访问的url: {}", url);    return Result.success(url);}

application.yml 设置上传文件的大小限制:servlet:multipart:max-file-size: 10MBmax-request-size: 100MB

11.springboot 配置文件:application.properties

@Value("endpoint") 注解private String endpoint;

12.springboot 配置文件:application.xml 推荐使用;

大小写敏感,缩进只能使用空格,值前需空格;

表示注释;

数据格式:对象/map集合;数组/list集合/set集合: -java -c#

  1. springboot 配置文件:application.xml 简化:ConfigurationProperties(prefix = "aliyun.oss") //批量注解多个值

@Value("endpoint") 注解1个值

关键词: