学习笔记——SSM整合(思路、步骤)(ssm整合pagehelper)

  本篇文章为你整理了学习笔记——SSM整合(思路、步骤)(ssm整合pagehelper)的详细内容,包含有ssm整合shiro ssm整合pagehelper ssm整合swagger ssm整合swagger 学习笔记——SSM整合(思路、步骤),希望能帮助你了解 学习笔记——SSM整合(思路、步骤)。

  (1)容器管理对象,由DispatcherServlet管理

  (2)Spring容器对象,由ContextLoaderListener管理

  2、解决组件扫描的冲突问题

  (1)SpringMVC只扫描Controller层

  (2)Spring扫描排除Controller层

  2、Spring+Mybatis

  (1)关于数据源、事务管理的代码冲突问题

  同意交给Spring管理

  (2)Spring管理Mybatis核心对象

  ①SqlSessionFactory

  ②Mapper代理对象

  二、SSM整合步骤

  1、Spring + SpringMVC

  (1)导入jar包

  

 !-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -- 

 

   dependency

   groupId org.springframework /groupId

   artifactId spring-webmvc /artifactId

   version 5.3.1 /version

   /dependency

   !-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 --

   dependency

   groupId org.thymeleaf /groupId

   artifactId thymeleaf-spring5 /artifactId

   version 3.0.12.RELEASE /version

   /dependency

   !-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --

   dependency

   groupId javax.servlet /groupId

   artifactId javax.servlet-api /artifactId

   version 4.0.1 /version

   scope provided /scope

   /dependency

 

  (2)配置文件

  ①web.xml

  a.注册CharacterEncodingFilter,解决请求乱码问题

  b.注册HiddenHttpMethodFilter,支持PUT DELETE提交【REST风格】

  c.注册DispatcherServlet(前端控制器),管理springMVC容器对象

  d.注册一个上下文参数(contextConfigLocation),设置spring.xml配置文件路径

  e.注册ContextLoaderListener,管理spring容器对象(目的是为了加载spring的配置文件,帮我们初始化IOC容器)

  

 ?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"

   !-- d.注册一个上下文参数(contextConfigLocation),设置spring.xml配置文件路径--

   context-param

   param-name contextConfigLocation /param-name

   param-value classpath:spring.xml /param-value

   /context-param

   !-- e.注册ContextLoaderListener,管理spring容器对象--

   listener

   listener-class org.springframework.web.context.ContextLoaderListener /listener-class

   /listener

   !-- a.注册CharacterEncodingFilter,解决请求乱码问题--

   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

   init-param

   param-name forceRequestEncoding /param-name

   param-value true /param-value

   /init-param

   /filter

   filter-mapping

   filter-name CharacterEncodingFilter /filter-name

   url-pattern /* /url-pattern

   /filter-mapping

   !-- b.注册HiddenHttpMethodFilter,支持PUT DELETE提交【REST风格】--

   filter

   filter-name HiddenHttpMethodFilter /filter-name

   filter-class org.springframework.web.filter.HiddenHttpMethodFilter /filter-class

   /filter

   filter-mapping

   filter-name HiddenHttpMethodFilter /filter-name

   url-pattern /* /url-pattern

   /filter-mapping

   !-- c.注册DispatcherServlet(前端控制器),管理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

   /web-app

 

  ②springMVC.xml

  a.开启组件扫描(只扫描Controller)

  b.装配视图解析器

  c.装配视图控制器(view-controller)

  d.装配"default-servlet-handler",解决静态资源加载问题

  e.装配"annotation-driven",解决后续问题

  解决view-controller问题、解决default-servlet-handler问题、解决Jackon配置消息转换器问题

  

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

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"

   !-- a.开启组件扫描(只扫描Controller)--

   context:component-scan base-package="com.hh" use-default-filters="false"

   context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/

   /context:component-scan

   !-- b.装配视图解析器--

   bean

   property name="characterEncoding" value="UTF-8" /property

   property name="templateEngine"

   bean

   property name="templateResolver"

   bean

   property name="characterEncoding" value="UTF-8" /property

   property name="prefix" value="WEB-INF/pages/" /property

   property name="suffix" value=".html" /property

   /bean

   /property

   /bean

   /property

   /bean

   !-- c.装配视图控制器(view-controller)--

   mvc:view-controller path="/" view-name="index" /mvc:view-controller

   !-- d.装配"default-servlet-handler",解决静态资源加载问题--

   mvc:default-servlet-handler /mvc:default-servlet-handler

   !-- e.装配"annotation-driven",解决后续问题--

   mvc:annotation-driven /mvc:annotation-driven

   /beans

 

  ③spring.xml

  开启组件扫描(排除Controller层)

  

 ?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"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"

   !-- 开启组件扫描--

   context:component-scan base-package="com.hh"

   context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/

   /context:component-scan

   /beans

 

  2、Spring+Mybatis

  (1)导入jar包

  ①spring的jar包

  

 !-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -- 

 

   dependency

   groupId org.springframework /groupId

   artifactId spring-jdbc /artifactId

   version 5.3.10 /version

   /dependency

   !-- https://mvnrepository.com/artifact/org.springframework/spring-orm --

   dependency

   groupId org.springframework /groupId

   artifactId spring-orm /artifactId

   version 5.3.10 /version

   /dependency

   !-- https://mvnrepository.com/artifact/org.springframework/spring-context --

   dependency

   groupId org.springframework /groupId

   artifactId spring-aspects /artifactId

   version 5.3.10 /version

   /dependency

 

  ②mybatis的jar包

  

 !-- https://mvnrepository.com/artifact/com.alibaba/druid -- 

 

   dependency

   groupId com.alibaba /groupId

   artifactId druid /artifactId

   version 1.1.10 /version

   /dependency

   !-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --

   dependency

   groupId mysql /groupId

   artifactId mysql-connector-java /artifactId

   version 8.0.26 /version

   /dependency

   !-- https://mvnrepository.com/artifact/org.mybatis/mybatis --

   dependency

   groupId org.mybatis /groupId

   artifactId mybatis /artifactId

   version 3.5.6 /version

   /dependency

   !-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --

   dependency

   groupId com.github.pagehelper /groupId

   artifactId pagehelper /artifactId

   version 5.0.0 /version

   /dependency

 

  ③spring与mybatis整合jar包

  

 dependency 

 

   groupId org.mybatis /groupId

   artifactId mybatis-spring /artifactId

   version 2.0.6 /version

   /dependency

 

  (2)配置文件

  ①spring.xml

  a.开启组件扫描(排除Controller层)

  b.加载外部属性文件

  c.装配数据源(DruidDataSource)

  d.装配事务管理器(DataSourceTransactionManager)

  e.开启声明式事务管理注解支持

  f.装配SqlSessionFactoryBean,管理SqlSessionFactory

  g.装配MapperScannerConfigurer,管理Mapper代理对象

  

 ?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:tx="http://www.springframework.org/schema/tx"

   xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"

   !-- a.开启组件扫描(排除Controller层)--

   context:component-scan base-package="com.hh"

   context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/

   /context:component-scan

   !-- b.加载外部属性文件--

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

   !-- c.装配数据源(DruidDataSource)--

   bean id="dataSource"

   property name="driverClassName" value="${db.driverClassName}" /property

   property name="url" value="${db.url}" /property

   property name="username" value="${db.username}" /property

   property name="password" value="${db.password}" /property

   /bean

   !-- d.装配事务管理器(DataSourceTransactionManager)--

   bean id="transactionManager"

   property name="dataSource" ref="dataSource" /property

   /bean

   !-- e.开启声明式事务管理注解支持--

   tx:annotation-driven transaction-manager="transactionManager" /tx:annotation-driven

   !-- f.装配SqlSessionFactoryBean,管理SqlSessionFactory--

   bean id="sqlSessionFactoryBean"

   !-- 设置数据源--

   property name="dataSource" ref="dataSource" /property

   !-- 设置mybatis-config.xml核心配置文件路径--

   property name="configLocation" value="classpath:mybatis-config.xml" /property

   !-- 设置类型别名--

   property name="typeAliasesPackage" value="com.hh.pojo" /property !-- 设置映射文件路径-- property name="mapperLocations" value="classpath:/mapper/*.xml" /property /bean !-- g.装配MapperScannerConfigurer,管理Mapper代理对象-- mybatis-spring:scan base-package="com.hh.mapper" /mybatis-spring:scan /beans

 

  ②springmvc.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:mvc="http://www.springframework.org/schema/mvc"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"

   !-- a.开启组件扫描(只扫描Controller)--

   context:component-scan base-package="com.hh" use-default-filters="false"

   context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/

   /context:component-scan

   !-- b.装配视图解析器--

   bean

   property name="characterEncoding" value="UTF-8" /property

   property name="templateEngine"

   bean

   property name="templateResolver"

   bean

   property name="characterEncoding" value="UTF-8" /property

   property name="prefix" value="WEB-INF/pages/" /property

   property name="suffix" value=".html" /property

   /bean

   /property

   /bean

   /property

   /bean

   !-- c.装配视图控制器(view-controller)--

   mvc:view-controller path="/" view-name="index" /mvc:view-controller

   !-- d.装配"default-servlet-handler",解决静态资源加载问题--

   mvc:default-servlet-handler /mvc:default-servlet-handler

   !-- e.装配"annotation-driven",解决后续问题--

   mvc:annotation-driven /mvc:annotation-driven

   /beans

 

  ③mybatis-config.xml

  

 ?xml version="1.0" encoding="UTF-8" ? 

 

   !DOCTYPE configuration

   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

   "https://mybatis.org/dtd/mybatis-3-config.dtd"

   configuration

   settings

   !-- 开启自动驼峰式命名映射--

   setting name="mapUnderscoreToCamelCase" value="true"/

   !-- 开启延迟加载--

   setting name="lazyLoadingEnabled" value="true"/

   !-- 延迟加载的属性--

   setting name="aggressiveLazyLoading" value="false"/

   !-- 开启二级缓存--

   setting name="cacheEnabled" value="true"/

   /settings

   !-- 添加分页插件--

   plugins

   plugin interceptor="com.github.pagehelper.PageInterceptor" /plugin

   /plugins

   /configuration

 

  

  以上就是学习笔记——SSM整合(思路、步骤)(ssm整合pagehelper)的详细内容,想要了解更多 学习笔记——SSM整合(思路、步骤)的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: