当前位置: 首页 > news >正文

ssm框架整合

MyBatis负责ORM(pojo)

Spring负责Bean的管理(dao, service)

SpringMVC负责响应视图的操作(controller)

SpringMVC是Spring的一部分

Spring MVC和MyBatis没有关系

三者中Spring相当于是粘合剂将MyBatis和SpringMVC整合起来

1. Spring + MyBatis:

在单独使用MyBatis时需要在test中利用SqlSessionFactoryBuilder构建SqlSessionFactory,再利用SqlSessionFactory创建SqlSession.最后使用SqlSession执行SQL语句,在MyBatis的配置文件中要增加数据源,事务等

<!--  通过这个配置文件完成mybatis与数据库的连接  -->
<configuration>
<!--  引入 database.properties 文件  -->
<properties resource="database.properties"/>
<!--  配置mybatis的log实现为LOG4J  -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="cn.dsscm.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!-- 配置事务管理,采用JDBC的事务管理  -->
<transactionManager type="JDBC"/>
<!--  POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源  -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--  将mapper文件加入到配置文件中  -->
<mappers>
<mapper class="cn.dsscm.dao.UserMapper"/>
</mappers>
</configuration>

在Spring和MyBatis融合之后, 就不需要在test中new SqlSessionFactory了,直接在配置文件中声明一个实例,包括数据源,和dao,service等Bean的扫描, 都在applicationContext.xml中,示例:

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--  <property name="url">
            <value><![CDATA[jdbc:mysql://127.0.0.1:3306/dsscm?
                    useUnicode=true&characterEncoding=utf-8]]></value>
        </property>  -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/dsscm? useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--  配置SqlSessionFactoryBean  -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--  引用数据源组件  -->
<property name="dataSource" ref="dataSource"/>
<!--  引用MyBatis配置文件中的配置  -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--  配置DAO  -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.dsscm.dao"/>
</bean>
<!-- 配置扫描注解定义的业务Bean  -->
<context:component-scan base-package="cn.dsscm.service"/>
</beans>

对应的mybatis-config.xml就非常简单了:

<configuration>
<!-- 类型别名  -->
<typeAliases>
<!-- 表明Mybatis框架会扫描指定包下的类,并为其起别名,例如: User->user -->
<package name="cn.dsscm.pojo"/>
</typeAliases>
</configuration>

2.Spring MVC:

最重要的就是前端控制器.负责拦截请求.

关于静态资源:当在 Spring MVC 配置文件中添加了 <mvc:default-servlet-handler/> 元素时,Spring MVC 会自动配置一个 HandlerMapping 和一个 HandlerAdapter,以便将那些没有匹配到任何控制器方法的请求转发给 Servlet 容器默认的 Servlet。这通常用于处理静态资源(如 HTML 文件、CSS 文件、JavaScript 文件、图片等)以及那些不应该由 Spring MVC 控制器处理的请求

对应的web.xml需要配置一个前端控制器(最基础的),也可以增加例如过滤器等等:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>springMVC</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</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>

对应的springmvc-servlet.xml可以配置视图解析器, 扫描Controller类, 拦截器,静态资源处理等等:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--  定义组件扫描器,指定需要扫描的包  -->
<context:component-scan base-package="cn.dsscm"/>
<!--  装配自定义格式化  -->
<mvc:annotation-driven/>
<!--  静态资源处理  -->
<mvc:default-servlet-handler/>
<!--  定义视图解析器  -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--  设置前缀  -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--  设置后缀  -->
<property name="suffix" value=".jsp"/>
</bean>
<!--  国际化 basenames属性用于指定国际化资源文件名  -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="message"/>
</bean>
<mvc:interceptors>
<!--  国际化操作拦截器 如果采用基于(Session/Cookie)则必需配置  -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>
<!--  AcceptHeaderLocaleResolver 配置,
	因为AcceptHeaderLocaleResolver是默认语言区域解析器,不配置也可以   -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/>
</beans>

3.ssm:

四个配置文件:applicationContext.xml mybatis-config.xml springmvc-config.xml web.xml

在Spring+Mybatis的test中每次都要在test中读取Spring配置文件,在ssm中这项工作交由web.xml来做, 在每次应用启动时自动启动spring容器, 其他的关于配置文件没有什么太大变化

简单示例:

mybatis-config.xml:

<configuration>
<settings>
<!--  changes from the defaults  -->
<setting name="lazyLoadingEnabled" value="false"/>
</settings>
<typeAliases>
<!-- 这里给实体类取别名,方便在mapper配置文件中使用 -->
<package name="cn.dsscm.pojo"/>
</typeAliases>
</configuration>

applicationContext-mybatis.xml:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="cn.dsscm.service"/>
<context:component-scan base-package="cn.dsscm.dao"/>
<!--  读取数据库配置文件  -->
<context:property-placeholder location="classpath:database.properties"/>
<!--  JNDI获取数据源(使用dbcp连接池)  -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="maxWait" value="${maxWait}"/>
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
<property name="removeAbandoned" value="${removeAbandoned}"/>
<!--  sql 心跳  -->
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="numTestsPerEvictionRun" value="${maxActive}"/>
</bean>
<!--  事务管理  -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--  配置mybitas SqlSessionFactoryBean  -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--  AOP 事务处理 开始  -->
<aop:aspectj-autoproxy/>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* *cn.dsscm.service..*(..))" id="transService"/>
<aop:advisor pointcut-ref="transService" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="dsscm*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!--  AOP 事务处理 结束  -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.dsscm.dao"/>
</bean>
</beans>

springmvc-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans" 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: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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.dsscm.controller"/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<!--    Date的日期转换器  -->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:resources location="/statics/" mapping="/statics/**"/>
<!--  配置多视图解析器:允许同样的内容数据呈现不同的view  -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorParameter" value="true"/>
<property name="defaultContentType" value="text/html"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html;charset=UTF-8"/>
<entry key="json" value="application/json;charset=UTF-8"/>
<entry key="xml" value="application/xml;charset=UTF-8"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
</beans>

web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name/>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>Ch15_01.root</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>

相关文章:

  • 芯科科技推出的BG29超小型低功耗蓝牙®无线SoC,是蓝牙应用的理想之选
  • 哈尔滨算力服务器托管推荐-青蛙云
  • 利用DeepSeek搭建跨工作表数据的可视化分析动态面板
  • VSCode 搭建C++编程环境 2025新版图文安装教程(100%搭建成功,VSCode安装+C++环境搭建+运行测试+背景图设置)
  • 智能三防手持终端破解传统仓储效率困局
  • 每天一道算法题【蓝桥杯】【两两交换链表中的节点】
  • 【SpringBoot】实现登录功能
  • ES 使用geo point 查询离目标地址最近的数据
  • Vue系统学习day01
  • idea中lombok插件的安装与使用
  • 接口自动化入门 —— JSON中的万能密码--JSONPath解析!
  • PyTorch 入门学习
  • 鸿蒙开发者社区资源的重要性
  • Smart Time Plus smarttimeplus-MySQLConnection SQL注入漏洞(CVE-2024-53544)
  • 快手__NS_sig3数据分析
  • c++介绍智能指针 十二(2)
  • 【C++】 —— 笔试刷题day_4
  • 【Mac 系统卸载 Go 语言完整指南】
  • 【微知】plantuml在泳道图中如何将多个泳道框起来分组并且设置颜色?(box “浏览器“ #LightGreen endbox)
  • 重生之我在学Vue--第11天 Vue 3 高级特性
  • 这群“工博士”,把论文“写”在车间里
  • 中国华能:1-4月新能源装机突破1亿千瓦,利润总额再创新高
  • 国家发改委:不断完善稳就业稳经济的政策工具箱,确保必要时能够及时出台实施
  • 今晚油价下调,加满一箱油将省9元
  • 520、521婚登预约迎高峰?上海民政:将增派力量,新人可现场办理
  • AI创业者聊大模型应用趋势:可用性和用户需求是关键