• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
在赶路的我
博客园    首页    新随笔    联系   管理    订阅  订阅

SSM整合经验

SSM项目整合经验

spring整合springweb mvc

  • 因为springwebmvc是spring的一个模块,所以整合起来无缝衔接,只需要在spring配置文件中

  • 需要导入的基本依赖

    • <!--        导入spring-webmvc-->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
                  <version>5.3.23</version>
              </dependency>
      <!--        导入spring的核心依赖包-->
              <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context</artifactId>
                  <version>5.3.20</version>
              </dependency>
      
  • 在spring配置文件中的配置

    • <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
             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.xsd
      	   http://www.springframework.org/schema/mvc
      	   http://www.springframework.org/schema/mvc/spring-mvc.xsd
      	    http://www.springframework.org/schema/p
      	   http://www.springframework.org/schema/p/spring-p.xsd
      	   http://www.springframework.org/schema/aop
         	   http://www.springframework.org/schema/aop/spring-aop.xsd
      
      	    http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
      
          <context:component-scan base-package="com.chen.controller"/>
          <context:component-scan base-package="com.chen.MyCharset"/>
      
          <mvc:annotation-driven>
              //这个是解决乱码的
      <!--        <mvc:message-converters>-->
      <!--            <bean class="org.springframework.http.converter.StringHttpMessageConverter">-->
      <!--                <property name="defaultCharset" value="UTF-8"/>-->
      
      <!--            </bean>-->
      <!--        </mvc:message-converters>-->
          </mvc:annotation-driven>
          <mvc:default-servlet-handler/>
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
              <property name="prefix" value="/"/>
              <property name="suffix" value=".jsp"/>
          </bean>
      </beans>
      
      
      
  • 在web.xml中配置dispatcherservlet

    • <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
               version="4.0">
          
          
          <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:ApplicationContext.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>
          
          
          
      </web-app>
      

spring整合mybatis

  • 需要导入的基本依赖

    • <!--        mysql驱动-->
              <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>8.0.31</version>
              </dependency>
      <!--        导入mybatis-->
              <dependency>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis</artifactId>
                  <version>3.5.9</version>
              </dependency>
      <!--        导入mybatis-spring的整合包-->
              <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
              <dependency>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis-spring</artifactId>
                  <version>1.3.0</version>
              </dependency>
      <!--        导入druid数据池-->
              <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
              <dependency>
                  <groupId>com.alibaba</groupId>
                  <artifactId>druid</artifactId>
                  <version>1.2.8</version>
              </dependency>
      
  • 在spring配置文件中的基本配置

    • <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:aop="http://www.springframework.org/schema/aop"
             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
      		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
      		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      		">
          <context:component-scan base-package="com.chen.dao"/>
          <context:property-placeholder location="classpath:jdbc.properties"/>
      
          <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
              <property name="driverClassName" value="${jdbc.driver}"/>
              <property name="url" value="${jdbc.url}"/>
              <property name="username" value="${jdbc.username}"/>
              <property name="password" value="${jdbc.password}"/>
          </bean>
      
          <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
              <property name="dataSource" ref="dataSource"/>
              <property name="mapperLocations" value="classpath:com/chen/dao/BlogMapper.xml"/>
          </bean>
      
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
              <property name="basePackage" value="com.chen.dao"/>
          </bean>
      
      
      </beans>
      
      
      
    • jdbc的配置文件

      • jdbc.driver=com.mysql.cj.jdbc.Driver
        jdbc.url=jdbc:mysql://localhost:3306/clt?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
        jdbc.username=root
        jdbc.password=7536951
        

解决乱码的几种方式

  1. 在每一个处理方法上加上 produces = "text/html;charset=UTF-8"

  2. 直接在StringHttpMessageConverter设置defaultcharset=UTF-8,默认为ISO..

    •     <mvc:annotation-driven>
              <mvc:message-converters>
                  <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                      <property name="defaultCharset" value="UTF-8"/>
      
                  </bean>
              </mvc:message-converters>
          </mvc:annotation-driven>
      
  3. 利用BeanPostProcessor在StringHttpMessageConverter这个bean实例化之后改变defaultCharset,并注入到IOC容器中覆盖默认的StringHttpMessageConverter

    • package com.chen.MyCharset;
      
      import org.springframework.beans.BeansException;
      import org.springframework.beans.factory.config.BeanPostProcessor;
      import org.springframework.http.MediaType;
      import org.springframework.http.converter.StringHttpMessageConverter;
      import org.springframework.stereotype.Component;
      
      import java.nio.charset.Charset;
      import java.nio.charset.StandardCharsets;
      import java.util.ArrayList;
      import java.util.List;
      
      @Component
      public class CharsetUTF8 implements BeanPostProcessor {
      
          @Override
          public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
              return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
          }
      
          @Override
          public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      
              if (bean instanceof StringHttpMessageConverter){
      
                  System.err.println("=========================================");
                  List<MediaType> mediaTypes = new ArrayList<>();
                  mediaTypes.add(new MediaType("application","json", Charset.forName("UTF-8")));
                  ((StringHttpMessageConverter) bean).setSupportedMediaTypes(mediaTypes);
      
              }
      
              return bean;
          }
      }
      
      
posted @ 2023-02-25 16:42  在赶路的我  阅读(14)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3