栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot入门笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot入门笔记

文章目录
  • SpringBoot
  • 第一章 JavaConfig
    • 1.1 JavaConfig
    • 1.2 [@ImporResource ](/ImporResource )
    • 1.3 [@PropertyResource ](/PropertyResource )
  • 第二 章 Spring Boot
    • 2.1 介绍
    • 2.2 创建Spring Boot项目
      • 2.2.1 第一种方式, 使用Spring提供的初始化器, 就是向导创建SpringBoot应用
      • 2.2.1 使用国内的地址
    • 2.3 注解的使用
      • @SpringBootApplication
        • 1.@SpringBootConfiguration
        • 2.@EnableAutoConfiguration
        • 3.[@ComponentScan ](/ComponentScan )
    • 2.4 SpringBoot的配置文件
    • 2.5 多环境配置
    • 2.6 @ConfigurationProperties
    • 2.7 使用jsp
    • 2.8 使用容器
    • 2.9 ComnandLineRunner 接口 , ApplcationRunner接口
  • 第三章 Web组件
    • 3.1 拦截器
    • 3.2 Servlet
    • 3.3 过滤器Filter
    • 3.4 字符集过滤器
    • 路径中的*
      • / 和 @Configuration public class SpringConfig { @Bean public Student createStudent(){ Student s1 = new Student(); s1.setName("张三"); s1.setAge(26); s1.setSex("男"); return s1; } @Bean(name = "lisiStudent") public Student makeStudent(){ Student s2 = new Student(); s2.setName("李四"); s2.setAge(22); s2.setSex("男"); return s2; } } 1.2 @ImporResource

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

        
        

        例如:

        @Configuration
        @ImportResource(value ={ "classpath:applicationContext.xml","classpath:beans.xml"})
        public class SpringConfig {
        }
        
        1.3 @PropertyResource

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

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

        步骤:

        1. 在resources目录下,创建properties文件, 使用k=v的格式提供数据
        2. 在PropertyResource 指定properties文件的位置
        3. 使用@Value(value=“${key}”)
        @Configuration
        @ImportResource(value ={ "classpath:applicationContext.xml","classpath:beans.xml"})
        @PropertySource(value = "classpath:config.properties")
        @ComponentScan(basePackages = "com.bjpowernode.vo")
        public class SpringConfig {
        }
        
        第二 章 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项目的结构:

        2.2.1 使用国内的地址

        https://start.springboot.io

        2.3 注解的使用 @SpringBootApplication
        @SpringBootApplication
        复合注解:即由多个注解组成
        @SpringBootConfiguration
        @EnableAutoConfiguration
        @ComponentScan
        
        1.@SpringBootConfiguration
        @Configuration
        public @interface SpringBootConfiguration {
            @AliasFor(
                annotation = Configuration.class
            )
            boolean proxyBeanMethods() default true;
        }
        
        说明:使用了@SpringBootConfiguration注解标注的类,可以作为配置文件使用的,
            可以使用Bean声明对象,注入到容器
        
        2.@EnableAutoConfiguration

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

        3.@ComponentScan

        默认扫描的包: @ComponentScan所在的类所在的包和子包。因为启动类Application.java是在主包下,而启动类上面的注解又包含@ComponentScan ,所以就可以不用配置@ComponentScan,只要把类放在正确的位置,就会自动扫描。

        @ComponentScan 扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等。
        默认扫描的包: @ComponentScan所在的类所在的包和子包。
        
        2.4 SpringBoot的配置文件

        配置文件名称: application

        扩展名有: properties( k=v) ; yml ( k: v)yml这种格式的v前面是有空格的,注意!!

        使用application.properties, application.yml

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

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

        例2: application.yml

        server:
          port: 8083
          servlet:
            context-path: /myboot2
        

        如果一个项目中同时有application.yml和 application.properties,会以application.properties为准,application.yml会失效

        2.5 多环境配置

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

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

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

        使用方式: 创建多个配置文件, 名称规则: application-环境名称.properties(yml) (命名一定要根据这个规范)

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

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

        命名规范,下面激活使用的名字就直接写 - 后面的名字即可
        #激活使用的test环境
        spring:
          profiles:
            active: test
        
        
        2.6 @ConfigurationProperties

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

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

        @Component
        @ConfigurationProperties(prefix = "school")
        public class SchoolInfo {
        
            private String name;
        
            private String website;
        
            private String address;
        
        
            public String getName() {
                return name;
            }
        
            public void setName(String name) {
                this.name = name;
            }
        
            public String getWebsite() {
                return website;
            }
        
            public void setWebsite(String website) {
                this.website = website;
            }
        
            public String getAddress() {
                return address;
            }
        
            public void setAddress(String address) {
                this.address = address;
            }
        
            @Override
            public String toString() {
                return "SchoolInfo{" +
                        "name='" + name + ''' +
                        ", website='" + website + ''' +
                        ", address='" + address + ''' +
                        '}';
            }
        }
        

        application.properties

        #配置端口号
        server.port=8082
        #context-path
        server.servlet.context-path=/myboot
        
        #自定义key=value
        school.name=动力节点
        school.website=www.bjpowernode.com
        school.address=北京的大兴区
        
        site=www.bjpowernode.com
        
        2.7 使用jsp

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

        使用jsp需要配置:

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

        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
        1. 如果需要使用servlet, jsp,jstl的功能
        
        	javax.servlet
        	jstl
        
        
        
        	javax.servlet
        	javax.servlet-api
        
        
        
        javax.servlet.jsp
        	javax.servlet.jsp-api
        	2.3.1
        
        
        1. 创建一个存放jsp的目录,一般叫做webapp

          index.jsp

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

        META-INF/resources

        5)创建Controller, 访问jsp

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

        2.8 使用容器

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

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

        public static ConfigurableApplicationContext run(Class primarySource, String... args) {
                return run(new Class[]{primarySource}, args);
        }
        
        ConfigurableApplicationContext : 接口,是ApplicationContext的子接口
        public interface ConfigurableApplicationContext extends ApplicationContext
        
        2.9 ComnandLineRunner 接口 , ApplcationRunner接口

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

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

        @FunctionalInterface
        public interface CommandLineRunner {
            void run(String... args) throws Exception;
        }
        
        @FunctionalInterface
        public interface ApplicationRunner {
            void run(ApplicationArguments args) throws Exception;
        }
        
        package com.bjpowernode;
        
        import com.bjpowernode.service.HelloService;
        import org.springframework.boot.CommandLineRunner;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.util.ResourceUtils;
        
        import javax.annotation.Resource;
        
        @SpringBootApplication
        public class Application implements CommandLineRunner {
        
        	@Resource
        	private HelloService helloService;
        
        	public static void main(String[] args) {
        		System.out.println("准备创建容器对象");
        		//创建容器对象
        		SpringApplication.run(Application.class, args);
        		System.out.println("容器对象创建之后");
        	}
        
        	@Override
        	public void run(String... args) throws Exception {
        
        		String str  = helloService.sayHello("lisi");
        		System.out.println("调用容器中的对象="+str);
        		//可做自定义的操作,比如读取文件, 数据库等等
        		System.out.println("在容器对象创建后,执行的方法");
        	}
        }
        
        第三章 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;
        }
            default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        }
        
        default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        }}
        

        2.需在SpringMVC的配置文件中,声明拦截器

        
          
            
            
          
        
        

        SpringBoot中注册拦截器:

        @Configuration
        public class MyAppConfig implements WebMvcConfigurer {
        
            //添加拦截器对象, 注入到容器中
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
        
                //创建拦截器对象
                HandlerInterceptor interceptor = new LoginInterceptor();
        
                //指定拦截的请求uri地址
                String path []= {"/user
        public class LoginInterceptor implements HandlerInterceptor  {
        
            
            @Override
            public boolean preHandle(HttpServletRequest request,
                                     HttpServletResponse response,
                                     Object handler) throws Exception {
                System.out.println("执行了LoginInterceptor的preHandle");
                return true;
            }
        }
        
        3.2 Servlet

        在SpringBoot框架中使用Servlet对象。

        使用步骤:

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

        例子:

        1.创建自定义Servlet

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

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

        在框架中使用过滤器:

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

        例子:

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

        注册Filter

        @Configuration
        public class WebApplicationConfig {
        
            @Bean
            public FilterRegistrationBean filterRegistrationBean(){
                FilterRegistrationBean bean  = new FilterRegistrationBean();
                bean.setFilter( new MyFilter());
                bean.addUrlPatterns("/user
        @Mapper
        public interface StudentDao {
        
            Student selectById(@Param("stuId") Integer id);
        }
        
        第二种方式 @MapperScan

        用了@MapperScan,就可以不用写@ Mapper

        @SpringBootApplication
        @MapperScan(basePackages = {"com.bjpowernode.dao","com.bjpowernode.mapper"})
        public class Application {
        }
        
        第三种方式: Mapper文件和Dao接口分开管理

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

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

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

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

        #指定mapper文件的位置
        mybatis.mapper-locations=classpath:mapper*.*
              
           
        
        
        第四个 事务

        Spring框架中的事务:

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

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

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

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

        3)事务处理方式:

          1) Spring框架中的[@Transactional ](/Transactional ) 
        
          2)    aspectj框架可以在xml配置文件中,声明事务控制的内容
        

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

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

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

        例子:

        @Transactional
        @Override
        public int addStudent(Student student) {
            System.out.println("业务方法addStudent");
            int rows  =  studentDao.insert(student);
            System.out.println("执行sql语句");
        
            //抛出一个运行时异常, 目的是回滚事务
            //int m   = 10 / 0 ;
        
            return rows;
        }
        
        @SpringBootApplication
        @EnableTransactionManagement
        @MapperScan(basePackages = "com.bjpowernode.dao")
        public class Application {
        
        	public static void main(String[] args) {
        		SpringApplication.run(Application.class, args);
        	}
        }
        
        MyBatis生成器
        • pom.xml中
        
        			
        			
        				org.mybatis.generator
        				mybatis-generator-maven-plugin
        				1.3.6
        				
        					
        					GeneratorMapper.xml
        					true
        					true
        				
        			
        
        			
        				org.springframework.boot
        				spring-boot-maven-plugin
        			
        		
        
        • GeneratorMapper.xml
        
        
        
        
        
            
            
        
            
            
        
                
                
                    
                
        
                
                
                
        
                
                
                    
                    
                
        
                
                
                    
                
        
                
                
                    
                
        
                
                
        • 使用

        同名表的解决方案参考 https://www.lovesofttech.com/mybatis/MBGForMySQL8.html

        mybatis-generator (逆向工程)会生成实例以及实例对应的Example,其主要作用就是能够自己组装sql语句中where后面的条件。
        xxxExample example = new xxxExample();
        Criteria criteria = new Example().createCriteria();


        关于生成器的详细使用参考
        https://blog.csdn.net/qq_43318965/article/details/106665863
        https://blog.csdn.net/wkj888888/article/details/83748886
        https://blog.csdn.net/weixin_45606067/article/details/108427878

        第五章 接口架构风格 —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://localhost:8080/myboot/student/1001)
        

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

        GET: 查询资源 – sql select

                     处理单个资源: 用他的单数方式
        
                      [http://localhost:8080/myboot/student/1001](http://localhost:8080/myboot/student/1001)
        
                     [http://localhost:8080/myboot/student/1001/1](http://localhost:8080/myboot/student/1001/1)
        
                    处理多个资源:使用复数形式
        
                      [http://localhost:8080/myboot/students/1001/1002](http://localhost:8080/myboot/students/1001/1002)
        

        POST: 创建资源 – sql insert

                    [http://localhost:8080/myboot/student](http://localhost:8080/myboot/student)
        
                    在post请求中传递数据
        
        姓名: 年龄:

        PUT: 更新资源 – sql update

        姓名: 年龄:

        DELETE: 删除资源 – sql delete

        删除1的数据
        

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

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

        `

        3) 一句话说明REST:
        使用url表示资源 ,使用http动作操作资源。
        
        4)注解

        @PathVariable : 从url中获取数据

         @GetMapping("/student/{stuId}")
            public String  queryStudent(@PathVariable("stuId") Integer  studentId){
                return "查询学生studentId="+studentId;
            }
        //可以把@PathVariable中的路径参数stuId,赋值给方法里的参数studentId
        

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

          
            @GetMapping("/student/{stuId}")
            public String  queryStudent(@PathVariable("stuId") Integer  studentId){
                return "查询学生studentId="+studentId;
            }
        

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

           
            @PostMapping("/student/{name}/{age}")
            public String createStudent(@PathVariable("name") String name,
                                        @PathVariable("age") Integer age){
                return "创建资源 student: name="+name+"#age="+age;
            }
        
        

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

            
            @PutMapping("/student/{id}/{age}")
            public String modifyStudent(@PathVariable Integer id,
                                        @PathVariable Integer age){
                return "更新资源, 执行put请求方式: id="+id+"#age="+age;
        
            }
        

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

           
            @DeleteMapping("/student/{id}")
            public String removeStudentById(@PathVariable Integer id){
                return "删除资源,执行delete, id="+id;
            }
        
        

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

                   在类的上面使用[@RestController ](/RestController ) , 表示当前类者的所有方法都加入了 [@ResponseBody ](/ResponseBody ) 
        
        5)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 过滤器
        # 启用支持put 、delete方式的请求
        spring.mvc.hiddenmethod.filter.enabled=true
        
        1. 在请求页面中,包含 _method参数, 他的值是 put, delete , 发起这个请求使用的post方式
        //发起请求的方式是 post,但下面要加个隐藏域,表示真正要请求的方式
        
        第六章 Redis

        Redis : 一个NoSQL数据库, 常用作 缓存使用 (cache),NoSQL是指 Not only SQL

        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 客户端库

        
        
           org.springframework.boot
           spring-boot-starter-data-redis
        
        data-redis使用的   lettuce 客户端库
        
        在程序中使用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的序列化方式

        // 使用RedisTemplate ,在存取值之前,设置序列化
        // 设置 key 使用String的序列化
        redisTemplate.setKeySerializer( new StringRedisSerializer());
        
        // 设置 value 的序列化
        redisTemplate.setValueSerializer( new StringRedisSerializer());
        
        redisTemplate.opsForValue().set(k,v);
        
        • RedisController代码
        package com.bjpowernode.controller;
        
        import com.bjpowernode.vo.Student;
        import org.springframework.data.redis.core.RedisTemplate;
        import org.springframework.data.redis.core.StringRedisTemplate;
        import org.springframework.data.redis.core.ValueOperations;
        import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
        import org.springframework.data.redis.serializer.StringRedisSerializer;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.RestController;
        
        import javax.annotation.Resource;
        
        @RestController
        public class RedisController {
            
            
            @Resource
            private RedisTemplate redisTemplate;
            
            
            @Resource
            private StringRedisTemplate stringRedisTemplate;
            
            // 添加数据到redis
            @PostMapping("/redis/addstring")
            public String addToRedis(String name, String value){
                
                // 操作Redis中string类型的数据, 先获取ValueOperations对象
                ValueOperations valueOperations = redisTemplate.opsForValue();
                
                //添加数据到redis
                valueOperations.set("myname","lisi");
                
                return "向redis添加string类型的数据";
            }
            
            
            // 从redis获取数据
            @GetMapping("/redis/getk")
            public String getData(String k){
                ValueOperations valueOperations = redisTemplate.opsForValue();
                Object v = valueOperations.get(k);
                return "key是"+k+",他的值是:"+v;
            }
            
            
            @PostMapping("/redis/{k}/{v}")
            public String addStringKV(@PathVariable  String k,
                                      @PathVariable  String v){
                
                // 使用StringRedisTemplate对象
                stringRedisTemplate.opsForValue().set(k,v);
                return "使用StringRedisTemplate对象";
            }
            
            @GetMapping("/redis/getstr/{k}")
            public String getStringValue(@PathVariable  String k){
                
                String v  = stringRedisTemplate.opsForValue().get(k);
                return "k的value:"+v;
            }
            
            
            
            @PostMapping("/redis/addstr")
            public String  addString(String k,String v){
                // 使用RedisTemplate
                // 设置 key 使用String的序列化
                redisTemplate.setKeySerializer( new StringRedisSerializer());
                
                // 设置 value 的序列化
                redisTemplate.setValueSerializer( new StringRedisSerializer());
                
                redisTemplate.opsForValue().set(k,v);
                return "定义RedisTemplate对象的key,value的序列化";
                
            }
            
            
            
            @PostMapping("/redis/addjson")
            public String  addJson(){
                Student student  = new Student();
                student.setId(1001);
                student.setName("zhangsan");
                student.setAge(20);
                
                redisTemplate.setKeySerializer(new StringRedisSerializer());
                // 把值作为json序列化
                redisTemplate.setValueSerializer( new Jackson2JsonRedisSerializer(Student.class) );
                
                redisTemplate.opsForValue().set("mystudent", student);
                
                return "json序列化";
            }
            
            
            @PostMapping("/redis/getjson")
            public String  getJson(){
                
                
                redisTemplate.setKeySerializer(new StringRedisSerializer());
                // 把值作为json序列化
                redisTemplate.setValueSerializer( new Jackson2JsonRedisSerializer(Student.class) );
                
                Object obj  = redisTemplate.opsForValue().get("mystudent");
                
                return "json反序列化="+ obj;
            }
        }
        
        第七章 SpringBoot集成Dubbo 7.1 看 SpringBoot继承Dubbo的文档

        https://github.com/apache/dubbo-spring-boot-project/blob/master/README_CN.md

        • 步骤
        dubbo官方推荐至少需要3个工程
        1.接口工程:存放实体类和业务接口
        	maven java工程
        2.服务提供者:接口的实现类,该工程集成MyBatis和Spring
        	MyBatis依赖,MyBatis集成Spring依赖,MySQL驱动,Spring相关依赖,Dubbo依赖,
        	注册中心zookeeper依赖,Spring集成redis依赖
        	1.添加依赖
        	2.配置核心配置文件(数据库配置,redis配置,dubbo配置)
        	3.MyBatis逆向工程
        	4.接口的实现类
        3.服务消费者:处理用户浏览器的请求,该工程集成Spring和SpringMVC
        	Spring相关依赖,Dubbo依赖,注册中心zookeeper依赖
        	1.添加依赖
        	2.配置核心配置文件(dubbo配置,配置视图解析器)
        	3.编写控制层处理用户的请求
        
        
        7.2 公共项目

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

        public class Student implements Serializable {
            private static final long serialVersionUID = 1901229007746699151L;
        
            private Integer id;
            private String name;
            private Integer age;
        }
        
        public interface StudentService {
        
            Student queryStudent(Integer id);
        }
        
        7.3 提供者

        创建SpringBoot项目

        1) pom.xml

        
        
           
           
              com.bjpowernode
              022-interface-api
              1.0.0
           
        
           
           
              org.apache.dubbo
              dubbo-spring-boot-starter
              2.7.8
           
        
        
           
           
              org.apache.dubbo
              dubbo-dependencies-zookeeper
              2.7.8
              pom
              
                 
                 
                    slf4j-log4j12
                    org.slf4j
                 
              
           
        
        

        2)实现接口

        @DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)
        public class StudentServiceImpl implements StudentService {
            @Override
            public Student queryStudent(Integer id) {
                Student student  = new Student();
                if( 1001 == id){
                    student.setId(1001);
                    student.setName("------1001-张三");
                    student.setAge(20);
                } else if(1002  == id){
                    student.setId(1002);
                    student.setName("#######1002-李四");
                    student.setAge(22);
                }
        
                return student;
            }
        }
        

        3)application.properties

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

        4)在启动类的上面

        @SpringBootApplication
        @EnableDubbo
        public class ProviderApplication {
        
           public static void main(String[] args) {
              SpringApplication.run(ProviderApplication.class, args);
           }
        }
        
        7.4消费者

        创建SpringBoot项目

        1) pom.xml

        
        
           
           
              com.bjpowernode
              022-interface-api
              1.0.0
           
        
           
           
              org.apache.dubbo
              dubbo-spring-boot-starter
              2.7.8
           
        
        
           
           
              org.apache.dubbo
              dubbo-dependencies-zookeeper
              2.7.8
              pom
              
                 
                 
                    slf4j-log4j12
                    org.slf4j
                 
              
           
        
        
        1. 创建了Controller 或者 Service都可以
        @RestController
        public class DubboController {
        
            
            //@DubboReference(interfaceClass = StudentService.class,version = "1.0")
        
            
            @DubboReference(version = "1.0")
            private StudentService studentService;
        
            @GetMapping("/query")
            public String queryStudent(Integer id){
                Student student   = studentService.queryStudent(id);
                return "调用远程接口,获取对象:"+student;
            }
        }
        

        3)application.properties

        #指定服务名称
        spring.application.name=consumer-application
        #指定注册中心
        dubbo.registry.address=zookeeper://localhost:2181
        
        7.5 练习

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

        Student表:

        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目录中
        
        第八章 打包 pom文件中的resources配置
        
          
          
            src/main/java
            
              ***.*
            
          
        
        
        8.1 打包war

        1.创建了一个jsp应用

        2.修改pom.xml

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

        
           
           myboot
        
        

        2)指定jsp编译目录

        
        
           
              src/main/webapp
              META-INF/resources
              
                 ***.xml
              
           
        
           
           
              src/main/resources
              
                 **
        @SpringBootApplication
        public class JspApplication  extends SpringBootServletInitializer  {
        
           public static void main(String[] args) {
              SpringApplication.run(JspApplication.class, args);
           }
        
           @Override
           protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
              return builder.sources(JspApplication.class);
           }
        }
        

        5)部署war

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

        8.2 打包为jar

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

        2.修改pom.xml

         1) 指定打包后的文件名称
        
        
           
           myboot
        
        
        1. 指定springboot-maven-plugin版本
        
           
              org.springframework.boot
              spring-boot-maven-plugin
              
              1.4.2.RELEASE
           
        
        

        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 官方手册:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

        9.1 表达式
        1. 标准变量表达式
          语法: k e y 作 用 : 获 取 k e y 对 于 的 文 本 数 据 , k e y 是 r e q u e s t 作 用 域 中 的 k e y , 使 用 r e q u e s t . s e t A t t r i b u t e ( ) , m o d e l . a d d A t t r i b u t e ( ) 在 页 面 中 的 h t m l 标 签 中 , 使 用 t h : t e x t = " {key} 作用: 获取key对于的文本数据, key 是request作用域中的key , 使用request.setAttribute(), model.addAttribute() 在页面中的 html标签中, 使用 th:text=" key作用:获取key对于的文本数据,key是request作用域中的key,使用request.setAttribute(),model.addAttribute()在页面中的html标签中,使用th:text="{key}"
        标准变量表达式: ${key}

        key不存在


        获取SysUser对象 属性值

        id

        姓名

        姓名:m男

        年龄

        获取姓名使用getXXX

        1. 选择变量表达式( 星号变量表达式)
          语法: *{key}
          作用: 获取这个key对应的数据, *{key}需要和th:object 这个属性一起使用。
          目的是简单获取对象的属性值。

        使用 *{} 获取SysUser的属性值

        使用*{}完成的表示 对象的属性值

        1. 链接表达式
          语法: @{url}
          作用: 表示链接, 可以
         
        
        9.7 字面量

        例子:

         
        文本字面量: 使用单引号括起来的字符串

        数据显示

        数字字面量

        20大于 5

        boolean字面量

        用户已经登录系统

        null字面量

        有myuser数据

        9.8 字符串连接

        连接字符串有两种语法

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

          

        数据显示

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

        显示数据

        例子:

            
        字符串连接方式1:使用单引号括起来的字符串

        数据显示



        字符串连接方式2:|字符串和表达式|

        9.9 运算符
        算术运 算: + , - - , * , / , %
        关系比较 : > , < , >= , <= ( gt , lt , ge , le )
        相等判断: == , != ( eq , ne )
        
        
        
        使用运算符

        年龄大于 10

        显示运算结果

        myuser是null

        myuser是null

        myuser不是null

        三元运算符: 表达式 ? true的结果 : false的结果 三元运算符可以嵌套
        9.10 内置对象

        文档地址:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#web-context-namespaces-for-requestsession-attributes-etc.

        #request 表示 HttpServletRequest

        #session 表示 HttpSession对象

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

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

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

        例子:
         
        内置对象#request,#session,session的使用

        获取作用域中的数据



        使用内置对象的方法 getRequestURL=
        getRequestURI=
        getQueryString=
        getContextPath=
        getServerName=
        getServerPort=
        9.11 内置工具类

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

        #dates: 处理日器的工具类

        #numbers:处理数字的

        #lists: 处理list集合的

        日期类对象 #dates


        内置工具类#numbers,操作数字的


        内置工具类#strings,操作字符串

        mystring 不是 空字符串


        内置工具类#lists,操作list集合

        有成员a

        list 集合有多个成员


        处理null

        9.12 自定义模板

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

        模板使用:

        1.定义模板

        2.使用模板

        模板定义语法:

        th:fragment="模板自定义名称"
        
        例如:
        

        动力节点-java开发

        www.bjpowernode.com

        引用模板语法:

        1) ~{templatename :: selector}
           templatename:  文件名称
           selector: 自定义模板名称
        2)templatename :: selector
           templatename:  文件名称
           selector: 自定义模板名称
        
        对于使用模板:有包含模板(th:include), 插入模板(th:insert)
        
        第十章 总结 10.1 注解

        Spring + SpringMVC + SpringBoot

        创建对象的:
        @Controller: 放在类的上面,创建控制器对象,注入到容器中
        @RestController: 放在类的上面,创建控制器对象,注入到容器中。
                     作用:复合注解是@Controller , @ResponseBody, 使用这个注解类的,里面的控制器方法的返回值                   都是数据
        
        @Service : 放在业务层的实现类上面,创建service对象,注入到容器
        @Repository : 放在dao层的实现类上面,创建dao对象,放入到容器。 没有使用这个注解,是因为现在使用MyBatis框               架,  dao对象是MyBatis通过代理生成的。 不需要使用@Repository、 所以没有使用。
        @Component:  放在类的上面,创建此类的对象,放入到容器中。 
        
        赋值的:
        @Value : 简单类型的赋值, 例如 在属性的上面使用@Value("李四") private String name
                  还可以使用@Value,获取配置文件者的数据(properties或yml)。 
                  @Value("${server.port}") private Integer port
        
        @Autowired: 引用类型赋值自动注入的,支持byName, byType. 默认是byType 。 放在属性的上面,也可以放在构造             方法的上面。 推荐是放在构造方法的上面
        @Qualifer:  给引用类型赋值,使用byName方式。   
                    @Autowird, @Qualifer都是Spring框架提供的。
        
        @Resource : 来自jdk中的定义, javax.annotation。 实现引用类型的自动注入, 支持byName, byType.
                     默认是byName, 如果byName失败, 再使用byType注入。 在属性上面使用
        
        
        其他:
        @Configuration : 放在类的上面,表示这是个配置类,相当于xml配置文件
        
        @Bean:放在方法的上面, 把方法的返回值对象,注入到spring容器中。
        
        @ImportResource : 加载其他的xml配置文件, 把文件中的对象注入到spring容器中
        
        @PropertySource : 读取其他的properties属性配置文件
        
        @ComponentScan: 扫描器 ,指定包名,扫描注解的
        
        @ResponseBody: 放在方法的上面,表示方法的返回值是数据, 不是视图
        @RequestBody : 把请求体中的数据,读取出来, 转为java对象使用。
        
        @ControllerAdvice:  控制器增强, 放在类的上面, 表示此类提供了方法,可以对controller增强功能。
        
        @ExceptionHandler : 处理异常的,放在方法的上面
        
        @Transcational :  处理事务的, 放在service实现类的public方法上面, 表示此方法有事务
        
        
        SpringBoot中使用的注解
            
        @SpringBootApplication : 放在启动类上面, 包含了@SpringBootConfiguration
                                  @EnableAutoConfiguration, @ComponentScan
        
        
            
        MyBatis相关的注解
        
        @Mapper : 放在类的上面 , 让MyBatis找到接口, 创建他的代理对象    
        @MapperScan :放在主类的上面 , 指定扫描的包, 把这个包中的所有接口都创建代理对象。 对象注入到容器中
        @Param : 放在dao接口的方法的形参前面, 作为命名参数使用的。
            
        Dubbo注解
        @DubboService: 在提供者端使用的,暴露服务的, 放在接口的实现类上面
        @DubboReference:  在消费者端使用的, 引用远程服务, 放在属性上面使用。
        @EnableDubbo : 放在主类上面, 表示当前引用启用Dubbo功能。
        
        转载请注明:文章转载自 www.mshxw.com
        我们一直用心在做
        关于我们 文章归档 网站地图 联系我们

        版权所有 (c)2021-2022 MSHXW.COM

        ICP备案号:晋ICP备2021003244-6号