SSM框架,分为Spring、Spring-MVC、Mybatis,是一个比较老的Web开发框架,不过现在很多地方任然在用,因为它虽然没有当下流行框架使用方便,但是请记住做开发必须要知道的一个道理,所有封装的框架使用越方便,所能实现的业务就越浅显,SSM同样遵循这个道理,因此它能实现的业务更深
下面介绍一下怎么用它的配置文件
首先是pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wy</groupId>
<artifactId>wangyang-cms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 定义主要版本号 -->
<properties>
<spring.version>5.1.5.RELEASE</spring.version>
<mybatis.version>3.4.2</mybatis.version>
<log4j.version>1.2.17</log4j.version>
<log4japi.version>1.7.26</log4japi.version>
<druid.version>1.0.9</druid.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.spring.version>1.3.0</mybatis.spring.version>
<jackson.version>2.9.1</jackson.version>
<validator.version>6.0.10.Final</validator.version>
<poi.version>3.9</poi.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<commons-io.version>2.2</commons-io.version>
<commons-net.version>3.3</commons-net.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<junit-version>4.12</junit-version>
<!-- aop使用 -->
<aspectj-version>1.8.0</aspectj-version>
<!-- 分页助手 -->
<pagehelper-version>5.1.2</pagehelper-version>
</properties>
<!-- jar依赖 -->
<dependencies>
<!-- spring 及springMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<!-- 引用版本 -->
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis-spring 整合jar -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- Mysql数据库驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${log4japi.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${log4japi.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- 依赖的公共包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${validator.version}</version>
</dependency>
<!-- mybaits pagehelper 分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 富文本编辑器使用该文件上传 -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
<!-- 构建项目 -->
<build>
<plugins>
<!-- jetty配置 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.7.v20160115</version>
<configuration>
<httpConnector>
<!-- 项目端口号 -->
<port>80</port>
<!--项目路径 -->
<host>localhost</host>
</httpConnector>
<!-- 配置虚拟访问路径,这个可以不要,这是我之前使用SSM做头像上传用的 -->
<contextHandlers>
<jettyWebAppContext>
<!-- 虚拟路径 访问路径 -->
<contextPath>/pic/</contextPath>
<!-- 物理路径 -->
<resourceBase>D:/pic/</resourceBase>
</jettyWebAppContext>
</contextHandlers>
<!-- 扫描的配置. 如果项目源码改变,1秒后重启jetty -->
<scanIntervalSeconds>1</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
</build>
</project>
之后肯等是web.xml,我们要在其中加载Spring-MVC的配置文件,使得Web服务器加载Spring的框架内部的前段控制器–>DispatcherServlet,它的本质就是个原生的被Servlet注解的一个Controller,同时我们还要加载Spring的配置文件,用来启动Spring容器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>ceshi3.5</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>
<!-- 配置*控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联springMVC核心配置文件,这个文件路径必须正确,我是放在里source下 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
</servlet>
<!-- 配置*控制器生效的前提或者说是拦截的请求格式,支持通配符 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 使用容器装配监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 为监听器指定spring核心配置文件,可以不用这一步,但是前提是在source下有spring框架默认加载的文件,我不喜欢用那个格式,有兴趣的可以网上查查,我记不清了 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
</web-app>
之后准备spring-MVC的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<!-- 扫描controller -->
<context:component-scan base-package="com.wy.controller"></context:component-scan>
<!-- 启动前端注解驱动 -->
<mvc:annotation-driven />
<!-- 禁止拦截静态资源 -->
<mvc:default-servlet-handler/>
<!-- 视图解析图 ,这个可以先注释,它的作用就是自动拼接请求的公共部分,是的开发中可以只写不同的地方-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/view/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 上传支持,如果你的开发中涉及到上传那么可以使用这个,这是spring框架提供的上传接收类,不过用的时候注意id和class是框架内部定死的 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 文件名的编码格式 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 上传文件的大小 10兆-->
<property name="maxUploadSize" value="10485760"/>
<!-- 单个文件上传的大小 -->
<property name="maxUploadSizePerFile" value="1048576"/>
</bean>
</beans>
之后是Spring的配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="com.wy"></context:component-scan>
<!-- mysql数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///cms"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置sqlSession工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 关联mybatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"/>
<!-- 扫描mapper文件 但是注意我把他注释了,因为如果使用这个那么你的Dao层的Mapper文件就要在source下
但是我们一般都是使用扫描器,使得Mapper文件也在Dao层里
<property name="mapperLocations" value="classpath:mappers/*"/>
-->
</bean>
<!-- 托管sqlSession工厂 -->
<bean class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean>
<!-- dao层接口与附带mapper文件扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wy.dao"/>
</bean>
<!-- 事务管理器 ,这个事务管理器可以先注释有需要的时候在用-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务 transaction-manager:表示关联的事务管理器是谁 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 事务属性配置 -->
<tx:attributes>
<!-- 以find.select get开头的方法为只读事务,用来提高数据库的性能 -->
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="list*" read-only="true" />
<!-- 其他的方法为默认事务 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置aop 这个也可以先注释,这个是面向切面编程用的,不过我用它做关联事物的操作
当然想要关联事物还有一种方法,不过不常用,有兴趣的可以看看别的资料
-->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut
expression="execution(* com.wy.service.*.*(..))" id="pointcut" />
<!-- 关联事务 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
</beans>
最后是准备Mybatis的配置文件
<?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="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 启动分页拦截器,如果你的web中用到里spring提供的PageInfo那么就要配置拦截器 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
mybatis的配置文件相当简单,这是因为原本在其中配置的数据源和sql文件在spring中配置了
经过如上的配置之后我们就可以开始实现web的业务开发了,但是最好检查一下上面的配置有很多地方是要修改成你自己的配置的,比如路径之类的