spring

Spring IOC

1.Spring提供的IOC容器的使用?

  • Spring给我们提供了核心IOC容器,提供了依赖注入(DI)的核心功能。所以使用Spring首先需要研究核心容器。

  • Spring的核心容器使用步骤

    • 导入依赖
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>5.3.11</version>
        </dependency>
      

      context = aop + beans + context + core + expression + jcl

    • 配置配置文件+交给Spring管理的类
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      
       xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans.xsd">
           <!-- 以上 规定.xml文件格式规范   --> 
      
          <!-- 向Spring的IOC容器中注册一个bean对象 ,对象类型是Student 标示[名字]是:stu-->
          <bean id="stu" class="com.etoak.student.entity.Student"></bean>
          <bean id="sch" class="com.etoak.student.entity.School"></bean>
      </beans>
      
    • 通过Spring提供的核心容器获得对象。
      • BeanFactory是Spring IOC容器的核心接口。其中所有的IOC容器可以认为都是实现了该接口的具体类。(该接口中提供了最核心的方法getBean

      • XmlBeanFactory:从XML文件获得IOC容器的bean工厂。也是BeanFactory接口的一个实现类。已过时 不推荐

      • ApplicationContext接口 来自于spring-context包,也是BeanFactory接口的子接口,与WEB容器配置更紧密,一般WEB开发使用ApplicationContext更多。

      • ClassPathXmlApplicationContext: 从类路径中获得资源的ApplicationContext(IOC容器).

      • AnnotationConfigApplicationContext:从注解中获得配置信息的ApplicationContext(IOC容器)

      • BeanFactory采用延迟加载,ApplicationContext采用的立即加载方式。延迟加载:当使用到对象才会创建,立即加载:不管是否使用到对象,只要创建IOC容器(ApplicationContext)对象就会创建所有的容器管理的对象
      • Spring管理的bean对象【就是注册到IOC容器中的】默认是单例的,可以通过Scope属性进行修改,Scope默认值是:singleton可以改成prototype
    • 工程名字预告:

      • spring_ioc_demo【小案例】

        controller----service----dao-----db

      • spring_ioc_student_xml【基于XML的配置】

      • spring_ioc_student_xml_annotation【基于xml+注解的混合配置】

      • spring_ioc_student_annotation【基于注解的配置】

2.关于Spring 配置文件的头信息?

<beans 
       xmlns:xmlnamespace:xml的命名空间
       命名空间:相当于JAVA中的package,避免命名的冲突
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       //schemaLocation:schema:XML的约束文件的位置
       xsi:schemaLocation=
       "http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd"> schame文件后缀名 xsd
  
  XML文档的约束文件有两种:
  	DTD文档:  语法简单 老  后缀:xx.dtd
  schema文档: 功能强大 新  后缀:xx.xsd
    <!-- 向Spring的IOC容器中注册一个bean对象 ,对象类型是Student 标示[名字]是:stu-->
    <bean id="stu" class="com.etoak.student.entity.Student"></bean>
    <bean id="sch" class="com.etoak.student.entity.School"></bean>

</beans>

3.SpringIOC容器构造对象的方式?

  • 无参构造方法
     <bean id="stu"  scope="prototype"
              class="com.etoak.student.entity.Student"></bean>
        
     <bean id="sch" class="com.etoak.student.entity.School"></bean>
    
  • 带参数构造方法
     <!--2.构造对象的方式:带参数构造方法-->
        <!--形参的名字-->
     <bean id="stu1" class="com.etoak.student.entity.Student">
         <constructor-arg name="username" value="etoak" />
         <constructor-arg name="pwd" value="abc" />
     </bean>
        <!--形参的类型-->
    <bean id="stu2" class="com.etoak.student.entity.Student">
     <constructor-arg type="java.lang.String" value="etoak" />
     <constructor-arg type="java.lang.String" value="abc" />
    </bean>
        <!--索引 下标-->
    <bean id="stu3" class="com.etoak.student.entity.Student">
    		<constructor-arg index="0" value="etoak" />
        <constructor-arg index="1" value="abc" />
    </bean>
    
  • 静态工厂
    <!-- 静态工厂:向IOC容器中注册bean对象,标示是stu4,
        类型是:StudentFactory 中getStu静态方法返回值-->
        <bean id="stu4"
              class="com.etoak.student.entity.StudentFactory"
              factory-method="getStu"></bean>
    
  • 实例工厂
     <!--非静态工厂 实例工厂方法-->
        <bean id="f" class="com.etoak.student.entity.StudentFactory"></bean>
        <bean id="stu5" factory-method="getStu1" factory-bean="f"></bean>
    
  • 实现FactoryBean接口的方式

    java

    public class StudentFactoryBean  implements FactoryBean<Student> {
        public StudentFactoryBean(){
            System.out.println("1111111111");
        }
        public Student getObject() throws Exception {
            System.out.println("非常复杂的构造对象的过程");
            return new Student();
        }
        public Class<?> getObjectType() {
            return Student.class;
        }
    }
    
    <!--向IOC容器中注册一个bean对象,标示、名字叫stu6
        对象是:(因为实现FactoryBean接口)StudentFactoryBean类中的getObject方法返回值-->
        <bean id="stu6" class="com.etoak.student.entity.StudentFactoryBean"></bean>
    
  • 注解

4.Spring IOC容器属性赋值的方式?

  • setter (前提! 属性有setter方法)
     <bean id="stu"  scope="prototype"
              class="com.etoak.student.entity.Student">
            <property name="username" value="etoak"></property>
            <property name="pwd" value="aaaa"></property>
            <property name="emails">
                <list>
                    <value>etoak@qq.com</value>
                    <value>et@qq.com</value>
                </list>
            </property>
            <property name="score">
                <map>
                    <entry key="corejava" value="100"></entry>
                    <entry key="db" value="120"></entry>
                    <entry key="spring" value="150"></entry>
                </map>
            </property>
            <property name="strs">
                <array>
                    <value>abc</value>
                    <value>dfa</value>
                </array>
            </property>
            <property name="sch" ref="sch"></property>
            <property name="properties">
                <props>
                    <prop key="key">value</prop>
                    <prop key="key1">value1</prop>
                </props>
            </property>
    </bean>
    
  • 构造方法
     <bean id="stu1" class="com.etoak.student.entity.Student">
            <constructor-arg name="a" value="etoak"></constructor-arg>
            <constructor-arg name="pwd" value="abc"></constructor-arg>
        </bean>
        <!--形参的类型-->
        <bean id="stu2" class="com.etoak.student.entity.Student">
            <constructor-arg type="java.lang.String" value="etoak"></constructor-arg>
            <constructor-arg type="java.lang.String" value="abc"></constructor-arg>
        </bean>
        <!--索引 下标-->
        <bean id="stu3" class="com.etoak.student.entity.Student">
            <constructor-arg index="0" value="etoak"></constructor-arg>
            <constructor-arg index="1" value="abc"></constructor-arg>
        </bean>
    
  • 注解

5. BeanFactory和FactoryBean的区别?

  • BeanFactory是Spring提供的IOC容器的核心接口。 getBean是其核心方法,用来创建、管理、获得bean对象。

  • FactoryBean是IOC容器的构造对象的方式之一。 当使用该接口时,我们写一个类实现该接口并且提供getObject方法,就可以像IOC容器中注册bean对象。一般XXFactoryBean就是用来提供 XX对象的,如:StudentFactoryBean就是用来提供Student对象的。

6. 混合版的Spring核心容器的使用?

  • 一部分类采用注解的方式交给IOC容器管理,另一个部分还是XML
  1. 导入依赖
  2. 建类 包
  3. 配置文件
  4. 注解
  5. 测试

7. Spring核心容器中的常用注解?

  1. 构造对象的注解
    1. @Component: 组件 相当于
      1. 一般用在类上
      2. value属性表示 向ioc容器中注册的bean对象的id,默认类名字首字母小写
    2. @Controller:一般用在controller类上
    3. @Service:一般用在service类上
    4. @Repository:一般用在Dao实现类上
    5. 以上注解全部来自 spring-context包
  2. 属性赋值的注解
    1. @Value:给普通属性赋值
    2. @Autowired:spring-beans包
      1. 默认按照类型到IOC容器中寻找对象,给属性赋值。
      2. 如果一个类型的对象有多个,则默认按照属性名字匹配【从ioc容器中寻找指定名字的对象】
      3. 如果通过@Qualifier注解指定了匹配的名字,则默认按照@Qualifier指定的名字匹配。
    3. @Resource: javaee的包
      1. 默认按照名字到IOC容器中寻找对象,给属性赋值
      2. 如果一个类型的对象有多个,则可以通过 name指定名字
      3. 也可以通过type指定类型
    4. /* @Autowired
          @Qualifier("studentServiceImpl")
          private IStudentService service;*/
      
         /*@Resource(name="studentServiceImpl")
         private IStudentService service1;*/
      
         //寻找类型是StudentServiceImpl2的对象给该属性赋值
         @Resource(type= StudentServiceImpl2.class)
         private IStudentService service1;
      

    8. Spring中的JdbcTemplate的常用操作?

    +++

    • update

    • query

    • 结果集的处理器 RowMapper ,相当于DBUtils中 ResultSetHandler.

      • 组装对象:BeanPropertyRowMapper

      • 组装Map:ColumnMapRowMapper

      • 单个数据:SingleColumnRowMapper

9. Spring WEB MVC的基本使用?

+++

  1. 导入依赖

    spring-context被spring-webmvc替换

    spring-jdbc

  2. 配置文件
    1. mvc:annotation-driven
  3. 注解
    1. @RequestMapping
    2. @ResponseBody :返回json
  4. web.xml 配置DispatcherServlet

10. 错误代码小全?

  • 404
  • 500
  • 405:方法不允许 如:需要POST 而使用的GET提交
  • 400: 参数格式错误
  • 304:读缓存 缓存没变
  • 302:重定向 response.sendRedirect(“”)
  • 406: 一般情况是 需要返回JSON,但是没有处理json的组件。之前我们自己使用阿里 的 fastjson处理json数据,而Spring中默认使用JACKSON组件处理json。

11.Spring MVC接收客户端传递过来的参数的方式?

  1. 最原始的使用HttpServletRequest +HttpServletResponse处理请求。【不推荐】

    可以直接在Spring的Controller中的方法中添加request和response对象,直接处理请求,生成响应

      @RequestMapping("/add") // /stu/add请求交给该方法处理
       @ResponseBody  //返回JSON数据
       public Student addStudent(HttpServletRequest req , HttpServletResponse resp){
            //System.out.println("Controller中的add...");
            //System.out.println(service1+name+"==>"+age);
          // service1.addStu(stu);
           return null;
       }
    
  2. 接收客户端的普通的key=value的格式的参数,可以通过基本类型、String等参数直接接收【特点:一个一个的拿】
    1. 客户端:name=value&age=value1

    2. Controller中的方法参数: (String name,int age){}

    3. 前端日期 ,后台指定格式通过:@DateTimeFormat(pattern=“yyyy-MM-dd”)

    4. @RequestParam注解可以指定哪一个前端参数 和当前controller中的额参数对应

    5. //~~~~~~~~~~~~~使用名字和客户端参数名字一致的参数 直接接收
         //客户端参数的格式: name=xxx&age=xx&email+xx&birth=xxx
         @RequestMapping("/add") // /stu/add请求交给该方法处理
         @ResponseBody  //返回JSON数据
      public Student addStudent(@RequestParam("username") String name,
            int age,String email,
           @DateTimeFormat(pattern = "yyyy-MM-dd") Date birth){
            //System.out.println("Controller中的add...");
            //System.out.println(service1+name+"==>"+age);
            // service1.addStu(stu);
      
            return null;
         }
      
  3. 客户端传递多个key=value参数时,controller可以使用对象接收
    1. 客户端传递 name=111&age=111形式的参数

    2. controller的方法中直接使用 对象接收

     //~~~~~~~~~~~~~~~~~~~~~
       @RequestMapping("/add") // /stu/add请求交给该方法处理
       @ResponseBody  //返回JSON数据
       public Student addStudent(Student stu ){
          //System.out.println("Controller中的add...");
          //System.out.println(service1+name+"==>"+age);
          // service1.addStu(stu);
    

    4.前端传递JSON,后端使用对象接收?


​ 客户端传递JSON对象,后台使用对象接收+@RquestBody==

​ 1. 客户端传递 JSON格式的参数
​ 2. 服务端 controller中 使用 对象接收 ,对象前+@RequestBody

12. 文件上传

1. 之前:我们使用第三方组件 解析请求。从请求中获得参数

2. `<form  enctype="multipart/form-data" method="POST">
   <input type="file" name="pic" />  
   </form>`

3. 在Spring webmvc中,完成文件上传还是 `commons-fileupload.` 

   ServletFileUpload  parseRequest()=>List<FileItem> getName

4. Spring中提供了文件上传解析器:`CommonsMultipartResolver`负责从第三方组件`commons-fileupload`中获得文件参数给controller中的方法中参数赋值。

5. 我们就像使用普通参数一样使用文件参数。

13. Spring 整合MyBatis案例?

  1. 导入依赖

    mybatis-spring整合包

    mysql-connector-java

    mybatis

    spring-webmvc + spring-jdbc+jackson-databind+commons-fileupload

    lombok

    servlet

    log4j

     <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>2.0.7</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.22</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.5.8</version>
        </dependency>
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.3.11</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>5.3.11</version>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.13.3</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
        </dependency>
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.24</version>
        </dependency>
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.4</version>
        </dependency>
    
  2. 配置文件+实体类+mapper
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
     <!--spring Ioc容器通过注解的方式扫描 注册的bean对象,以及扫描属性赋值的注解
     1.构造对象 @Component @Controller @Service @Respository
     2.属性赋值: @Value @Autowired @Resource
     -->
        <context:component-scan base-package="com.etoak.student"></context:component-scan>
        <!--引入外部配置文件-->
        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    
        <!--
        MVC中的注解:
            @RequestMapping:接收请求`
            @ResponseBody :返回JSON
            @RequestBody: 客户端JSON格式的参数组装成对象
            @RequestParam: 指定客户端传递的参数名字
        -->
        <mvc:annotation-driven></mvc:annotation-driven>
    
      <!--  <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="ds"></property>
        </bean>
        <bean  class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg name="sqlSessionFactory" ref="f"></constructor-arg>
        </bean>-->
        <!--1.Mybatis自动扫描指定的包中接口,自动使用动态代理创建对象
            2.自动把该对象注册到IOC容器中
    
            1.我们配置MappperScannerConfigurer 是为了让整合包帮我们自动的
            构造Mapper对象,并注入到IOC容器中,
    
            2.在构造对象时需要SqlSessionFacotry对象的。所以我们注册一个bean对象
            叫SqlSessionFactoryBean
    
            3.这两个bean对象都没有配置ID,说明这两个对象按照类型注入
           -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.etoak.student.mapper"></property>
        </bean>
    
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--数据源-->
            <property name="dataSource" ref="ds"></property>
            <!--给包起别名-->
            <property name="typeAliasesPackage" value="com.etoak.student.pojo"></property>
            <!--SQL映射文件-->
            <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"></property>
            <!--mybatis的配置文件的位置-->
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        </bean>
    
        <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${m.driver}"></property>
            <property name="url" value="${m.url}"></property>
            <property name="username" value="${m.user}"></property>
            <property name="password" value="${m.pwd}"></property>
        </bean>
    
    </beans>
    
  3. 注解
  4. web.xml
    1. DispatcherServlet

14.MyBatis分页插件?

参考: https://pagehelper.github.io/docs/howtouse/

1.导入依赖
 <!--分页组件-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.2.0</version>
    </dependency>
2.配置分页的interceptor
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="ds"></property>
        <!--给包起别名-->
        <property name="typeAliasesPackage" value="com.etoak.student.pojo"></property>
        <!--SQL映射文件-->
        <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"></property>
        <!--mybatis的配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 注意其他配置 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <value>
                            helperDialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
3.在service使用PageHelper.startPage分页

请添加图片描述

4.在controller中获得分页对象,交给PageInfo转换

请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值