搭建springWeb保姆级教程
经过我们对mybatis和spring框架的学习,我们即将要用框架进行前后端数据交互,已经脱离了那种用servlet的方式进行数据传输,今天让我们来搭建最基本的springweb框架!!!
1.创建一个web项目
1.
2. 选择一个web项目
2.将spring集成mybatis
1.导入mybatis Jar包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
2.配置 sqlSessionFactory(在spring-mybatis.xml中)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--扫描SQl映射文件 -->
<property name="mapperLocations" value="classpath:mappers/*Mappers.xml">
</property>
</bean>
<!--让spring框架生成接口代理对象-->
<bean id="mapperFactory"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ffyc.ssm.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
</property>
</bean>
3.将比较常用的包导入到项目中
<!--mybatis,mysql,druid数据源,单元测试 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>provided</scope>
</dependency>
<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- spring-context,aop,jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- spring-aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
4.创建响应的包和类
5.配置对应的mybatis-config.xml文件
<?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="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写-->
<typeAliases>
<package name="com.ffyc.ssm.model"/>
</typeAliases>
</configuration>
6.配置spring.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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注解功能封装在AOP包中,开启注解扫描-->
<context:component-scan base-package="com.ffyc.ssm"> </context:component-scan>
<!--配置数据库 Aop(切面编程)的xml文件,运行时解析xml文件-->
<!--开启自动代理-->
<aop:aspectj-autoproxy />
</beans>
7.配置数据库db.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: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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置JDBC功能的配置 负责加载config.properties文件-->
<context:property-placeholder location="classpath:config.properties"></context:property-placeholder>
<!--spring统一管理数据库连接对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${uname}"></property>
<property name="password" value="${pwd}"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean>
<!-- 配置 spring 事务管理类, 并注入数据源 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
config.properties:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/数据库名?serverTimezone=Asia/Shanghai
uname=账号 //不能写成username,可能会调用系统上的username,结果报错
pwd=密码
8.集成mybatis
将数据库db.xml添加到spring-mybatis.xml中,spring-mybatis.xml添加到spring.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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 导入数据库连接,以及事务管理配置 -->
<import resource="classpath:db.xml"></import>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--扫描SQl映射文件 -->
<property name="mapperLocations" value="classpath:mappers/*Mappers.xml">
</property>
</bean>
<!--让spring框架生成接口代理对象-->
<bean id="mapperFactory"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ffyc.ssm.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
</property>
</bean>
</beans>
9.导入 SpringWeb Jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
10.在 web.xml 文件中配置 DispatcherServlet
<!-- 配置DispatcherService(请求分发servlet)-->
<servlet>
<servlet-name>application</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置读取spring配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<!-- 控制servlet创建,初始化的时间,-1第一次访问时创建,>=0是服务器启动时创建 -->
<load-on-startup>0</load-on-startup>
</servlet>
<!-- 请求映射 配置映射地址-->
<servlet-mapping>
<servlet-name>application</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
11.开启 SpringMVC 注解
新建一个xml文件,在spring.xml统一进行解析
<!-- 配置开启使用springweb中的注解-->
<mvc:annotation-driven></mvc:annotation-driven>
12.配置过滤器
<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>
13.添加 gson jar 包(json数据)
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
14.配置关于jackson Jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
15.添加过滤器 Jar包
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.5</version>
</dependency>
16.在 web.xml 中配置过滤器
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
17.注册拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/statics/**"/>
<mvc:exclude-mapping path="/loginCtl/checklogin"/>
<bean id="demo" class="com.ff.springMVC.util.DemoInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
18.添加拦截器所需要的Jar包
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
19.添加拦截器文件解析器
文件解析器
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760"></property>
</bean>