狂神说Java SpringBoot
狂神说SpringBoot04:JSR303数据校验及多环境切换
狂神说SpringBoot17:Dubbo和Zookeeper集成
狂神说SpringBoot18:集成SpringSecurity
一、运行原理初探
-
注解:
//获取所有配置 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes);
获取候选配置:
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct."); return configurations; }
二、JSR303数据校验及多环境切换
-
配置文件加载位置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5Nr27xHV-1594913883189)(D:\我\MyBlog\狂神说Java SpringBoot.assets\image-20200709213144157.png)]
file : 文件路径,就是项目路径
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MmY445bN-
三、SpringBoot Web开发
自动装配:
SpringBoot到底帮我们配置了什么?我们能不能进行修改?能修改哪些东西?能不能扩展?
- xxxAutoConfiguration…向容器中自动配置组件
- xxxProperties:自动配置类,装配配置文件中自定义的一些内容
要解决的问题:
-
导入静态资源…
-
首页
-
jsp, 模板引擎Thymeleaf
thymeleaf依赖
-
装配扩展SpringMVC
-
增删改查
-
拦截器
-
国际化
四、员工管理系统
- 首页配置
- 注意点,所有页面的静态资源都需要使用thymeleaf接管;(导入thymeleaf依赖)
- url: @{}
- 页面国际化
- 我们需要配置i18n文件
- 我们如果需要在项目中进行按钮自动切换,我们需要自定义一个组件
LocaleResolver
- 记得将自己写的组件配置到spring容器
@Bean
- #{}
五、整合MyBatis
整合包
mybatis-spring-boot-starter
-
导入包
<!--引入mybatis,这是Mybatis官方提供的适配SpringBoot的,而不是SpringBoot自己的--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
-
配置yml文件
application.yml
# 配置spring自带的数据源 spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver # 整合mybatis mybatis: # 别名 type-aliases-package: com.kuang.pojo # mapper文件位置 mapper-locations: classpath:mybatis/mapper/*.xml
-
mybatis配置
-
User
@Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private String password; }
-
UserMapper接口
@Repository @Mapper public interface UserMapper { public User queryUserByName(String name); }
-
UserMapper.xml配置文件
<!--namespace=绑定一个指定的Dao/Mapper接口--> <mapper namespace="com.kuang.mapper.UserMapper"> <select id="getUserList" resultType="com.kuang.pojo.User" parameterType="String"> select * from USER where name = #{name} </select> </mapper>
-
-
编写sql
-
service层调用dao层
-
UserService 接口
public interface UserService { public User queryUserByName(String name); }
-
UserServiceImpl实现类
@Service public class UserServiceImpl implements UserService{ @Autowired UserMapper mapper; public User queryUserByName(String name) { User user = mapper.queryUserByName(name); return user; } }
-
-
controller调用service层
@Autowired UserServiceImpl userService; public void mian(String args[]){ User user = userService.queryUserByName("dog"); }
六、SpringSecurity
-
引入 Spring Security 模块
-
编写 Spring Security 配置类
参考官网:https://spring.io/projects/spring-security
-
编写基础配置类
- 定制请求的授权规则
- 定义认证规则
//AOP : 拦截器 @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // 链式编程 @Override