SSM项目整合

文章目录

    • 1. 导入依赖
    • 2. 配置web.xml文件
    • 3. 编写springmvc.xml配置文件
    • 4. 编写spring.xml配置文件
    • 5. 编写项目整体业务逻辑
    • 6. 开发过程中常见状态码错误

1. 导入依赖

  • 在整合SSM项目时,需要导入以下依赖
    • Spring 相关依赖 (springmvc相关、事务相关、spring 相关核心依赖、Spring测试)
    • Mybatis 依赖(mybatis依赖、mybatis-spring依赖(用于整合mybatis于spring))
    • 数据库相关依赖(mysql驱动依赖、若使用其他数据源与需要导入相关依赖(eg : druid))
    • json转换相关依赖
    • 其他相关依赖
<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>	

    <spring.version>5.2.0.RELEASEspring.version>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>


    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring.version}version>
    dependency>


    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.6version>
    dependency>

    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.5version>
    dependency>


    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.2version>
    dependency>


    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>1.18.12version>
    dependency>

    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
      <scope>providedscope>
    dependency>

    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.0version>
      <scope>providedscope>
    dependency>

    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>


    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.9.0version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
      <version>2.9.0version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
      <version>2.9.0version>
    dependency>

  dependencies>

2. 配置web.xml文件

(1)配置监听器 – ContextLoaderListener(初始化web容器时,加载spring.xml文件)

  • 在context-params 标签中配置spring.xml
  • 配置监听器:初始化web容器时,用于加载spring容器(applicationContext),会加载(context-param)
    SSM项目整合_第1张图片

(2)配置核心控制器(初始化核心控制器时,加载springmvc.xml配置文件)
(3)配置过滤器(post请求设置为UTF-8编码格式)

<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>

  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring.xmlparam-value>
  context-param>
  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
    <init-param>
      <param-name>forceRequestEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    
    <url-pattern>/*url-pattern>
  filter-mapping>


  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>


  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>

    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

3. 编写springmvc.xml配置文件

  • 需要扫描的包(controller层)
  • 视图解析器
  • 筛选过滤资源
  • 开启mvc注解扫描

    <context:component-scan base-package="com.qfedu.controller">context:component-scan>


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/">property>
        <property name="suffix" value=".jsp">property>
    bean>


    <mvc:resources mapping="/WEB-INF/js/**" location="/WEB-INF/js/">mvc:resources>


    <mvc:annotation-driven>mvc:annotation-driven>

4. 编写spring.xml配置文件

  • 需要扫描的包
  • 引入数据库配置文件(可以通过 ${} 读取properties配置文件)
  • 配置数据库相关(整合mybatis)
    • 数据源
    • 配置事务
    • 整合mybatis
  • 配置扫描mapper接口包,会自动扫描并生成在spring容器中生成响应mapper的bean实例
    SSM项目整合_第2张图片
  • 事务注解扫描(配置事务管理器)

    <context:component-scan base-package="com.qfedu">context:component-scan>

    <context:property-placeholder location="classpath:db.properties">context:property-placeholder>



    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <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>
    bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
    bean>

    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qfedu.mapper">property>
    bean>


    <tx:annotation-driven transaction-manager="transactionManager">tx:annotation-driven>

5. 编写项目整体业务逻辑

  • 经过上述四个步骤的配置,SSM框架的环境整合已经完成
  • 正常编写Mapper层、Service层、Controller层(可以使用注解较为简单)
  • 在Service层添加事务注解,进行相应的事务控制

6. 开发过程中常见状态码错误

  • 400 请求参数错误
  • 405 请求方式服务器不支持
  • 415 请求媒体格式不支持(常与数据类型格式转换有关)
  • 404 URL访问服务器资源不存在
  • 500 未知服务器错误

你可能感兴趣的:(java进阶,mybatis,spring,springmvc)