超详细SpringMVC学习笔记(黑马视频)day03

目录

一、整合方式

二、实例

1、Spring整合SpringMVC

2、Spring整合mybatis


SpringMVC——day03——SSM整合

一、整合方式

      一定是用Spring去整合另两个框架

      注解+XML

二、实例

1、Spring整合SpringMVC

   1)spring配置:创建applicationContext.xml文件进行配置。

        我们不在applicationContext.xml配置对controller的扫描,我们在springmvc.xml中配置对controller的扫描,那么思考一个问题:

        我们如何把service注入到controller中 ?

        解决方式,我们只需要在启动服务器时就加载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" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 开启注解扫描,只处理 service 与 dao -->
    <context:component-scan base-package="com.mvc" >
        <!-- 配置那些不需要扫描 ,不扫描controller,controller是交给SpringMVC配置的-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

   2)SpringMVC配置,创建springmcv.xml进行配置

<?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:p="http://www.springframework.org/schema/p"
       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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <!-- 配置controller扫描包,扫描注解 -->
  <context:component-scan base-package="com.mvc">
    <!-- 只扫描controller -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>

  <!-- 配置视图解析器 ,前端控制器通过视图解析器找到后台返回的数据该发给哪个页面  -->
  <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <!-- 前端控制器,哪些静态资源不拦截 -->
  <mvc:resources mapping="/js/**" location="/js/**" />

  <!-- 配置异常处理器 -->
  <bean id="SysExceptionResolver" class="com.mvc.exception.SysExceptionResolver" />

  <!-- 开启SpringMVC注解的支持 -->
  <mvc:annotation-driven />
</beans>

 

    3)项目

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
  <!-- 核心控制器(前端控制器)的配置 -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      <!-- 加载SpringMVC的核心配置文件 -->
      <init-param>
          <!-- 指定SpringMVC核心配置文件的路径。如果不指定,默认为/WEB-INF/${servlet-name}-servlet.xml -->
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>

     <!--拦截所有请求,所有请求都要经过dispatcherServlet-->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
    
    <!-- 配置解决中文乱码的过滤器-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

实体类:

 

  Service:

public interface AccountService {
    //查询所有账户
    public String findAll();

    //添加账户
    public void saveAccount(Account account);
}

ServiceImpl:

@Service
public class AccountServiceImpl implements AccountService {


    public String findAll(){
        System.out.println("业务层代码执行了");
        return "sjioqw";
    }

    public void saveAccount(Account account) {

    }
}

Controller:

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    priva AccountService accountService;

    @RequestMapping("/findAll")
    public String findAll(){
        System.out.println("表现层代码执行了");
        accountService.findAll();
        return "success";
    }
}

       到这里,我们发现,下面这段代码报错了。因为web.xml只加载了springMVC配置文件,没有人去加载spring.xml文件,也就是没有把service对象放进IOC,当然不能注入啦

      @Autowired
      AccountService accountService;

     为解决这个问题,我们需要让Tomcat服务器启动时就加载spring.xml。我们知道JavaWeb有一个ServletContext对象,生命周期与服务器一样,他有一类监听器,监听ServletContext对象的创建与销毁,且至在服务器启动时执行一次。所以我们可以让监听器去加载Spring配置文件,创建web1版本的工厂,存在ServletContext对象中。

Spring的spring-web已经给我们提供好了这个监听器类,我们只需要在web.xml中加入如下代码,去配置spring监听器

<web-app>

<!-- 配置spring监听器,默认只加载WEN-INT下的applicationContext.xml文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 配置路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

</web-app>

那么我们的Spring与SpringMVC就整合好了运行代码

控制台会打印:

        表现层代码执行了
        业务层代码执行了

2、Spring整合mybatis

目的:用spring创建代理,存到 IOC容器中

1)配置:

jdbc.propties:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/spring4_demo03_tx?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8
jdbc.username = root
jdbc.password =

applicationContext.xml:

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 开启注解扫描,只处理 service 与 dao -->
    <context:component-scan base-package="com.mvc" >
        <!-- 配置那些不需要扫描 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- Spring整合mybatis -->
    <!-- 1、配置连接池 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!-- 2、配置SqlSessionFactory工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 3、配置accountDao接口所在的包 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mvc.dao"/>
    </bean>

</beans>

2)声明式事务管理的配置

<!-- 配置声明式事务管理 -->
    <!-- 1、配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 2、配置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 设置隔离级别、传播行为等一些行为 -->
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!-- 3、配置Aop增强 -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.mvc.service.impl.*ServiceImpl.*(..))" />
    </aop:config>

完结,撒花花

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值