Spring & Spring MVC 14. SSM 框架整合(Spring 5、Spring MVC 5、MyBatis 3.5.6)非Maven

一、Spring、Spring MVC、MyBatis 三大框架

Spring:Ioc、AOP

Spring MVC:模型-视图-控制框架

MyBatis:ORM 框架

这三个玩意可以说是撑起了很大的一片天,在 SSH(Struts2 + Spring + Hibernate)框架之后又一个主流的框架。

以后我们会经常使用这套框架,所以需要学习一下这三个框架如何整合到一起去使用。

二、整合

第一步

创建一个 Dynamic Web Project 2.5(带有 web.xml)的项目

第二步

引入我们所需要的 jar 包(这里 jar 包比较多,大家自己去 Maven 仓库下载~~),这里的这些个 jar 包都是我们之前学习时候所需要的,去掉一些我们不需要的,也就这么多。

这里唯一多的两个就是 log4j、c3p0,log4j 是管理日志的一个东西,c3p0 是一个数据库连接池。

第三步

配置 web.xml

这里我们需要将 spring mvc 配置上去。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssm_test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- SpringMVC的前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 设置自己定义的控制器xml文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext.xml</param-value>
		</init-param>
	</servlet>
	<!-- Spring MVC配置文件结束 -->
 
	<!-- 拦截设置 -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- 由SpringMVC拦截所有请求 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

同样,这个就是我们之前学习时候使用的 Spring mvc 配置。

第四步

配置好 web.xml 之后,我们还需要配置 applicationContext.xml,因为我们 Spring mvc 需要这个。

<?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://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
        https://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
        
	<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/"></property>
    	<property name="suffix" value=".jsp" />
    </bean>
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<property name="defaultEncoding" value="utf-8" /> 
    	<property name="maxUploadSize" value="10485760" />
    </bean>
</beans>

第五步

继续配置 applicationContext.xml,这里面还需要加入数据源,也就是我们当时学习 spring jdbc 的东西。

<context:component-scan base-package="com.lemon1234"/>
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

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

     <tx:annotation-driven transaction-manager="transactionManager"/>
     
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	      <property name="dataSource" ref="dataSource"></property>
	      <property name="configLocation" value="classpath:mybatis.xml"></property>
     </bean>
     

     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	     <property name="basePackage" value="com.lemon1234.mapper"></property>
	     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
     </bean>

第六步

配置 jdbc.properties,其实也不用配置,把之前学过的 spring jdbc 的复制过来就好。

第七步

配置 mybatis.xml,这个其实也是复制之前的。这里我们只需要配置这个别名,因为数据源我们放给了 spring 去管理。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
  	<setting name="cacheEnabled" value="true"/>
  </settings>
  <typeAliases>
  	<package name="com.lemon1234.entity"/>
  </typeAliases>
</configuration>

第八步

配置 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: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
        https://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 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven />
    
    <mvc:resources mapping="/img/**" location="file:///D:/file/img/" />
</beans>

第九步

自己写一个 HelloController、HelloService、HelloMapper,还有 HelloMapper.xml ~~

这里我们使用一下注解,@Autowired,这个注解可以直接把我们需要的类注入进来,我们后面说 SpringBoot 框架的时候回详细的去说。

然后我们就可以启动服务器,进行测试一波。

不对,再弄一个 index.jsp 页面~~~

测试

可以看到啊,是 OK 的。

这里 Console 窗口中 Log4j:WARN 不需要去管,因为我们没有配置日志。后期我们也会说一下这个 Log4j 的日志组件。

 

到这里我们的 SSM 框架整合就完成了,可以自己定义一个项目主体,然后就进行开发了~~~

有问题可以联系我:QQ 2100363119,欢迎大家访问我的个人自愿网站:https://www.lemon1234.com

最近网站在改造中,欢迎各位提出意见。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

他 他 = new 他()

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值