• 吃透SpringBoo的这些t知识,你就已经超过90%的Java面试者了


    前言

    做 Java 开发,没有人敢小觑 Spring Boot 的重要性,现在出去面试,无论多小的公司 or 项目,都要跟你扯一扯 Spring Boot,扯一扯微服务,如果啃不下来,很可能就与大厂失之交臂。

    精通Spring Boot的原理实现的话,可以帮助你更好地职业进阶,学习前辈优秀的架构设计思想,总结出最优使用方案,绕过工作中遇到的很多坑。

    SpringBoot

    第一章 JavaConfig

    1. 为什么要使用 Spring Boot

      因为Spring, SpringMVC 需要使用的大量的配置文件 (xml文件)

      还需要配置各种对象,把使用的对象放入到spring容器中才能使用对象

      需要了解其他框架配置规则。

    2. SpringBoot 就相当于 不需要配置文件的Spring+SpringMVC。 常用的框架和第三方库都已经配置好了。

      拿来就可以使用了。

    3. SpringBoot开发效率高,使用方便多了

    1.1 JavaConfig

    JavaConfig: 使用java类作为xml配置文件的替代, 是配置spring容器的纯java的方式。 在这个java类这可以创建java对象,把对象放入spring容器中(注入到容器),

    使用两个注解:

    1)@Configuration : 放在一个类的上面,表示这个类是作为配置文件使用的。

    2)@Bean:声明对象,把对象注入到容器中。

    1. 例子:
    2. package comfig;
    3. import com.bjpowernode.vo.Student;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. /**
    7. * Configuration:表示当前类是作为配置文件使用的。 就是用来配置容器的
    8. * 位置:在类的上面
    9. *
    10. * SpringConfig这个类就相当于beans.xml
    11. */
    12. @Configuration
    13. public class SpringConfig {
    14. /**
    15. * 创建方法,方法的返回值是对象。 在方法的上面加入@Bean
    16. * 方法的返回值对象就注入到容器中。
    17. *
    18. * @Bean: 把对象注入到spring容器中。 作用相当于<bean>
    19. *
    20. * 位置:方法的上面
    21. *
    22. * 说明:@Bean,不指定对象的名称,默认是方法名是 id
    23. *
    24. */
    25. @Bean
    26. public Student createStudent(){
    27. Student s1 = new Student();
    28. s1.setName("张三");
    29. s1.setAge(26);
    30. s1.setSex("男");
    31. return s1;
    32. }
    33. /***
    34. * 指定对象在容器中的名称(指定<bean>的id属性)
    35. * @Bean的name属性,指定对象的名称(id)
    36. */
    37. @Bean(name = "lisiStudent")
    38. public Student makeStudent(){
    39. Student s2 = new Student();
    40. s2.setName("李四");
    41. s2.setAge(22);
    42. s2.setSex("男");
    43. return s2;
    44. }
    45. }

    1.2 @ImporResource

    @ImportResource 作用导入其他的xml配置文件, 等于 在xml

    <import resources="其他配置文件"/>
    

    例如:

    1. @Configuration
    2. @ImportResource(value ={ "classpath:applicationContext.xml","classpath:beans.xml"})
    3. public class SpringConfig {
    4. }

    1.3 @PropertyResource

    @PropertyResource: 读取properties属性配置文件。 使用属性配置文件可以实现外部化配置 ,

    在程序代码之外提供数据。

    步骤:

    1. 在resources目录下,创建properties文件, 使用k=v的格式提供数据
    2. 在PropertyResource 指定properties文件的位置
    3. 使用@Value(value="${key}")
    1. @Configuration
    2. @ImportResource(value ={ "classpath:applicationContext.xml","classpath:beans.xml"})
    3. @PropertySource(value = "classpath:config.properties")
    4. @ComponentScan(basePackages = "com.bjpowernode.vo")
    5. public class SpringConfig {
    6. }

    第二 章 Spring Boot

    2.1 介绍

    SpringBoot是Spring中的一个成员, 可以简化Spring,SpringMVC的使用。 他的核心还是IOC容器。

    特点:

    • Create stand-alone Spring applications

      创建spring应用

    • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

      内嵌的tomcat, jetty , Undertow

    • Provide opinionated 'starter' dependencies to simplify your build configuration

      提供了starter起步依赖,简化应用的配置。

      比如使用MyBatis框架 , 需要在Spring项目中,配置MyBatis的对象 SqlSessionFactory , Dao的代理对象

      在SpringBoot项目中,在pom.xml里面, 加入一个 mybatis-spring-boot-starter依赖

    • Automatically configure Spring and 3rd party libraries whenever possible

      尽可能去配置spring和第三方库。叫做自动配置(就是把spring中的,第三方库中的对象都创建好,放到容器中, 开发人员可以直接使用)

    • Provide production-ready features such as metrics, health checks, and externalized configuration

      提供了健康检查, 统计,外部化配置

    • Absolutely no code generation and no requirement for XML configuration

      不用生成代码, 不用使用xml,做配置

    2.2 创建Spring Boot项目

    2.2.1 第一种方式, 使用Spring提供的初始化器, 就是向导创建SpringBoot应用

    使用的地址: https://start.spring.io

    SpringBoot项目的结构:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-obbEjRya-1645601371946)(D:\course\25-SpringBoot\笔记\images\image-20210115152427829.png)]

    2.2.1 使用国内的地址

    https://start.springboot.io

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WUQwGegr-1645601371948)(D:\course\25-SpringBoot\笔记\images\image-20210115155556662.png)]

    2.3 注解的使用

    1. @SpringBootApplication
    2. 符合注解:由
    3. @SpringBootConfiguration
    4. @EnableAutoConfiguration
    5. @ComponentScan
    6. 1.@SpringBootConfiguration
    7. @Configuration
    8. public @interface SpringBootConfiguration {
    9. @AliasFor(
    10. annotation = Configuration.class
    11. )
    12. boolean proxyBeanMethods() default true;
    13. }
    14. 说明:使用了@SpringBootConfiguration注解标注的类,可以作为配置文件使用的,
    15. 可以使用Bean声明对象,注入到容器

    2.@EnableAutoConfiguration

    启用自动配置, 把java对象配置好,注入到spring容器中。例如可以把mybatis的对象创建好,放入到容器中

    3.@ComponentScan

    1. @ComponentScan 扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等。
    2. 默认扫描的包: @ComponentScan所在的类所在的包和子包。

    2.4 SpringBoot的配置文件

    配置文件名称: application

    扩展名有: properties( k=v) ; yml ( k: v)

    使用application.properties, application.yml

    例1:application.properties设置 端口和上下文

    1. #设置端口号
    2. server.port=8082
    3. #设置访问应用上下文路径, contextpath
    4. server.servlet.context-path=/myboot

    例2: application.yml

    1. server:
    2. port: 8083
    3. servlet:
    4. context-path: /myboot2

    2.5 多环境配置

    有开发环境, 测试环境, 上线的环境。

    每个环境有不同的配置信息, 例如端口, 上下文件, 数据库url,用户名,密码等等

    使用多环境配置文件,可以方便的切换不同的配置。

    使用方式: 创建多个配置文件, 名称规则: application-环境名称.properties(yml)

    创建开发环境的配置文件: application-dev.properties( application-dev.yml )

    创建测试者使用的配置: application-test.properties

    2.6 @ConfigurationProperties

    @ConfigurationProperties: 把配置文件的数据映射为java对象。

    属性:prefix 配置文件中的某些key的开头的内容。

    1. @Component
    2. @ConfigurationProperties(prefix = "school")
    3. public class SchoolInfo {
    4. private String name;
    5. private String website;
    6. private String address;
    7. public String getName() {
    8. return name;
    9. }
    10. public void setName(String name) {
    11. this.name = name;
    12. }
    13. public String getWebsite() {
    14. return website;
    15. }
    16. public void setWebsite(String website) {
    17. this.website = website;
    18. }
    19. public String getAddress() {
    20. return address;
    21. }
    22. public void setAddress(String address) {
    23. this.address = address;
    24. }
    25. @Override
    26. public String toString() {
    27. return "SchoolInfo{" +
    28. "name='" + name + '\'' +
    29. ", website='" + website + '\'' +
    30. ", address='" + address + '\'' +
    31. '}';
    32. }
    33. }

    application.properties

    1. #配置端口号
    2. server.port=8082
    3. #context-path
    4. server.servlet.context-path=/myboot
    5. #自定义key=value
    6. school.name=动力节点
    7. school.website=www
    8. school.address=北京的大兴区
    9. site=www

    2.7 使用jsp

    SpringBoot不推荐使用jsp ,而是使用模板技术代替jsp

    使用jsp需要配置:

    1) 加入一个处理jsp的依赖。 负责编译jsp文件

    1. <dependency>
    2. <groupId>org.apache.tomcat.embed</groupId>
    3. <artifactId>tomcat-embed-jasper</artifactId>
    4. </dependency>
    1. 如果需要使用servlet, jsp,jstl的功能
    1. <dependency>
    2. <groupId>javax.servlet</groupId>
    3. <artifactId>jstl</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>javax.servlet</groupId>
    7. <artifactId>javax.servlet-api</artifactId>
    8. </dependency>
    9. <dependency>
    10. <groupId>javax.servlet.jsp</groupId>
    11. <artifactId>javax.servlet.jsp-api</artifactId>
    12. <version>2.3.1</version>
    13. </dependency>
    1. 创建一个存放jsp的目录,一般叫做webapp

    ​ index.jsp

    1. 需要在pom.xml指定jsp文件编译后的存放目录。

    META-INF/resources

    5)创建Controller, 访问jsp

    6)在application.propertis文件中配置视图解析器

    2.8 使用容器

    你想通过代码,从容器中获取对象。

    通过SpringApplication.run(Application.class, args); 返回值获取容器。

    1. public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    2. return run(new Class[]{primarySource}, args);
    3. }
    4. ConfigurableApplicationContext : 接口,是ApplicationContext的子接口
    5. public interface ConfigurableApplicationContext extends ApplicationContext

    2.9 ComnandLineRunner 接口 , ApplcationRunner接口

    这两个接口都 有一个run方法。 执行时间在容器对象创建好后, 自动执行run()方法。

    可以完成自定义的在容器对象创建好的一些操作。

    1. @FunctionalInterface
    2. public interface CommandLineRunner {
    3. void run(String... args) throws Exception;
    4. }
    5. @FunctionalInterface
    6. public interface ApplicationRunner {
    7. void run(ApplicationArguments args) throws Exception;
    8. }

    第三章 Web组件

    讲三个内容: 拦截器, Servlet ,Filter

    3.1 拦截器

    拦截器是SpringMVC中一种对象,能拦截器对Controller的请求。

    拦截器框架中有系统的拦截器, 还可以自定义拦截器。 实现对请求预先处理。

    实现自定义拦截器:

    1. 创建类实现SpringMVC框架的HandlerInterceptor接口

       

    public interface HandlerInterceptor {
    default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    return true;
    }

    1. default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    2. }
    3. default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
    4. }

    }

    1. 2.需在SpringMVC的配置文件中,声明拦截器
    2. ```xml
    3. <mvc:interceptors>
    4. <mvc:interceptor>
    5. <mvc:path="url" />
    6. <bean class="拦截器类全限定名称"/>
    7. </mvc:interceptor>
    8. </mvc:interceptors>

    SpringBoot中注册拦截器:

    1. @Configuration
    2. public class MyAppConfig implements WebMvcConfigurer {
    3. //添加拦截器对象, 注入到容器中
    4. @Override
    5. public void addInterceptors(InterceptorRegistry registry) {
    6. //创建拦截器对象
    7. HandlerInterceptor interceptor = new LoginInterceptor();
    8. //指定拦截的请求uri地址
    9. String path []= {"/user/**"};
    10. //指定不拦截的地址
    11. String excludePath [] = {"/user/login"};
    12. registry.addInterceptor(interceptor)
    13. .addPathPatterns(path)
    14. .excludePathPatterns(excludePath);
    15. }
    16. }

    3.2 Servlet

    在SpringBoot框架中使用Servlet对象。

    使用步骤:

    1. 创建Servlet类。 创建类继承HttpServlet
    2. 注册Servlet ,让框架能找到Servlet

    例子:

    1.创建自定义Servlet

    1. //创建Servlet类
    2. public class MyServlet extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. doPost(req,resp);
    6. }
    7. @Override
    8. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    9. //使用HttpServletResponse输出数据,应答结果
    10. resp.setContentType("text/html;charset=utf-8");
    11. PrintWriter out = resp.getWriter();
    12. out.println("===执行的是Servlet==");
    13. out.flush();
    14. out.close();
    15. }
    16. }
    1. 注册Servlet
    1. @Configuration
    2. public class WebApplictionConfig {
    3. //定义方法, 注册Servlet对象
    4. @Bean
    5. public ServletRegistrationBean servletRegistrationBean(){
    6. //public ServletRegistrationBean(T servlet, String... urlMappings)
    7. //第一个参数是 Servlet对象, 第二个是url地址
    8. //ServletRegistrationBean bean =
    9. //new ServletRegistrationBean( new MyServlet(),"/myservlet");
    10. ServletRegistrationBean bean = new ServletRegistrationBean();
    11. bean.setServlet( new MyServlet());
    12. bean.addUrlMappings("/login","/test"); // <url-pattern>
    13. return bean;
    14. }
    15. }

    3.3 过滤器Filter

    Filter是Servlet规范中的过滤器,可以处理请求, 对请求的参数, 属性进行调整。 常常在过滤器中处理字符编码

    在框架中使用过滤器:

    1. 创建自定义过滤器类
    2. 注册Filter过滤器对象

    例子:

    1. // 自定义过滤器
    2. public class MyFilter implements Filter {
    3. @Override
    4. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    5. System.out.println("执行了MyFilter,doFilter ");
    6. filterChain.doFilter(servletRequest,servletResponse);
    7. }
    8. }

    注册Filter

    1. @Configuration
    2. public class WebApplicationConfig {
    3. @Bean
    4. public FilterRegistrationBean filterRegistrationBean(){
    5. FilterRegistrationBean bean = new FilterRegistrationBean();
    6. bean.setFilter( new MyFilter());
    7. bean.addUrlPatterns("/user/*");
    8. return bean;
    9. }
    10. }

    3.4 字符集过滤器

    CharacterEncodingFilter : 解决post请求中乱码的问题

    在SpringMVC框架, 在web.xml 注册过滤器。 配置他的属性。

    第一种方式:

    使用步骤:

    1. 配置字符集过滤器

      1. @Configuration
      2. public class WebSystemConfig {
      3. //注册Servlet
      4. @Bean
      5. public ServletRegistrationBean servletRegistrationBean(){
      6. MyServlet myServlet = new MyServlet();
      7. ServletRegistrationBean reg = new ServletRegistrationBean(myServlet,"/myservlet");
      8. return reg;
      9. }
      10. //注册Filter
      11. @Bean
      12. public FilterRegistrationBean filterRegistrationBean(){
      13. FilterRegistrationBean reg = new FilterRegistrationBean();
      14. //使用框架中的过滤器类
      15. CharacterEncodingFilter filter = new CharacterEncodingFilter();
      16. //指定使用的编码方式
      17. filter.setEncoding("utf-8");
      18. //指定request , response都使用encoding的值
      19. filter.setForceEncoding(true);
      20. reg.setFilter(filter);
      21. //指定 过滤的url地址
      22. reg.addUrlPatterns("/*");
      23. return reg;
      24. }
      25. }
    2. 修改application.properties文件, 让自定义的过滤器起作用

    1. #SpringBoot中默认已经配置了CharacterEncodingFilter。 编码默认ISO-8859-1
    2. #设置enabled=false 作用是关闭系统中配置好的过滤器, 使用自定义的CharacterEncodingFilter
    3. server.servlet.encoding.enabled=false

    第二种方式

    修改application.properties文件

    1. server.port=9001
    2. server.servlet.context-path=/myboot
    3. #让系统的CharacterEncdoingFilter生效
    4. server.servlet.encoding.enabled=true
    5. #指定使用的编码方式
    6. server.servlet.encoding.charset=utf-8
    7. #强制requestresponse都使用charset属性的值
    8. server.servlet.encoding.force=true

    第四章 ORM 操作 MySQL

    使用MyBatis框架操作数据, 在SpringBoot框架集成MyBatis

    使用步骤:

    1. mybatis起步依赖 : 完成mybatis对象自动配置, 对象放在容器中

    2. pom.xml 指定把src/main/java目录中的xml文件包含到classpath中

    3. 创建实体类Student

    4. 创建Dao接口 StudentDao , 创建一个查询学生的方法

    5. 创建Dao接口对应的Mapper文件, xml文件, 写sql语句

    6. 创建Service层对象, 创建StudentService接口和他的实现类。 去dao对象的方法。完成数据库的操作

    7. 创建Controller对象,访问Service。

    8. 写application.properties文件

      配置数据库的连接信息。

    第一种方式 : @Mapper

    @Mapper:放在dao接口的上面, 每个接口都需要使用这个注解。

    1. /**
    2. * @Mapper:告诉MyBatis这是dao接口,创建此接口的代理对象。
    3. * 位置:在类的上面
    4. */
    5. @Mapper
    6. public interface StudentDao {
    7. Student selectById(@Param("stuId") Integer id);
    8. }

    第二种方式 @MapperScan

    1. /**
    2. * @MapperScan: 找到Dao接口和Mapper文件
    3. * basePackages:Dao接口所在的包名
    4. */
    5. @SpringBootApplication
    6. @MapperScan(basePackages = {"com.bjpowernode.dao","com.bjpowernode.mapper"})
    7. public class Application {
    8. }

    第三种方式: Mapper文件和Dao接口分开管理

    现在把Mapper文件放在resources目录下

    1)在resources目录中创建子目录 (自定义的) , 例如mapper

    2)把mapper文件放到 mapper目录中

    3)在application.properties文件中,指定mapper文件的目录

    1. #指定mapper文件的位置
    2. mybatis.mapper-locations=classpath:mapper/*.xml
    3. #指定mybatis的日志
    4. mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    1. 在pom.xml中指定 把resources目录中的文件 , 编译到目标目录中
    1. <!--resources插件-->
    2. <resources>
    3. <resource>
    4. <directory>src/main/resources</directory>
    5. <includes>
    6. <include>**/*.*</include>
    7. </includes>
    8. </resource>
    9. </resources>

    第四个 事务

    Spring框架中的事务:

    1) 管理事务的对象: 事务管理器(接口, 接口有很多的实现类)

    ​ 例如:使用Jdbc或mybatis访问数据库,使用的事务管理器:DataSourceTransactionManager

    2 ) 声明式事务: 在xml配置文件或者使用注解说明事务控制的内容

    ​ 控制事务: 隔离级别,传播行为, 超时时间

    3)事务处理方式:

    ​ 1) Spring框架中的@Transactional

    ​ 2) aspectj框架可以在xml配置文件中,声明事务控制的内容

    SpringBoot中使用事务: 上面的两种方式都可以。

    1)在业务方法的上面加入@Transactional , 加入注解后,方法有事务功能了。

    2)明确的在 主启动类的上面 ,加入@EnableTransactionManager

    例子:

    1. /**
    2. * @Transactional: 表示方法的有事务支持
    3. * 默认:使用库的隔离级别, REQUIRED 传播行为; 超时时间 -1
    4. * 抛出运行时异常,回滚事务
    5. */
    6. @Transactional
    7. @Override
    8. public int addStudent(Student student) {
    9. System.out.println("业务方法addStudent");
    10. int rows = studentDao.insert(student);
    11. System.out.println("执行sql语句");
    12. //抛出一个运行时异常, 目的是回滚事务
    13. //int m = 10 / 0 ;
    14. return rows;
    15. }

    第五章 接口架构风格 —RESTful

    接口: API(Application Programming Interface,应用程序接口)是一些预先定义的接口(如函数、HTTP接口),或指软件系统不同组成部分衔接的约定。 用来提供应用程序与开发人员基于某软件或硬件得以访问的一组例程,而又无需访问源码,或理解内部工作机制的细节。

    接口(API): 可以指访问servlet, controller的url, 调用其他程序的 函数

    架构风格: api组织方式(样子)

    就是一个传统的: http://localhost:9002/mytrans/addStudent?name=lisi&age=26

    ​ 在地址上提供了 访问的资源名称addStudent, 在其后使用了get方式传递参数。

    5.1 REST

    RESTful架构风格

    1)REST : (英文: Representational State Transfer , 中文: 表现层状态转移)。

    REST:是一种接口的架构风格和设计的理念,不是标准。

    优点: 更简洁,更有层次

    表现层状态转移:

    ​ 表现层就是视图层, 显示资源的, 通过视图页面,jsp等等显示操作资源的结果。

    ​ 状态: 资源变化

    ​ 转移: 资源可以变化的。 资源能创建,new状态, 资源创建后可以查询资源, 能看到资源的内容,

    这个资源内容 ,可以被修改, 修改后资源 和之前的不一样。

    2)REST中的要素:

    用REST表示资源和对资源的操作。 在互联网中,表示一个资源或者一个操作。

    资源使用url表示的, 在互联网, 使用的图片,视频, 文本,网页等等都是资源。

    资源是用名词表示。

    对资源:

    ​ 查询资源: 看,通过url找到资源。

    ​ 创建资源: 添加资源

    ​ 更新资源:更新资源 ,编辑

    ​ 删除资源: 去除

    资源使用url表示,通过名词表示资源。

    ​ 在url中,使用名词表示资源, 以及访问资源的信息, 在url中,使用“ / " 分隔对资源的信息

    ​ http://localhost:8080/myboot/student/1001

    使用http中的动作(请求方式), 表示对资源的操作(CURD)

    GET: 查询资源 -- sql select

    ​ 处理单个资源: 用他的单数方式

    ​ http://localhost:8080/myboot/student/1001

    ​ http://localhost:8080/myboot/student/1001/1

    ​ 处理多个资源:使用复数形式

    ​ http://localhost:8080/myboot/students/1001/1002

    POST: 创建资源 -- sql insert

    ​ http://localhost:8080/myboot/student

    ​ 在post请求中传递数据

    1. <form action="http://localhost:8080/myboot/student" method="post">
    2. 姓名:<input type="text" name="name" />
    3. 年龄:<input type="text" name="age" />
    4. </form>

    PUT: 更新资源 -- sql update

    1. <form action="http://localhost:8080/myboot/student/1" method="post">
    2. 姓名:<input type="text" name="name" />
    3. 年龄:<input type="text" name="age" />
    4. <input type="hidden" name="_method" value="PUT" />
    5. </form>

    DELETE: 删除资源 -- sql delete

    1. ```xml

    删除1的数据
    ```

    需要的分页, 排序等参数,依然放在 url的后面, 例如

    http://localhost:8080/myboot/students?page=1&pageSize=20

    `

    3) 一句话说明REST:

    ​ 使用url表示资源 ,使用http动作操作资源。

    1. 注解

    @PathVariable : 从url中获取数据

    @GetMapping: 支持的get请求方式, 等同于 @RequestMapping( method=RequestMethod.GET)

    @PostMapping: 支持post请求方式 ,等同于 @RequestMapping( method=RequestMethod.POST)

    @PutMapping: 支持put请求方式, 等同于 @RequestMapping( method=RequestMethod.PUT)

    @DeleteMapping: 支持delete请求方式, 等同于 @RequestMapping( method=RequestMethod.DELETE)

    @RestController: 符合注解, 是@Controller 和@ResponseBody组合。

    ​ 在类的上面使用@RestController , 表示当前类者的所有方法都加入了 @ResponseBody

    1. Postman : 测试工具

      使用Postman : 可以测试 get ,post , put ,delete 等请求

    5.2 在页面中或者ajax中,支持put,delete请求

    在SpringMVC中 有一个过滤器, 支持post请求转为put ,delete

    过滤器: org.springframework.web.filter.HiddenHttpMethodFilter

    作用: 把请求中的post请求转为 put , delete

    实现步骤:

    1. application.properties(yml) : 开启使用 HiddenHttpMethodFilter 过滤器
    2. 在请求页面中,包含 _method参数, 他的值是 put, delete , 发起这个请求使用的post方式

    第六章 Redis

    Redis : 一个NoSQL数据库, 常用作 缓存使用 (cache)

    Redis的数据类型: string , hash ,set ,zset , list

    Redis是一个中间件: 是一个独立的服务器。

    java中著名的客户端: Jedis , lettuce , Redisson

    Spring,SpringBoot中有 一个RedisTemplate(StringRedisTemplate) ,处理和redis交互

    6.1 配置Windows版本的redis

    Redis-x64-3.2.100.rar 解压缩到一个 非中文 的目录

    redis-server.exe:服务端, 启动后,不要关闭

    redis-cli.exe:客户端, 访问redis中的数据

    redisclient-win32.x86_64.2.0.jar : Redis图形界面客户端

    执行方式: 在这个文件所在的目录, 执行 java -jar redisclient-win32.x86_64.2.0.jar

    RedisTemplate 使用的 lettuce 客户端库

    1. <!--redis起步依赖: 直接在项目中使用RedisTemplate(StringRedisTemplate)-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-data-redis</artifactId>
    5. </dependency>
    6. data-redis使用的 lettuce 客户端库
    7. 在程序中使用RedisTemplate类的方法 操作redis数据, 实际就是调用的lettuce 客户端的中的方法

    6.2 对比 StringRedisTemplate 和 RedisTemplate

    StringRedisTemplate : 把k,v 都是作为String处理, 使用的是String的序列化 , 可读性好

    RedisTemplate : 把k,v 经过了序列化存到redis。 k,v 是序列化的内容, 不能直接识别.

    ​ 默认使用的jdk序列化, 可以修改为前提的序列化

    序列化:把对象转化为可传输的字节序列过程称为序列化。

    反序列化:把字节序列还原为对象的过程称为反序列化。

    为什么需要序列化

    序列化最终的目的是为了对象可以跨平台存储,和进行网络传输。而我们进行跨平台存储和网络传输的方式就是IO,而我们的IO支持的数据格式就是字节数组。我们必须在把对象转成字节数组的时候就制定一种规则(序列化),那么我们从IO流里面读出数据的时候再以这种规则把对象还原回来(反序列化)。

    什么情况下需要序列化

    通过上面我想你已经知道了凡是需要进行“跨平台存储”和”网络传输”的数据,都需要进行序列化。

    本质上存储和网络传输 都需要经过 把一个对象状态保存成一种跨平台识别的字节格式,然后其他的平台才可以通过字节信息解析还原对象信息。

    序列化的方式

    序列化只是一种拆装组装对象的规则,那么这种规则肯定也可能有多种多样,比如现在常见的序列化方式有:

    JDK(不支持跨语言)、JSON、XML、Hessian、Kryo(不支持跨语言)、Thrift、Protofbuff、

    Student( name=zs, age=20) ---- { "name":"zs", "age":20 }

    java的序列化: 把java对象转为byte[], 二进制数据

    json序列化:json序列化功能将对象转换为 JSON 格式或从 JSON 格式转换对象。例如把一个Student对象转换为JSON字符串{"name":"李四", "age":29} ),反序列化(将JSON字符串 {"name":"李四", "age":29} 转换为Student对象)

    设置key或者value的序列化方式

    1. // 使用RedisTemplate ,在存取值之前,设置序列化
    2. // 设置 key 使用String的序列化
    3. redisTemplate.setKeySerializer( new StringRedisSerializer());
    4. // 设置 value 的序列化
    5. redisTemplate.setValueSerializer( new StringRedisSerializer());
    6. redisTemplate.opsForValue().set(k,v);

    第七章 SpringBoot集成Dubbo

    7.1 看 SpringBoot继承Dubbo的文档

    dubbo-spring-boot-project/README_CN.md at master · apache/dubbo-spring-boot-project · GitHub

    7.2 公共项目

    独立的maven项目: 定义了接口和数据类

    1. public class Student implements Serializable {
    2. private static final long serialVersionUID = 1901229007746699151L;
    3. private Integer id;
    4. private String name;
    5. private Integer age;
    6. }
    7. public interface StudentService {
    8. Student queryStudent(Integer id);
    9. }

    7.3 提供者

    创建SpringBoot项目

    1) pom.xml

    1. <dependencies>
    2. <!--加入公共项目的gav-->
    3. <dependency>
    4. <groupId>com.bjpowernode</groupId>
    5. <artifactId>022-interface-api</artifactId>
    6. <version>1.0.0</version>
    7. </dependency>
    8. <!--dubbo依赖-->
    9. <dependency>
    10. <groupId>org.apache.dubbo</groupId>
    11. <artifactId>dubbo-spring-boot-starter</artifactId>
    12. <version>2.7.8</version>
    13. </dependency>
    14. <!--zookeeper依赖-->
    15. <dependency>
    16. <groupId>org.apache.dubbo</groupId>
    17. <artifactId>dubbo-dependencies-zookeeper</artifactId>
    18. <version>2.7.8</version>
    19. <type>pom</type>
    20. <exclusions>
    21. <!-- 排除log4j依赖 -->
    22. <exclusion>
    23. <artifactId>slf4j-log4j12</artifactId>
    24. <groupId>org.slf4j</groupId>
    25. </exclusion>
    26. </exclusions>
    27. </dependency>
    28. </dependencies>

    2)实现接口

    1. /**
    2. * 使用dubbo中的注解暴露服务
    3. * @Component 可以不用加
    4. */
    5. @DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)
    6. public class StudentServiceImpl implements StudentService {
    7. @Override
    8. public Student queryStudent(Integer id) {
    9. Student student = new Student();
    10. if( 1001 == id){
    11. student.setId(1001);
    12. student.setName("------1001-张三");
    13. student.setAge(20);
    14. } else if(1002 == id){
    15. student.setId(1002);
    16. student.setName("#######1002-李四");
    17. student.setAge(22);
    18. }
    19. return student;
    20. }
    21. }

    3)application.properties

    1. #配置服务名称 dubbo:application name="名称"
    2. spring.application.name=studentservice-provider
    3. #配置扫描的包, 扫描的@DubboService
    4. dubbo.scan.base-packages=com.bjpowernode.service
    5. #配置dubbo协议
    6. #dubbo.protocol.name=dubbo
    7. #dubbo.protocol.port=20881
    8. #注册中心
    9. dubbo.registry.address=zookeeper://localhost:2181

    4)在启动类的上面

    1. @SpringBootApplication
    2. @EnableDubbo
    3. public class ProviderApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(ProviderApplication.class, args);
    6. }
    7. }

    7.4消费者

    创建SpringBoot项目

    1) pom.xml

    1. <dependencies>
    2. <!--加入公共项目的gav-->
    3. <dependency>
    4. <groupId>com.bjpowernode</groupId>
    5. <artifactId>022-interface-api</artifactId>
    6. <version>1.0.0</version>
    7. </dependency>
    8. <!--dubbo依赖-->
    9. <dependency>
    10. <groupId>org.apache.dubbo</groupId>
    11. <artifactId>dubbo-spring-boot-starter</artifactId>
    12. <version>2.7.8</version>
    13. </dependency>
    14. <!--zookeeper依赖-->
    15. <dependency>
    16. <groupId>org.apache.dubbo</groupId>
    17. <artifactId>dubbo-dependencies-zookeeper</artifactId>
    18. <version>2.7.8</version>
    19. <type>pom</type>
    20. <exclusions>
    21. <!-- 排除log4j依赖 -->
    22. <exclusion>
    23. <artifactId>slf4j-log4j12</artifactId>
    24. <groupId>org.slf4j</groupId>
    25. </exclusion>
    26. </exclusions>
    27. </dependency>
    28. </dependencies>
    1. 创建了Controller 或者 Service都可以
    1. @RestController
    2. public class DubboController {
    3. /**
    4. * 引用远程服务, 把创建好的代理对象,注入给studentService
    5. */
    6. //@DubboReference(interfaceClass = StudentService.class,version = "1.0")
    7. /**
    8. * 没有使用interfaceClass,默认的就是 引用类型的 数据类型
    9. */
    10. @DubboReference(version = "1.0")
    11. private StudentService studentService;
    12. @GetMapping("/query")
    13. public String queryStudent(Integer id){
    14. Student student = studentService.queryStudent(id);
    15. return "调用远程接口,获取对象:"+student;
    16. }
    17. }

    3)application.properties

    1. #指定服务名称
    2. spring.application.name=consumer-application
    3. #指定注册中心
    4. dubbo.registry.address=zookeeper://localhost:2181

    7.5 练习

    使用的技术: SpringBoot ,Dubbo, Redis, MyBatis

    Student表:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A4Mx9VVS-1645601371949)(D:\course\25-SpringBoot\笔记\images\image-20210119150418295.png)]

    CREATE TABLE student (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(255) COLLATE utf8_bin DEFAULT NULL,
    phone varchar(11) COLLATE utf8_bin DEFAULT NULL,
    age int(11) DEFAULT NULL,
    PRIMARY KEY (id)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

    1. 注册学生

    ​ phone必须唯一, 如果已经存在了手机号, 注册失败的。

    ​ int addStudent(Student student);

    ​ 返回值:int

    ​ 1: 注册成功

    ​ 2 : 手机号已经存在

    ​ name至少两个字符,

    ​ age 必须 大于 0

    2) 查询学生,根据id查询,此学生。

    ​ 先到redis查询学生, 如果redis没有此学生,从数据库查询, 把查询到的学生放入到redis。

    ​ 后面再次查询这个学生应该从redis就能获取到。

    ​ Student queryStudent(Integer id);

    1. 使用Dubbo框架, addStudent, queryStudent 是有服务提供者实现的。

    ​ 消费者可以是一个Controller , 调用提供者的两个方法。 实现注册和查询。

    4)页面使用html和ajax,jquery。

    ​ 在html页面中提供 form 注册学生, 提供文本框输入id,进行查询。

    ​ 注册和查询都使用ajax技术。

    ​ html,jquery.js都放到resources/static目录中

    第八章 打包

    8.1 打包war

    1.创建了一个jsp应用

    2.修改pom.xml

    1)指定打包后的文件名称

    1. <build>
    2. <!--打包后的文件名称-->
    3. <finalName>myboot</finalName>
    4. </build>

    2)指定jsp编译目录

    1. <!--resources插件, 把jsp编译到指定的目录-->
    2. <resources>
    3. <resource>
    4. <directory>src/main/webapp</directory>
    5. <targetPath>META-INF/resources</targetPath>
    6. <includes>
    7. <include>**/*.*</include>
    8. </includes>
    9. </resource>
    10. <!--使用了mybatis ,而且mapper文件放在src/main/java目录-->
    11. <resource>
    12. <directory>src/main/java</directory>
    13. <includes>
    14. <include>**/*.xml</include>
    15. </includes>
    16. </resource>
    17. <!--把src/main/resources下面的所有文件,都包含到classes目录-->
    18. <resource>
    19. <directory>src/main/resources</directory>
    20. <includes>
    21. <include>**/*.*</include>
    22. </includes>
    23. </resource>
    24. </resources>

    3)执行打包是war

    1. <!--打包类型-->
    2. <packaging>war</packaging>

    4)主启动类继承SpringBootServletInitializer

    1. /**
    2. * SpringBootServletInitializer: 继承这个类, 才能使用独立tomcat服务器
    3. */
    4. @SpringBootApplication
    5. public class JspApplication extends SpringBootServletInitializer {
    6. public static void main(String[] args) {
    7. SpringApplication.run(JspApplication.class, args);
    8. }
    9. @Override
    10. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    11. return builder.sources(JspApplication.class);
    12. }
    13. }

    5)部署war

    把war放到tomcat等服务器的发布目录中。 tomcat为例, myboot.war放到tomcat/webapps目录。

    8.2 打包为jar

    1.创建了一个包含了jsp的项目

    2.修改pom.xml

    ​ 1) 指定打包后的文件名称

    1. <build>
    2. <!--打包后的文件名称-->
    3. <finalName>myboot</finalName>
    4. </build>
    1. 指定springboot-maven-plugin版本
    1. <plugins>
    2. <plugin>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-maven-plugin</artifactId>
    5. <!--打包jar, 有jsp文件时,必须指定maven-plugin插件的版本是 1.4.2.RELEASE-->
    6. <version>1.4.2.RELEASE</version>
    7. </plugin>
    8. </plugins>

    3)最后执行 maven clean package

    ​ 在target目录中,生成jar 文件, 例子是myboot.jar

    ​ 执行独立的springboot项目 在cmd中 java -jar myboot.jar

    第九章 Thymeleaf 模板引擎

    Thymeleaf: 是使用java开发的模板技术, 在服务器端运行。 把处理后的数据发送给浏览器。

    ​ 模板是作视图层工作的。 显示数据的。 Thymeleaf是基于Html语言。 Thymleaf语法是应用在

    ​ html标签中 。 SpringBoot框架集成Thymealeaf, 使用Thymeleaf代替jsp。

    Thymeleaf 的官方网站:http://www.thymeleaf.org
    Thymeleaf 官方手册:Tutorial: Using Thymeleaf

    9.1 表达式

    1. 标准变量表达式

      语法: ${key}

      作用: 获取key对于的文本数据, key 是request作用域中的key , 使用request.setAttribute(), model.addAttribute()

      在页面中的 html标签中, 使用 th:text="${key}"

    1. <div style="margin-left: 400px">
    2. <h3>标准变量表达式: ${key}</h3>
    3. <p th:text="${site}">key不存在</p>
    4. <br/>
    5. <p>获取SysUser对象 属性值</p>
    6. <p th:text="${myuser.id}">id</p>
    7. <p th:text="${myuser.name}">姓名</p>
    8. <p th:text="${myuser.sex}">姓名:m男</p>
    9. <p th:text="${myuser.age}">年龄</p>
    10. <p th:text="${myuser.getName()}">获取姓名使用getXXX</p>
    11. </div>
    1. 选择变量表达式( 星号变量表达式)

      语法: *{key}

      作用: 获取这个key对应的数据, *{key}需要和th:object 这个属性一起使用。

      目的是简单获取对象的属性值。

      1. <p>使用 *{} 获取SysUser的属性值</p>
      2. <div th:object="${myuser}">
      3. <p th:text="*{id}"></p>
      4. <p th:text="*{name}"></p>
      5. <p th:text="*{sex}"></p>
      6. <p th:text="*{age}"></p>
      7. </div>
      8. <p>使用*{}完成的表示 对象的属性值</p>
      9. <p th:text="*{myuser.name}" ></p>
    2. 链接表达式

      语法: @{url}

      作用: 表示链接, 可以

      1. <script src="..."> , <link href="..."> <a href=".."> ,<form action="..."> <img src="...">

    9.2 Thymeleaf属性

    属性是放在html元素中的,就是html元素的属性,加入了th前缀。 属性的作用不变。 加入上th, 属性的值由模板引擎处理了。 在属性可以使用变量表达式

    例如:

    1. <form action="/loginServlet" method="post"></form>
    2. <form th:action="/loginServlet" th:method="${methodAttr}"></form>

    9.3 each

    each循环, 可以循环List,Array

    语法:

    在一个html标签中,使用th:each

    1. <div th:each="集合循环成员,循环的状态变量:${key}">
    2. <p th:text="${集合循环成员}" ></p>
    3. </div>
    4. 集合循环成员,循环的状态变量:两个名称都是自定义的。 “循环的状态变量”这个名称可以不定义,默认是"集合循环成员Stat"

    each循环Map

    在一个html标签中,使用th:each

    1. <div th:each="集合循环成员,循环的状态变量:${key}">
    2. <p th:text="${集合循环成员.key}" ></p>
    3. <p th:text="${集合循环成员.value}" ></p>
    4. </div>
    5. 集合循环成员,循环的状态变量:两个名称都是自定义的。 “循环的状态变量”这个名称可以不定义,默认是"集合循环成员Stat"
    6. key:map集合中的key
    7. value:map集合key对应的value值

    9.4 th:if

    "th:if" : 判断语句, 当条件为true, 显示html标签体内, 反之不显示 没有else语句

    1. 语法:
    2. <div th:if=" 10 > 0 "> 显示文本内容 </div>

    还有一个 th:unless 和 th:if相反的行为

    1. 语法:
    2. <div th:unless=" 10 < 0 "> 当条件为false显示标签体内容 </div>

    例子:if

    1. <div style="margin-left: 400px">
    2. <h3> if 使用</h3>
    3. <p th:if="${sex=='m'}">性别是男</p>
    4. <p th:if="${isLogin}">已经登录系统</p>
    5. <p th:if="${age > 20}">年龄大于20</p>
    6. <!--""空字符是true-->
    7. <p th:if="${name}">name是“”</p>
    8. <!--null是false-->
    9. <p th:if="${isOld}"> isOld是null</p>
    10. </div>

    例子: unless

    1. <div style="margin-left: 400px">
    2. <h3>unless: 判断条件为false,显示标签体内容</h3>
    3. <p th:unless="${sex=='f'}">性别是男的</p>
    4. <p th:unless="${isLogin}">登录系统</p>
    5. <p th:unless="${isOld}"> isOld是null </p>
    6. </div>

    9.5 th:switch

    th:switch 和 java中的swith一样的

    1. 语法:
    2. <div th:switch="要比对的值">
    3. <p th:case="值1">
    4. 结果1
    5. </p>
    6. <p th:case="值2">
    7. 结果2
    8. </p>
    9. <p th:case="*">
    10. 默认结果
    11. </p>
    12. 以上的case只有一个语句执行
    13. </div>

    9.6 th:inline

    1. 内联text: 在html标签外,获取表达式的值

      语法:

      1. <p>显示姓名是:[[${key}]]</p>
      2. <div style="margin-left: 400px">
      3. <h3>内联 text, 使用内联表达式显示变量的值</h3>
      4. <div th:inline="text">
      5. <p>我是[[${name}]],年龄是[[${age}]]</p>
      6. 我是<span th:text="${name}"></span>,年龄是<span th:text="${age}"></span>
      7. </div>
      8. <div>
      9. <p>使用内联text</p>
      10. <p>我是[[${name}]],性别是[[${sex}]]</p>
      11. </div>
      12. </div>
    2. 内联javascript

    1. 例子:
    2. <script type="text/javascript" th:inline="javascript">
    3. var myname = [[${name}]];
    4. var myage = [[${age}]];
    5. //alert("获取的模板中数据 "+ myname + ","+myage)
    6. function fun(){
    7. alert("单击事件,获取数据 "+ myname + ","+ [[${sex}]])
    8. }
    9. </script>

    9.7 字面量

    例子:

    1. <div style="margin-left: 400px">
    2. <h3>文本字面量: 使用单引号括起来的字符串</h3>
    3. <p th:text="'我是'+${name}+',我所在的城市'+${city}">数据显示</p>
    4. <h3>数字字面量</h3>
    5. <p th:if="${20>5}"> 20大于 5</p>
    6. <h3>boolean字面量</h3>
    7. <p th:if="${isLogin == true}">用户已经登录系统</p>
    8. <h3>null字面量</h3>
    9. <p th:if="${myuser != null}">有myuser数据</p>
    10. </div>

    9.8 字符串连接

    连接字符串有两种语法

    1) 语法使用 单引号括起来字符串 , 使用 + 连接其他的 字符串或者表达式

      <p th:text="'我是'+${name}+',我所在的城市'+${city}">数据显示</p>
    

    2)语法:使用双竖线, |字符串和表达式|

    1. <p th:text="|我是${name},我所在城市${city|">
    2. 显示数据
    3. </p>

    例子:

    1. <div style="margin-left: 400px">
    2. <h3>字符串连接方式1:使用单引号括起来的字符串</h3>
    3. <p th:text="'我是'+${name}+',我所在的城市'+${city}">数据显示</p>
    4. <br/>
    5. <br/>
    6. <h3>字符串连接方式2:|字符串和表达式|</h3>
    7. <p th:text="|我是${name},所在城市${city},其他人${myuser.name}|"></p>
    8. </div>

    9.9 运算符

    1. 算术运 算: + , - - , * , / , %
    2. 关系比较 : > , < , >= , <= ( gt , lt , ge , le )
    3. 相等判断: == , != ( eq , ne )
    4. <div style="margin-left: 400px">
    5. <h3>使用运算符</h3>
    6. <p th:text="${age > 10}">年龄大于 10 </p>
    7. <p th:text="${ 20 + 30 }">显示运算结果</p>
    8. <p th:if="${myuser == null}">myuser是null</p>
    9. <p th:if="${myuser eq null}">myuser是null</p>
    10. <p th:if="${myuser ne null}">myuser不是null</p>
    11. <p th:text="${isLogin == true ? '用户已经登录' : '用户需要登录'}"></p>
    12. <p th:text="${isLogin == true ? ( age > 10 ? '用户是大于10的' : '用户年龄比较小') : '用户需要登录'}"></p>
    13. </div>
    14. 三元运算符:
    15. 表达式 ? true的结果 : false的结果
    16. 三元运算符可以嵌套

    9.10 内置对象

    文档地址:Tutorial: Using Thymeleaf.

    request 表示 HttpServletRequest

    session 表示 HttpSession对象

    session 表示Map对象的, 是#session的简单表示方式, 用来获取session中指定的key的值

    ​ #session.getAttribute("loginname") == session.loginname

    这些是内置对象,可以在模板文件中直接使用。

    1. 例子:
    2. <div style="margin-left: 350px">
    3. <h3>内置对象#request,#session,session的使用</h3>
    4. <p>获取作用域中的数据</p>
    5. <p th:text="${#request.getAttribute('requestData')}"></p>
    6. <p th:text="${#session.getAttribute('sessionData')}"></p>
    7. <p th:text="${session.loginname}"></p>
    8. <br/>
    9. <br/>
    10. <h3>使用内置对象的方法</h3>
    11. getRequestURL=<span th:text="${#request.getRequestURL()}"></span><br/>
    12. getRequestURI=<span th:text="${#request.getRequestURI()}"></span><br/>
    13. getQueryString=<span th:text="${#request.getQueryString()}"></span><br/>
    14. getContextPath=<span th:text="${#request.getContextPath()}"></span><br/>
    15. getServerName=<span th:text="${#request.getServerName()}"></span><br/>
    16. getServerPort=<span th:text="${#request.getServerPort()}"></span><br/>
    17. </div>

    9.11 内置工具类

    内置工具类型: Thymeleaf自己的一些类,提供对string, date ,集合的一些处理方法

    dates: 处理日器的工具类

    numbers:处理数字的

    lists: 处理list集合的

    1. <div style="margin-left: 350px">
    2. <h3>日期类对象 #dates</h3>
    3. <p th:text="${#dates.format(mydate )}"></p>
    4. <p th:text="${#dates.format(mydate,'yyyy-MM-dd')}"></p>
    5. <p th:text="${#dates.format(mydate,'yyyy-MM-dd HH:mm:ss')}"></p>
    6. <p th:text="${#dates.year(mydate)}"></p>
    7. <p th:text="${#dates.month(mydate)}"></p>
    8. <p th:text="${#dates.monthName(mydate)}"></p>
    9. <p th:text="${#dates.createNow()}"></p>
    10. <br/>
    11. <h3>内置工具类#numbers,操作数字的</h3>
    12. <p th:text="${#numbers.formatCurrency(mynum)}"></p>
    13. <p th:text="${#numbers.formatDecimal(mynum,5,2)}"></p>
    14. <br/>
    15. <h3>内置工具类#strings,操作字符串</h3>
    16. <p th:text="${#strings.toUpperCase(mystr)}"></p>
    17. <p th:text="${#strings.indexOf(mystr,'power')}"></p>
    18. <p th:text="${#strings.substring(mystr,2,5)}"></p>
    19. <p th:text="${#strings.substring(mystr,2)}"></p>
    20. <p th:text="${#strings.concat(mystr,'---java开发的黄埔军校---')}"></p>
    21. <p th:text="${#strings.length(mystr)}"></p>
    22. <p th:text="${#strings.length('hello')}"></p>
    23. <p th:unless="${#strings.isEmpty(mystr)}"> mystring 不是 空字符串 </p>
    24. <br/>
    25. <h3>内置工具类#lists,操作list集合</h3>
    26. <p th:text="${#lists.size(mylist)}"></p>
    27. <p th:if="${#lists.contains(mylist,'a')}">有成员a</p>
    28. <p th:if="!${#lists.isEmpty(mylist)}"> list 集合有多个成员</p>
    29. <br/>
    30. <h3>处理null</h3>
    31. <p th:text="${zoo?.dog?.name}"></p>
    32. </div>

    9.12 自定义模板

    模板是内容复用, 定义一次,在其他的模板文件中多次使用。

    模板使用:

    1.定义模板

    2.使用模板

    模板定义语法:

    1. th:fragment="模板自定义名称"
    2. 例如:
    3. <div th:fragment="head">
    4. <p>
    5. 动力节点-java开发
    6. </p>
    7. <p>
    8. www
    9. </p>
    10. </div>

    引用模板语法:

    1. 1) ~{templatename :: selector}
    2. templatename: 文件名称
    3. selector: 自定义模板名称
    4. 2)templatename :: selector
    5. templatename: 文件名称
    6. selector: 自定义模板名称
    7. 对于使用模板:有包含模板(th:include), 插入模板(th:insert)

    第十章 总结

    10.1 注解

    Spring + SpringMVC + SpringBoot

    1. 创建对象的:
    2. @Controller: 放在类的上面,创建控制器对象,注入到容器中
    3. @RestController: 放在类的上面,创建控制器对象,注入到容器中。
    4. 作用:复合注解是@Controller , @ResponseBody, 使用这个注解类的,里面的控制器方法的返回值 都是数据
    5. @Service : 放在业务层的实现类上面,创建service对象,注入到容器
    6. @Repository : 放在dao层的实现类上面,创建dao对象,放入到容器。 没有使用这个注解,是因为现在使用MyBatis框 架, dao对象是MyBatis通过代理生成的。 不需要使用@Repository、 所以没有使用。
    7. @Component: 放在类的上面,创建此类的对象,放入到容器中。
    8. 赋值的:
    9. @Value : 简单类型的赋值, 例如 在属性的上面使用@Value("李四") private String name
    10. 还可以使用@Value,获取配置文件者的数据(properties或yml)。
    11. @Value("${server.port}") private Integer port
    12. @Autowired: 引用类型赋值自动注入的,支持byName, byType. 默认是byType 。 放在属性的上面,也可以放在构造 方法的上面。 推荐是放在构造方法的上面
    13. @Qualifer: 给引用类型赋值,使用byName方式。
    14. @Autowird, @Qualifer都是Spring框架提供的。
    15. @Resource : 来自jdk中的定义, javax.annotation。 实现引用类型的自动注入, 支持byName, byType.
    16. 默认是byName, 如果byName失败, 再使用byType注入。 在属性上面使用
    17. 其他:
    18. @Configuration : 放在类的上面,表示这是个配置类,相当于xml配置文件
    19. @Bean:放在方法的上面, 把方法的返回值对象,注入到spring容器中。
    20. @ImportResource : 加载其他的xml配置文件, 把文件中的对象注入到spring容器中
    21. @PropertySource : 读取其他的properties属性配置文件
    22. @ComponentScan: 扫描器 ,指定包名,扫描注解的
    23. @ResponseBody: 放在方法的上面,表示方法的返回值是数据, 不是视图
    24. @RequestBody : 把请求体中的数据,读取出来, 转为java对象使用。
    25. @ControllerAdvice: 控制器增强, 放在类的上面, 表示此类提供了方法,可以对controller增强功能。
    26. @ExceptionHandler : 处理异常的,放在方法的上面
    27. @Transcational : 处理事务的, 放在service实现类的public方法上面, 表示此方法有事务
    28. SpringBoot中使用的注解
    29. @SpringBootApplication : 放在启动类上面, 包含了@SpringBootConfiguration
    30. @EnableAutoConfiguration@ComponentScan
    31. MyBatis相关的注解
    32. @Mapper : 放在类的上面 , 让MyBatis找到接口, 创建他的代理对象
    33. @MapperScan :放在主类的上面 , 指定扫描的包, 把这个包中的所有接口都创建代理对象。 对象注入到容器中
    34. @Param : 放在dao接口的方法的形参前面, 作为命名参数使用的。
    35. Dubbo注解
    36. @DubboService: 在提供者端使用的,暴露服务的, 放在接口的实现类上面
    37. @DubboReference: 在消费者端使用的, 引用远程服务, 放在属性上面使用。
    38. @EnableDubbo : 放在主类上面, 表示当前引用启用Dubbo功能。

    引用模板语法:

    1. 1) ~{templatename :: selector}
    2. templatename: 文件名称
    3. selector: 自定义模板名称
    4. 2)templatename :: selector
    5. templatename: 文件名称
    6. selector: 自定义模板名称
    7. 对于使用模板:有包含模板(th:include), 插入模板(th:insert)

    第十章 总结

    10.1 注解

    Spring + SpringMVC + SpringBoot

    1. 创建对象的:
    2. @Controller: 放在类的上面,创建控制器对象,注入到容器中
    3. @RestController: 放在类的上面,创建控制器对象,注入到容器中。
    4. 作用:复合注解是@Controller , @ResponseBody, 使用这个注解类的,里面的控制器方法的返回值 都是数据
    5. @Service : 放在业务层的实现类上面,创建service对象,注入到容器
    6. @Repository : 放在dao层的实现类上面,创建dao对象,放入到容器。 没有使用这个注解,是因为现在使用MyBatis框 架, dao对象是MyBatis通过代理生成的。 不需要使用@Repository、 所以没有使用。
    7. @Component: 放在类的上面,创建此类的对象,放入到容器中。
    8. 赋值的:
    9. @Value : 简单类型的赋值, 例如 在属性的上面使用@Value("李四") private String name
    10. 还可以使用@Value,获取配置文件者的数据(properties或yml)。
    11. @Value("${server.port}") private Integer port
    12. @Autowired: 引用类型赋值自动注入的,支持byName, byType. 默认是byType 。 放在属性的上面,也可以放在构造 方法的上面。 推荐是放在构造方法的上面
    13. @Qualifer: 给引用类型赋值,使用byName方式。
    14. @Autowird, @Qualifer都是Spring框架提供的。
    15. @Resource : 来自jdk中的定义, javax.annotation。 实现引用类型的自动注入, 支持byName, byType.
    16. 默认是byName, 如果byName失败, 再使用byType注入。 在属性上面使用
    17. 其他:
    18. @Configuration : 放在类的上面,表示这是个配置类,相当于xml配置文件
    19. @Bean:放在方法的上面, 把方法的返回值对象,注入到spring容器中。
    20. @ImportResource : 加载其他的xml配置文件, 把文件中的对象注入到spring容器中
    21. @PropertySource : 读取其他的properties属性配置文件
    22. @ComponentScan: 扫描器 ,指定包名,扫描注解的
    23. @ResponseBody: 放在方法的上面,表示方法的返回值是数据, 不是视图
    24. @RequestBody : 把请求体中的数据,读取出来, 转为java对象使用。
    25. @ControllerAdvice: 控制器增强, 放在类的上面, 表示此类提供了方法,可以对controller增强功能。
    26. @ExceptionHandler : 处理异常的,放在方法的上面
    27. @Transcational : 处理事务的, 放在service实现类的public方法上面, 表示此方法有事务
    28. SpringBoot中使用的注解
    29. @SpringBootApplication : 放在启动类上面, 包含了@SpringBootConfiguration
    30. @EnableAutoConfiguration@ComponentScan
    31. MyBatis相关的注解
    32. @Mapper : 放在类的上面 , 让MyBatis找到接口, 创建他的代理对象
    33. @MapperScan :放在主类的上面 , 指定扫描的包, 把这个包中的所有接口都创建代理对象。 对象注入到容器中
    34. @Param : 放在dao接口的方法的形参前面, 作为命名参数使用的。
    35. Dubbo注解
    36. @DubboService: 在提供者端使用的,暴露服务的, 放在接口的实现类上面
    37. @DubboReference: 在消费者端使用的, 引用远程服务, 放在属性上面使用。
    38. @EnableDubbo : 放在主类上面, 表示当前引用启用Dubbo功能。

    关注关注,主页更多的java课程学习路线,笔记,面试等架构资料


     

     

  • 相关阅读:
    AWS SAP-C02教程0--课程概述
    MacOS设置JAVA_HOME环境变量
    【Graph Net学习】DeepWalk/Node2Vec实现Graph Embedding
    通过HbaseClient来写Phoenix表实现
    VL6 多功能数据处理器
    Linux网络编程- inet_pton()函数
    谈谈什么是缓存穿透,缓存击穿,缓存雪崩?怎么解决?
    查找bug的方法(随笔)
    微积分在金融投资的应用
    虹科分析 | 终端安全 | 移动目标防御是“变革性”技术——GARTNER
  • 原文地址:https://blog.csdn.net/uuqaz/article/details/125408430