SSM整合
Posted ynhwl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合相关的知识,希望对你有一定的参考价值。
一、新建Maven项目
在pom.xml中添加如下依赖代码,
1 <!-- 引入Servlet API,用于开发WEB项目,引入该资源后就不需要再引入Tomcat运行库了 --> 2 <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> 3 <dependency> 4 <groupId>javax.servlet</groupId> 5 <artifactId>javax.servlet-api</artifactId> 6 <version>3.1.0</version> 7 <scope>provided</scope> 8 </dependency>
更新Eclipse对项目的设置,查看Marks窗口的错误提示,快速修正,升级为1.8
遇到一个拦路虎,就是更新项目后jdk版本又跳回去1.5了,解决办法:
学习前辈的方法:https://www.cnblogs.com/gdjlc/articles/6978454.html
更新自带的web.xml至当前所用的Tomcat的版本,该模板可以到Tomcat安装路径下的conf文件夹找到web.xml,抄入XML文件头、根节点定义。最终如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 5 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 6 7 <display-name>Archetype Created Web Application</display-name> 8 9 10 </web-app>
至此,一个标准Maven项目准备完成。
接下来配置SpringMVC
1.引入资源spring-contextspring-webspring-webmvc依赖
1 <!-- spring + MVC 整合资源 --> 2 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 3 <dependency> 4 <groupId>org.springframework</groupId> 5 <artifactId>spring-context</artifactId> 6 <version>4.3.19.RELEASE</version> 7 </dependency> 8 <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> 9 <dependency> 10 <groupId>org.springframework</groupId> 11 <artifactId>spring-web</artifactId> 12 <version>4.3.19.RELEASE</version> 13 </dependency> 14 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-webmvc</artifactId> 18 <version>4.3.19.RELEASE</version> 19 </dependency> 20 <!-- end spring MVC -->
到此,Spring MVC 资源全了,接下来是配置了,
配置参见官网:Spring.io
- 在web.xml中添加MVc基础设置
1 <!-- Spring MVC 基础设置 详见官网:22.3 --> 2 <!-- https://docs.spring.io/spring/docs/4.3.20.RELEASE/spring-framework-reference/htmlsingle/#mvc --> 3 <!-- 设置 spring 配置文件路径 --> 4 <context-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value>classpath:spring.xml</param-value> 7 </context-param> 8 <!-- MVC 核心控制器 --> 9 <servlet> 10 <servlet-name>dispatcher</servlet-name> 11 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 12 <init-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value></param-value> 15 </init-param> 16 <load-on-startup>1</load-on-startup> 17 </servlet> 18 <servlet-mapping> 19 <servlet-name>dispatcher</servlet-name> 20 <url-pattern>*.do</url-pattern> 21 </servlet-mapping> 22 <!-- 初始化IOC容器的 --> 23 <listener> 24 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 25 </listener> 26 <!-- end MVC -->
添加SPring配置文件并添加MVC开启
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/mvc 9 http://www.springframework.org/schema/mvc/spring-mvc.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd"> 12 13 <!-- 开启MVC功能 --> 14 <mvc:annotation-driven /> 15 <!-- end MVC --> 16 17 </beans>
- 返回web.xml【MVc基础设置】中添加配置路径:
到此,运行服务器,测试是否可以
遇到问题
解决:https://blog.csdn.net/steveguoshao/article/details/38414145
到此,SpringMVC配置成功
一、整合MabatiS
- 在pom.xml中添加 mybatis核心包的依赖:mybatis
1 <!-- spring + mybatis --> 2 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 3 <dependency> 4 <groupId>org.mybatis</groupId> 5 <artifactId>mybatis</artifactId> 6 <version>3.4.6</version> 7 </dependency> 8 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> 9 <dependency> 10 <groupId>org.mybatis</groupId> 11 <artifactId>mybatis-spring</artifactId> 12 <version>1.3.1</version> 13 </dependency> 14 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-jdbc</artifactId> 18 <version>4.3.19.RELEASE</version> 19 </dependency> 20 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 21 <dependency> 22 <groupId>mysql</groupId> 23 <artifactId>mysql-connector-java</artifactId> 24 <version>5.1.38</version> 25 </dependency> 26 <!-- end sm -->
- 创建Mabatis的配置文件:
回到spring.xml添加myBATIS整合:
- 配置数据源、Mybatis SQL会话工厂(注意路径)、映射接口扫描器(注意路径)
1 <!-- 整合Mybatis --> 2 <!-- 配置数据源 --> 3 <bean id="dataSource" 4 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 5 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 6 <property name="url" value="jdbc:mysql://127.0.0.1:3306/boke?useUnicode=true&characterEncoding=utf8" /> 7 <property name="username" value="root" /> 8 <property name="password" value="199751hwl" /> 9 </bean> 10 11 <!-- 配置Mybatis SQL会话工厂 --> 12 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 13 <!-- 配置个数据源对象 --> 14 <property name="dataSource" ref="dataSource" /> 15 <!-- 配置文件自定义路径 --> 16 <property name="configLocation" value="classpath:mybatis.xml"></property> 17 <!-- 映射文件路径 --> 18 <property name="mapperLocations" 19 value="classpath*:kohgylw/kkpager_quickstart_18072/mapper/xml/*.xml" /> 20 </bean> 21 <!-- 映射接口扫描器 --> 22 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 23 <property name="basePackage" value="kohgylw.kkpager_quickstart_18072.mapper" /> 24 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 25 </bean> 26 <!-- end mybatis -->
添加包层次
到此,整合完成,测试:
可以
以上是关于SSM整合的主要内容,如果未能解决你的问题,请参考以下文章
520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“