【自用】ssm整合【写的特别烂】

完了,文件下载一点都记不起来了,等用的时候再说吧

这种有继承又实现的东西,我是真搞不定,我好想用过,上勾股NB

listable 什么意思 可列出 是遍历的意思 可遍历的

这是真正的容器对象

这个东西是干什么来着的?

AnnotationConfigApplicationContext,基于java配置文件。

在spring3 最后一节课里面,都忘了,没有敲的东西都忘了,唉。。

但是还可以复习,别害怕

在这里插入图片描述

笑死了,支持web环境的容器,原来是里面持有了一个servlet上下文

我以为啥核心技术呢,就是持有一个servlet上下文容器对象

在这里插入图片描述

servlet 容器

我真是大无语了,姐妹们,这个就不能整点高端东西吗,我以为这个是把配置文件读成一长条的字符串,原来就读了个名字,我真是无语死了

在这里插入图片描述

我的头要炸了,我先自己捋一下

监听器加载的那个是没有 web的容器 存到servlet上下文里面
它的子类 那个分发器 从servlet上下文里面拿到容器 作为父类 生成子类 加载的是 有web的容器

等会上面那个可能是错的

就是 Spring 监听器 加载了 spring 有了父容器 但是这个没有针对控制层那些东西的应用,就是普通的spring容器

SpringWeb 需要中央分发器 用于控制层的那些东西 中央分发器 持有 父容器 自己new了一个 子容器,用于处理控制层的那些东西
主要的是这个
在这里插入图片描述

子类型的东西比较多,java继承

monitor 班长 监视器

同步锁有我也忘了,CK

在这里插入图片描述

delegate 代理的意思

拿过滤器bean

在这里插入图片描述

在这里执行过滤方法

在这里插入图片描述

过滤方法

在这里插入图片描述

过滤器是在servlet之前执行的,所以过滤器 在初始化的时候,是没有子容器的

select ‘x’ 这个是测试连接池可不可用的,真的惊呆了,还可以这样

1229ssm整合

1.springmvc与spring整合

timemillis 毫秒

millis 毫秒

eviction 驱逐 viction 胜利

初始连接数设置为项目组的开发人员数,每个人一个连接

在这里插入图片描述

从池子里面拿连接和放连接的时候不要检测有效性,会降低性能,空闲的时候检测一下就好了

当这个bean被实例化完成以后,立马执行的初始化方法

@PostContract init 和 InitializingBean接口

在这里插入图片描述
在这里插入图片描述

assert 明确肯定

不为null且不为空字符串

在这里插入图片描述
在这里插入图片描述

他这个连接池用的是自己注入的值,可以指定

在这里插入图片描述
在这里插入图片描述

  • 添加spring与springmvc的jar包环境;
  • 创建spring.xml与springmvc.xml,各自扫描不同的注解,springmvc.xml只扫描Controller;sprng扫描其他所有
spring.xml
  <context:component-scan base-package="com.javasm">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

    <import resource="cors.xml"></import>
springmvc.xml
 <!--子容器配置文件-->
    <!--只注册Controller对象-->
    <context:component-scan base-package="com.javasm" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
    </context:component-scan>

    <!--springmvc注解识别-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--静态资源访问-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--文件上传-->

    <!--拦截器-->
  • 在web.xml中分别配置监听器与springmvc前端控制器,加载各自的xml创建父子容器
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
<servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

ContextLoaderListener implements ServletContextListener监听器类,监听ServletContext对象的创建与销毁。

这个监听器内,创建了XmlWebApplicationContext对象,该spring容器对象与ServletContext对象互相持有。

public void contextInitialized(ServletContextEvent event) {
	ServletContext sc = event.getServletCotnext();
    this.context = new XMLWebApplicationContext();
    this.context.setServletContext(sc);
    String contxtConfigLocation = sc.getInitParameter("contextConfigLocation");
    this.context.setConfigLocation(configLocationParam);//指定xml配置文件
    this.context.refresh();//加载xml初始化容器对象
    sc.setAttribute("WebApplicationContext.ROOT",this.context);
}

DispatcherServlet:

这个servlet中,创建了XmlWebApplicationContext子容器对象,该子容器对象持有者监听器中创建的父容器对象。

public void init(){
	ServletContext sc = this.getServletContext();
    WebApplicationContext parent = (WebApplicationContext)sc.getAttribute("WebApplicationContext.ROOT");
	this.wac = new XmlWebApplicationContext();
	this.wac.setParent(parent);
	this.wac.setConfigLocation("classpath:springmvc.xml");
	this.wac.setServletContext(this.getServletContext());
	sc.setAttribute("dispatcherServlet",this.wac);
}

springmvc与spring整合思路:

前提:控制层的对象,必须放在dispathcerserlvet类创建的容器中。因为springmvc查找处理器对象,处理器方法,只去自己的容器中查找。

前提:子容器持有父容器,子容器中的对象可以依赖到的父容器中对象。

思路1:抛弃父容器,只要子容器。所有对象全部放子容器。springboot就这样设计的。

思路2:控制层对象放子容器,其他对象放父容器。ssm框架中这样使用。

2.corsFilter注册父容器

由于DelegatingFilterProxy在tomcat启动时,去servletContext.getAttribute(‘WebApplicationContext.root’)获取到的父容器对象,从父容器中getBean(‘corsFilter’).因此CorsFilter跨域过滤器对象要注册到父容器中。

<!--真正的跨域过滤器-->
    <!--CorsFilter应该注册到哪个容器中,该对象应该注册到父容器中。-->
    <bean id="corsFilter" class="org.springframework.web.filter.CorsFilter">
        <constructor-arg name="configSource">
            <bean class="org.springframework.web.cors.UrlBasedCorsConfigurationSource">
                <property name="corsConfigurations">
                    <map>
                        <entry key="/**">
                            <bean class="org.springframework.web.cors.CorsConfiguration">
                                <property name="allowCredentials" value="true"/>
                                <property name="allowedMethods">
                                    <list>
                                        <value>GET</value>
                                        <value>POST</value>
                                        <value>PUT</value>
                                        <value>DELETE</value>
                                        <value>OPTIONS</value>
                                    </list>
                                </property>
                                <property name="allowedHeaders" value="*"/>
                                <property name="allowedOrigins" value="http://localhost:8088"/>
                            </bean>
                        </entry>
                    </map>
                </property>
            </bean>
        </constructor-arg>
    </bean>
<filter>
    <filter-name>delegatingFilterProxy</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetBeanName</param-name>
        <param-value>corsFilter</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>delegatingFilterProxy</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3.mybatis与spring整合

aware [əˈweə®] 意识到的 awaken 唤醒

transaction aware 事务感知 这个翻译真尼玛好

老师说第三层包名就是jar包的名称

在这里插入图片描述

我瞎了真的是,居然能这么简介

在这里插入图片描述

要把mybatis的全局单例的SqlSessionFactory对象注册spring父容器中;

要把mybatis的dao接口代理对象注册spring父容器中;

  • mysql驱动;druid连接池包;mybatis核心包;mybatis-spring整合包;
  • 添加spring-jdbc包。
  • 创建dao.xml,注册连接池,注册SqlSessionFactoryBean
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${jdbc.url}"></property>
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="initialSize" value="${jdbc.initSize}"></property>
    <property name="maxActive" value="${jdbc.maxActive}"></property>
    <property name="maxWait" value="${jdbc.maxWait}"></property>
    <property name="minIdle" value="${jdbc.minIdle}"></property>
    <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"></property>
    <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"></property>
    <property name="validationQuery" value="${jdbc.validationQuery}"></property>
    <property name="testWhileIdle" value="true"></property>
    <property name="testOnBorrow" value="false"></property>
    <property name="testOnReturn" value="false"></property>
</bean>

<bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:mybatis.xml"></property>
    <property name="typeAliasesPackage" value="com.javasm"></property><!--别名注册Configuration-->
    <property name="dataSource" ref="dataSource"></property><!--第三方连接池-->
    <property name="mapperLocations" value="classpath:mappers/*/*.xml"></property><!--映射文件路径-->
</bean>

 <!--对指定包下的所有接口全部创建代理对象注册容器,创建的代理对象中依赖SqlSESSionFactory-->
    <!--切记basePackage必须指定到映射接口包名-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.javasm.*.mapper"></property>
    </bean>

4.事务控制

obtain 获得

truncation [ˈtrʌŋkeɪʃn] 截断

spring 的事务管理器只会回滚 运行时异常

改成只要是异常都回滚(要看情况来确定是否)21:50

在这里插入图片描述

  • 添加spring-jdbc;spring-tx
  • 注册事务管理器对象进入容器DataSourceTransactionManager,依赖DataSource
 <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
</bean>

事务切面的使用方式有两种:

基于注解:建议使用

开启事务注解识别:
<!--事务注解识别 @Transactional-->
<tx:annotation-driven></tx:annotation-driven>
 //是否任何异常都会触发回滚,只有运行时异常才触发回滚
    @Transactional
    @Override
    public Boolean updateUsers(Sysuser u1, Sysuser u2) {
        userMapper.update(u1);//正常

        InputStream in=null;
        try {
            in= new FileInputStream("D:/aaadfasd.docx");//受检异常。编译时异常
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        if(in==null){
            throw new MvcException();//抛出运行时异常,使事务回滚;不抛,则事务提交
        }else{
            return true;
        }
//        userMapper.update(u2);//抛出MysqlDataTruncation,触发回滚
    }

基于配置:不建议使用

<!--事务通知,可以对切入点表达式进行补充-->
<!--不建议使用,因为无法精准的控制到某个服务层的方法是否启用事务。-->
<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="query*" propagation="SUPPORTS"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="servicePointcut" expression="execution(* com.javasm.*.service.impl.*.*(..))"></aop:pointcut>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"></aop:advisor>
</aop:config>

5.分页插件

以后看到这种需要用一个功能但是有两个jar包的不要怕,他们是依赖的关系

dialect 就是数据库不用的写法

通过url解析数据库就是这个

在这里插入图片描述

在这里插入图片描述

使用github上的pageHelper分页插件,此插件是基于mybatis开发。

  • 添加pageHelper分页组件
  • 配置PageInterceptor分页拦截器对象进入SqlSEssionFactory。
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis.xml"></property>
        <property name="typeAliasesPackage" value="com.javasm"></property><!--别名注册Configuration-->
        <property name="dataSource" ref="dataSource"></property><!--第三方连接池-->
        <property name="mapperLocations" value="classpath:mappers/*/*.xml"></property><!--映射文件路径-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--分页参数合理化。-->
                        <value>reasonable=true</value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
  • 在需要做分页的查询之前启用分页,在查询完成后封装分页参数
@GetMapping("list")
    public PageInfo userlist(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "2") Integer pageSize){
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<Sysuser> l = userService.queryUsers();//返回的其实Page对象
        //封装分页结果
        PageInfo i = new PageInfo(l);//total,list,pageNum,pageSize
        return i;
    }

spring中的ApplicationContext容器接口类图:

BeanFactory

​ ListableBeanFactory

​ DefaultListableBeanFactory:该类是最早期的容器对象,这个容器中没有aop实现。

​ ApplicationContext:

​ ClasspathXMLApplicationContext:含有ioc,di,aop都有支持的容器对象,但不支持web环境

​ AnnotationConfigApplicationContext:含有ioc,di,aop都有支持的容器对象,但不支持web环境

​ WebApplicatioContext:接口

​ XmlWebApplicationContext:含有ioc,di,aop都有支持的容器对象,web环境下使用

​ AnnotationConfigWebApplicationContext:含有ioc,di,aop都有支持的容器对象,web环境下使用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值