目录
spring四个核心包 spring-webmvc、spring-jdbc、spring-test、spring-aspects
jackson : POJO <-> JSON
thymeleaf-spring5
Mybatis 核心mybatis
mybatis-spring
Mybatis分页pagehelper
连接池druid
mysql驱动mysql-connector-java
单元测试junit
log4j 日志和slf4j-api
logback日志ogback-classic
servlet API(javax.servlet-api)在spring中前端控制器dispatchservlet继承的就是这个依赖里的httpservlet
文件上传commons-fileupload
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- <spring.version>5.3.1spring.version>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.16.10version>
- dependency>
-
-
-
-
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.12.1version>
- dependency>
-
-
- <dependency>
- <groupId>org.thymeleafgroupId>
- <artifactId>thymeleaf-spring5artifactId>
- <version>3.0.12.RELEASEversion>
- dependency>
-
-
-
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.7version>
- dependency>
-
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>5.2.0version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.6version>
- dependency>
-
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.0.9version>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.16version>
- dependency>
-
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>log4jgroupId>
- <artifactId>log4jartifactId>
- <version>1.2.17version>
- dependency>
-
-
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- <version>1.7.36version>
- dependency>
-
-
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.2.3version>
- dependency>
-
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
-
-
- <dependency>
- <groupId>commons-fileuploadgroupId>
- <artifactId>commons-fileuploadartifactId>
- <version>1.3.1version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>5.2.0version>
- dependency>
- dependencies>
1. 编码控制器(请求响应编码)
2. 前端控制器
3. 监听器listener,配置Spring监听器:服务器启动的时候加载Spring的配置文件
4. 声明spring配置文件位置
5. HiddenHttpMethodFilter 设置处理请球方法put和delete
5. HiddenHttpMethodFilter 设置处理请球方法put和delete
- "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>CharacterEncodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encondingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- <init-param>
- <param-name>forceResponseEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
-
-
- <servlet>
- <servlet-name>DispatcherServletservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:springMVC.xmlparam-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>DispatcherServletservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
-
-
-
-
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
-
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:spring.xmlparam-value>
- context-param>
-
-
-
- <filter>
- <filter-name>HiddenHttpMethodFilterfilter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
- filter>
- <filter-mapping>
- <filter-name>HiddenHttpMethodFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>
1. 扫描组件context:component-scan
2. thymeleaf
3. mvc:view-controller视图控制器
4. mvc:default-servlet-handler处理静态资源,加上静态资源static后要重新在maven中打包
5. mvc:annotation-driven开启mvc注解驱动
6. multipartResolver 必须通过文件解析器的解析才能将通过表单上传的文件转换为MultipartFile对象
7. 拦截器
8. 异常处理器
- "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.zt">context:component-scan>
-
-
- <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
- <property name="order" value="1"/>
- <property name="characterEncoding" value="UTF-8"/>
- <property name="templateEngine">
- <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
- <property name="templateResolver">
- <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
-
- <property name="prefix" value="/WEB-INF/templates/"/>
-
- <property name="suffix" value=".html"/>
- <property name="templateMode" value="HTML5"/>
- <property name="characterEncoding" value="UTF-8" />
- bean>
- property>
- bean>
- property>
- bean>
-
-
- <mvc:default-servlet-handler>mvc:default-servlet-handler>
-
- <mvc:annotation-driven>mvc:annotation-driven>
- <mvc:view-controller path="/" view-name="index"/>
-
-
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
-
-
-
- beans>
1. 扫描组件,除控制层,(将出去控制层以外的所有组件,都交给sping.xml管理,控制层是springmvc管理)
2. 引入数据源jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/ibatis?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8 jdbc.user=root jdbc.password=761311 jdbc.driver=com.mysql.cj.jdbc.Driver
3. 配置数据源(这一部分也可以在mybatis-config中配置)
4. 配置、开启事务管理器
使用#Transctional注解标识的所有方法和类 进行事务管理
5. 将SqlSessionFactoryBean配置到IOC容器中,就可以在容器中获取SqlSessionFactoryBean的对象。
6. 获取mybatis-config.xml配置(这一部分在spring和mybatis-config中都可以配置)
配置上面的数据源、设置typeAliasesPackage 实体类包中的别名
7. 分页插件(这一部分也可以在mybatis-config中配置)
8. 引入mapper文件,mapper扫描(在mybatis-config中就不用引入映射文件了)
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:util="http://www.springframework.org/schema/util"
- 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-4.0.xsd
- http://www.springframework.org/schema/util
- https://www.springframework.org/schema/util/spring-util.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">
-
-
-
-
- <context:component-scan base-package="com.zt.ssm">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
-
-
-
- <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
-
-
-
- <bean id="transctionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource">property>
- bean>
- <tx:annotation-driven/>
-
-
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
-
-
- <property name="driverClassName" value="${jdbc.driver}">property>
- <property name="url" value="${jdbc.url}">property>
- <property name="username" value="${jdbc.user}">property>
- <property name="password" value="${jdbc.password}">property>
-
-
- <property name="initialSize" value="10">property>
- <property name="maxActive" value="16">property>
- bean>
-
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="configLocation" value="classpath:mybatis-config.xml">property>
- <property name="dataSource" ref="dataSource">property>
- <property name="typeAliasesPackage" value="com.zt.ssm.pojo">property>
-
-
- bean>
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.zt.ssm.mapper">property>
- bean>
-
- beans>
1. 引入jdbc.properties
2. setting-mapUnderscoreToCamelCase将下划线映射为驼峰 解决数据库和属性名不一致问题
3. setting-lazyLoadingEnabled/aggressiveLazyLoading延迟加载 按需加载
4. typeAliases设置当前实体类的别名(spring可以设置)
5. 配置连接数据库环境
6. 引入 MyBatis 的映射文件 : 存放SQL语句 和 POJO的映射方式
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//com.zt.mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
-
- <configuration>
-
-
-
-
-
-
-
- <settings>
- <setting name="mapUnderscoreToCamelCase" value="true"/>
- <setting name="lazyLoadingEnabled" value="true"/>
- <setting name="aggressiveLazyLoading" value="false"/>
- settings>
-
-
-
-
- <plugins>
-
- <plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
- plugins>
-
-
-
-
-
-
- configuration>