1.4.ssm整合

ssm整合

1.springmvc与spring的整合

在DispatcherServlet对象实例化了一个WebApplicationContext对象。

1.1在DispatcherServlet源码:

该servlet的init方法的作用:去serlvetContext获取root容器;创建了新的容器对象,并持有root容器对象

//去serlvetContext获取root容器
WebApplicationContext root = ServletContext.getAttribute("WebApplicationContext.ROOT");

//创建springmvc子容器对象,持有spring父容器对象
WebApplicationContext wac = new XMLWebApplicationContext();
wac.setParent(root);
wac.setConfigLocation("classpath:springmvc.xml")
wac.setServletContext(ServletContext sc);
wac.refresh();

在ContextLoaderListener上下文对象监听器源码:

该监听器作用:创建了XmlWebApplicationContext对象,并且把该对象放在ServletContext对象中。

WebApplicationContext root = new XmlWebApplicationContext()
root.setServletContext(ServletContext sc);
root.setConfigLocation("classpath:spring.xml");
root.refresh();

servletContext.setAttribute("WebApplicationContext.ROOT",root);

现在代码中分了两个容器,容器的功能划分清晰化:

springmvc是控制层框架,springmvc子容器中注册的bean是控制层的bean对象;

spring是一个大的平台性框架,注册所有非控制层bean(service,repository,component);

子容器能调用到父容器的bean;父不能调子

1.2具体的配置方式:

1.spring.xml
 <!--spring容器配置,识别非Controller注解-->
 <context:component-scan base-package="com.javasm">
     <context:exclude-filter type="annotation"
               expression="org.springframework.stereotype.Controller">
     </context:exclude-filter>
  </context:component-scan>

2.springmvc.xml
<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>
3.web.xml
<!--初始化spring父容器-->
    <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>
<!--初始化springmvc子容器-->
<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>

 <!--配置跨域过滤器-->
    <filter>
        <filter-name>myCorsFilter</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>myCorsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

注意点:

在ssm环境下,分了父子容器后,corsFilter应该放在先加载的容器中

在web.xml中引用了容器中的corsFilter对应的bean对象,而在加载web.xml时,要实例化过滤器,此时需要把corsFilter的注册移动到spring.xml,把该对象放在spring父容器中。

2.mybatis与spring整合

  • 1.添加mysql驱动,druid连接池的依赖
  • 2.添加mybatis核心包,mybaits-spring的整合包
  • 3.添加spring-jdbc包;spring-tx事务包
  • 4.添加数据库连接信息properties配置,同时注册DruidDataSource对象到spring父容器。
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"></context:property-placeholder>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
      <property name="driverClassName" value="${jdbc.driver}"></property>
      <property name="url" value="${jdbc.url}"></property>
      <property name="username" value="${jdbc.username}"></property>
      <property name="password" value="${jdbc.password}"></property>
      <property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
  • 5.SqlSessionFactory对象注册到spring容器。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
  	<property name="mapperLocations" value="classpath:com/javasm/*/mapper/*.xml" ></property>
    <property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
  • 6.把mapper接口的代理对象注册到spring容器。
 <!--扫描指定包下的所有接口,并对接口创建代理对象注册到容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.javasm.*.mapper"></property>
    </bean>

3.使用spring的事务管理器

  • 1.配置事务管理器
 <!--配置事务管理器对象,id必须是transactionManager-->
 <bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
 </bean>
  • 2.事务使用方式:

    基于注解的事务管理:建议这种方式

<!--方法1:把事务通知织入到带有Transactional注解的方法--> 
<tx:annotation-driven></tx:annotation-driven>

​ 基于xml进行aop配置:

<tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="edit*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="select*" propagation="SUPPORTS"></tx:method>
        </tx:attributes>
    </tx:advice>

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

4.使用mybatis的分页插件

基于mybatis的拦截器,在执行select语句之前,判断当前线程变量下是否有分页参数,动态的修改我们select语句,加上limit 1 10。

  • 1.添加pageHelper分页插件jar包;
  • 2.配置PageInterceptor拦截器到SqlSessionFactory对象;
  • 3.使用分页插件,只要在mapper接口查询方法之前;
@GetMapping("list")
public RespBean selectUsers(Sysuser u,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "2")Integer pageSize){
		//开启分页
        PageHelper.startPage(pageNum,pageSize);
        //返回Page分页集合对象
        List<Sysuser> users =us.selectUsers(u);
        //解析Page对象中的分页数据,封装到PageInfo对象
        PageInfo info = new PageInfo(users);
        //返回数据
        return new RespBean(StatusEnum.SUC,info);
}

注意:PageInfo对象中我们需要记得的几个属性:pageNum,pageSize,total,pages,list

5.mybatis逆向工程生成mapper文件与mapper接口

mybatis-generator组件:

  • 1.创建独立java工程执行代码生成
  • 2.拷贝genertor.xml到项目
  • 3.添加mybaits-generator逆向工程的核心包
  • 4.拷贝代码。

总结:

1.在springmvc和spring配置文件中,配置包扫描,切记谁需要配置use-default-filters="false"
2.切记include-filter|exclude-filter区别
3.跨域过滤器切记移动到spring容器中。
4.切记DruidDataSource注入驱动类的时候,属性名:driverClassName,不是driver
5.在配置SqlSessionFactoryBean的时候,注入mapperLocations属性的时候,写的是路径,不是包名。以classpath:com/javasm/*/mapper/*.xml
6.配置MapperScannerConfigurer扫描mapper接口时,注入basePackage属性,写的包名,不是路径。而且包名必须精确到mapper包。
7.配置DataSourceTransactionManager,id必须是transactionManager,必须注入DataSource对象

了解:

InitializingBean接口与bean标签执行init-method方法,或者是@PostConstruct注解的作用是一样。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值