SpringBoot学习01:准备工作

SpringBoot学习笔记

环境准备:

  1. jdk1.8
  2. maven3.6.1
  3. idea
  4. SpringBoot

新建完成后,点击运行,就可以启动SpringBoot项目。

Application同级目录下就是你的项目。

现在在Application同级目录下新建一个名为Controller的包,在包里新建一个HelloController的类,在类里添加以下内容:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello () {
        //调用业务,接收前端参数
        return "hello world";
    }
}

一个叫Hello的接口就写完了。

现在访问localhost: 8080/hello,可以看到页面上有一行Hello world,说明第一步完成了。

SpringBoot自动装配原理初探

自动配置:

pom.xml:

  1. spring-boot-dependencies:核心依赖在父工程中!
  2. 我们在写入或引入一些SpringBoot依赖的时候,不需要指定版本。

启动器:

  1. 说白了就是SpringBoot的启动场景。
  2. 比如spring-boot-starter-web,它就会帮我们自动导入web的环境的所有依赖。
  3. springbboot会将所有的功能场景,都变成一个一个启动器。

SpringBoot配置文件赋值

官方的配置太多了,了解原理,一通百通。

先把项目自带的properties删掉,新建一个application.yaml。

配置文件的作用:修改SpringBoot自动配置的默认值。

用xml配置端口:

<server>
	<port>8081<port>
</server>

用yaml配置端口:

server:
	port: 8081

yaml对空格的要求很严格,不能瞎写空格。

yaml可以直接给实体类赋值。

先在Pojo包里新建两个类:Person类和Dog类,并在类外面加上@Component的注解表示是Spring的组件。

Spring怎么给Component类赋值?

在对应的变量上面加上@value("xx")即可。

image-20210422184324058

下面尝试在SpringBoot自带的测试类里测试:

image-20210422184631491

运行测试类后可以发现赋值成功。

也可以用yaml赋值:

image-20210422185139404

同时在Person类里面加上ConfigurationProperties的注解,即可将Person类和yaml文件里的person绑定。

image-20210422185445824

把测试类修改成这样:

image-20210422190205432

运行测试类,显示赋值成功。

JSR303校验

在Java类前面加上@Validated注解。

再在对应变量前面加上@Email(message="邮箱格式错误"),就可以自动识别输入的内容是不是一个合法的电子邮箱地址。

SpringBoot Web开发

自动装配。

SpringBoot到底帮我们自动配置了什么?我们能不能进行修改?能修改哪些东西?能不能扩展?

  1. xxxxAutoConfiguration..向容器中自动配置组件。
  2. xxxxProperties:自动配置类,装配配置文件中自定义的一些内容。

要解决的问题:

  1. 导入静态资源
  2. 首页
  3. jsp,模板引擎Thymeleaf
  4. 装配扩展SpringMVC
  5. 增删改查
  6. 拦截器
  7. 国际化

静态资源

先新建一个项目。

搭项目之前,先写一个Controller,测试接口是否能够成功运行。

首页如何定制

员工信息管理系统

准备工作

先从网上拷贝对应的前端静态资源。

把html文件拷贝入templates,剩下的拷贝入static。

然后先不用数据库,自己仿造数据库。

先给idea安装lombok插件,并在pow.xml文件里引入lombok依赖。

然后新建部门类,只需要这样搞,有参无参构造函数和getter setter方法就全部搞定了。

image-20210422214812863

然后同样的方式新建员工类。

同时,员工类、部门类、员工DAO、部门DAO前都加上@Repository注解,表示被SpringBoot托管。

员工DAO:

package com.example.demo.dao;

import com.example.demo.pojo.Department;
import com.example.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeDao {


    private static Map<Integer, Employee> employees = null;
    //员工有所属的部门
    @Autowired
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<Integer,Employee>();

        employees.put(1001,new Employee(1001,"AA","A123@qq.com",1,new Department(101,"后勤部")));
        employees.put(1002,new Employee(1002,"AA","B123@qq.com",1,new Department(101,"后勤部")));
        employees.put(1003,new Employee(1003,"AA","C123@qq.com",1,new Department(101,"后勤部")));
        employees.put(1004,new Employee(1004,"AA","D123@qq.com",1,new Department(101,"后勤部")));
        employees.put(1005,new Employee(1005,"AA","E123@qq.com",1,new Department(101,"后勤部")));
    }


    //主键自增
    private static Integer initId = 1006;

    //增加一个员工
    public void save (Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }

        employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }

    //查询全部员工信息
    public Collection<Employee> getAll () {
        return employees.values();
    }

    //通过id查询员工
    public Employee getEmployeeById (Integer id) {
        return employees.get(id);
    }

    //删除员工
    public void delete (Integer id) {
        employees.remove(id);
    }
}

部门DAO:

package com.example.demo.dao;


import com.example.demo.pojo.Department;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//部门dao
@Repository
public class DepartmentDao {

    //模拟数据库中的数据

    private static Map<Integer, Department> departments = null;

    static {
        departments = new HashMap<Integer, Department>();

        departments.put(101, new Department(101, "教学部"));
        departments.put(102, new Department(102, "市场部"));
        departments.put(103, new Department(103, "教研部"));
        departments.put(104, new Department(104, "运营部"));
        departments.put(105, new Department(105, "后勤部"));

    }
        //获得所有部门信息
    public Collection<Department> getDepartments () {
        return departments.values();
    }

        //通过id得到部门
    public Department getDepartment (Integer id) {
        return departments.get(id);
    }
}

至此,准备工作全部完成。

原文地址:https://www.cnblogs.com/zhanglichen/p/14691834.html