本章记录一下Spring、SpringMVC、Mybatis整合的配置操作–小白篇
SSM框架主要在于搭建resources文件里的配置文件,配置好了写逻辑代码就很方便
配置jdbc.properties-----这里写连接数据库的配置
##驱动类
mysql.driverClass=com.mysql.cj.jdbc.Driver
##########jdbc:mysql://服务器地址:端口号/数据明?mysql8.0的操作
mysql.url=jdbc:mysql://localhost:3306/ssmvip03?useSSL=true&serverTimezone=UTC
###数据库用户名
mysql.username=root
###数据库密码
mysql.password=root
配置myBatis-config.xml -----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>
<!--加载外部资源-->
<properties resource="classpath:jdbc.properties"/>
<!--定义别名-->
<typeAliases>
<!--使用packge标签,说明pojo包里的实体类全部定义了别名(默认类名首字母小写)-->
<package name="com.gtl.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driverClass}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--定义mapper映射的别名(默认首字母小写)-->
<package name="com.gtl.mapper"/>
</mappers>
</configuration>
配置applicationContext.xml-----Spring的xml配置整合Mybatis
<?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"
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">
<!--加载数据源配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--开启扫描注解-->
<context:component-scan base-package="com.gtl.service"/>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.driverClass}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</bean>
<!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务声明-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--mybatis与Spring整合配置-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--扫描pojo包指定别名-->
<property name="typeAliasesPackage" value="com.gtl.pojo"/>
<!--扫描映射文件-->
<property name="mapperLocations" value="classpath:com/gtl/mapper/*.xml"/>
</bean>
<!--配置扫描器,将myBatis的dao接口实现放入IOC容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gtl.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
</bean>
</beans>
配置SpringMVC.xml-----SpringMVC的配置
<?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">
<context:component-scan base-package="com.gtl.controller"/>
<!--处理静态资源的解决方式1-->
<mvc:default-servlet-handler/>
<!--解决静态方式之后404的处理-->
<mvc:annotation-driven/>
<!--处理静态资源的解决方式2(此种方式相对灵活)-->
<!-- <mvc:resources mapping="/js/**" location="/js/" />-->
<!-- <mvc:resources mapping="/css/**" location="/css/" />-->
<!-- <mvc:resources mapping="/img/**" location="/img/" />-->
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--指定视图解析的路径-->
<property name="prefix" value="/WEB-INF/"/>
<!--指定解析的格式-->
<property name="suffix" value=".jsp"/>
</bean>
<!--配置上传操作解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--配置解决乱码-->
<property name="defaultEncoding" value="uft-8"/>
<!--上传单个文件的大小-->
<property name="maxUploadSizePerFile" value="19243435"/>
<!--多文件上传的总大小-->
<property name="maxUploadSize" value="321493275409327"/>
</bean>
<!--配置springMVC拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--path:表示路径-->
<!--拦截所有的URL请求-->
<mvc:mapping path="/**"/>
<!--表示不拦截的请求-->
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/myLogin"/>
<!--指定请求的类-->
<bean class="com.gtl.config.MyHandlerInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
以上配置完成,就完成了SSM的配置
下面配置服务器
web.xml的配置
<?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">
<!--配置字符集,解决中文乱码问题-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encodingFilter</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--统一交给SpringMVC进行处理-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--接收所有格式的请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置Spring(初始化Spring容器)-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--使用监听器来加载与Spring相关的配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>