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
@AllArgsConstructor
public 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")
@RestController
public class DeptController {

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

    /**
     * 查询部门数据
     * @return
     */
    //@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        //调用service查询部门数据
        List<Dept> deptList =  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
     */
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门: {}" , dept);
        //调用service新增部门
        deptService.add(dept);
        return Result.success();
    }
}

7-2--------DeptServiceImpl.java

package 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;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Override
    public void delete(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        deptMapper.insert(dept);
    }
}

7-3------------------interface DeptService

package com.itheima.service;

import com.itheima.pojo.Dept;

import java.util.List;

/**
 * 部门管理
 */
public interface DeptService {
    /**
     * 查询全部部门数据
     * @return
     */
    List<Dept> list();

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

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


7-4----------interface DeptMapper
package 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;

/**
 * 部门管理
 */
@Mapper
public interface DeptMapper {
    /**
     * 查询全部部门
     * @return
     */
    @Select("select * from dept")
    List<Dept> list();

    /**
     * 根据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;

/**
 * 员工管理
 */
@Mapper
public interface EmpMapper {

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

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

    /**
     * 员工信息查询
     * @return
     */
    //@Select("select * from emp")
    public List<Emp> list(String name, Short gender,LocalDate begin,LocalDate end);

    /**
     * 批量删除
     * @param ids
     */
    void delete(List<Integer> ids);
}

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

<?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="com.itheima.mapper.EmpMapper">
    <!--批量删除员工 (1, 2, 3)-->
    <delete id="delete">
        delete
        from emp
        where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

    <!--条件查询-->
    <select id="list" resultType="com.itheima.pojo.Emp">
        select *
        from emp
        <where>
            <if test="name != null and name != ''">
                name like concat('%',#{name},'%')
            </if>
            <if test="gender != null">
                and gender = #{gender}
            </if>
            <if test="begin != null and end != null">
                and entrydate between #{begin} and #{end}
            </if>
        </where>
        order by update_time desc
    </select>

</mapper>


8.PageHelper分页插件  

PageHelper.startPage(page,pagesize)
引入依赖:
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.2</version>
        </dependency>


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

        //2. 执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) 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 image
 String 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: 10MB
max-request-size: 100MB



11.springboot 配置文件:application.properties

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



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

大小写敏感,
缩进只能使用空格,
值前需空格;
#表示注释;
数据格式:
    对象/map集合;
     数组/list集合/set集合:  -java  -c#

13. springboot 配置文件:application.xml 简化:

ConfigurationProperties(prefix = "aliyun.oss") //批量注解多个值

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

posted @ 2023-06-23 22:51  大树2  阅读(152)  评论(0编辑  收藏  举报