Springboot入门
Springboot
- Springboot最大的特点就是自动装配
JSR303数据校验
1.举个栗子春暖花开
@Component //组件,方便springboot自动配置
@ConfigurationProperties(prefix = "person") //给实体类赋值
@Validated //数据验证
public class Person {
@Email(message="邮箱格式错误")
private String name; //name必须是邮箱格式
}
2.数据校验常见参数
@NotNull(message="名字不能为空")
private String userName;
@Max(value=120,message="年龄最大不能查过120")
private int age;
@Email(message="邮箱格式错误")
private String email;
空检查
@Null 验证对象是否为null
@NotNull 验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty 检查约束元素是否为NULL或者是EMPTY.
Booelan检查
@AssertTrue 验证 Boolean 对象是否为 true
@AssertFalse 验证 Boolean 对象是否为 false
长度检查
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内
@Length(min=, max=) string is between min and max included.
日期检查
@Past 验证 Date 和 Calendar 对象是否在当前时间之前
@Future 验证 Date 和 Calendar 对象是否在当前时间之后
@Pattern 验证 String 对象是否符合正则表达式的规则
自动装配的原理
-
SpringBoot启动会加载大量的自动配置类
-
我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中
-
我们再来看这个自动配置类中到底配置了哪些组件(只要我们要用的组件存在其中,我们就不需要再手动配置了)
-
给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可
- xxxxAutoConfigurartion:自动配置类 给容器中添加组件
- xxxxProperties:封装配置文件中相关属性 与我们application.yml文件要配置的是一一对应的
- 举个栗子春暖花开
@ConfigurationProperties(prefix = "server") public class ServerProperties { private Integer port; //... } 首先在autoconfigure文件下的META-INF里找到spring.factories文件 HttpEncodingAutoConfiguration-----------------ServerProperties-----------------server 所以我们才可以在application.yml文件中使用server.port 我们也可以通过在application.yml文件中写入debug=true 从而知道哪些自动配置类生效
一个简单的web项目
- 1.首页配置
- 所有页面的静态资源都需要使用thymeleaf接管
- 2.页面国际化
- 我们需要配置i18n文件 文件名不能变
- 我们如果需要在项目中进行按钮自动切换 我们需要自定义一个组件
LocaleResolver
- 记得将自己写的组件配置到spring容器
@bean
- 3.登录+拦截器
- 4.增删改查
- 员工列表展示
- 提取公共页面 放在commons文件夹下
- 提取:
th:fragment="sidebar"
- 插入:
th:replace="~{commons/commons::sidebar(active='main.html')}
- 插入可以使用replace也可以使用insert
- (active=‘main.html’)为插入过程中传参 相当于?active=‘main.html’ 这样可能保证标签高亮
- 提取:
- 列表循环展示
- 提取公共页面 放在commons文件夹下
- 添加员工
- 按钮提交
- 跳转到添加页面
- 添加员工成功
- 返回首页
- 员工列表展示
Springboot整合Mybatis
- 导入 MyBatis 所需要的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
- 配置数据库连接信息以及整合mybatis的配置
spring:
datasource:
username: root
password: 123qwe
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
#整合mybatis
mybatis:
type-aliases-package: com.kuang.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
- 测试数据库是否连接成功
package com.kuang;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
@SpringBootTest
class Springboot03ApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource);
System.out.println(dataSource.getConnection());
}
}
- 创建实体类
package com.kuang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
- DAO层(Mapper接口)
package com.kuang.mapper;
import com.kuang.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
//这个注解表示了这是一个mybatis的mapper类
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int deleteUser(int id);
}
- 接口实现类(Mapper映射文件) 一般放在resources目录下
<?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.kuang.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user;
</select>
<select id="queryUserById" resultType="User">
select * from user where id=#{id};
</select>
<insert id="addUser" parameterType="User">
insert into user(id,name,pwd) values(#{id},#{name},#{pwd});
</insert>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id};
</delete>
</mapper>
- controller类
package com.kuang.controller;
import com.kuang.mapper.UserMapper;
import com.kuang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList(){
List<User> userList=userMapper.queryUserList();
return userList;
}
@GetMapping("/queryUserById/{id}")
public User queryUserById(@PathVariable("id") int id){
User user=userMapper.queryUserById(id);
return user;
}
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new User(8,"阿毛","423562"));
return "add ok";
}
@GetMapping("/deleteUser")
private String deleteUser(){
userMapper.deleteUser(8);
return "delete ok";
}
}
- 测试
- 在浏览器中请求
http:localhost/8080/queryUserList
- 在浏览器中请求
总结
- 如果短期内准备使用Springboot+mybatis做项目的话 掌握这个框架怎么用就行了(其实很简单啦) 不用深究
- 推荐视频 B站:狂神说
- 推荐方法 1.5倍速 遇到重点放慢点 遇到源码讲解可以跳过
- 如果以后准备从事java后端开发 建议没事多看看源码 多看官方文档 多自己做总结 多参加一些论坛