springmvc--依赖
pom.xml配置
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.etoak.et2006.springmvc</groupId> <artifactId>springmvc-05</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc-05 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>privided</scope> </dependency> <!-- spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!-- jackson-databind:Jackson是一个简单的、功能强大的、基于Java的应用库。 它可以很方便完成Java对象和Json对象(xml文档or其它格式)进行互转。 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> <scope>provided</scope> </dependency> <!-- thymeleaf-spring5:java模板引擎 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.11.RELEASE</version> </dependency> <!-- spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!-- druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!-- mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.1</version> </dependency> <!-- pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> <!-- spring-aspects:Spring提供的对AspectJ框架的整合(切面) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!-- aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.2</version> </dependency> <!-- aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- commons-fileload:文件上传 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <!-- hibernate-validator:后台验证 --> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.2.Final</version> </dependency> <!-- commons-lang3: 针对java.lang包,用StringUtils方法, 和commons-collections4包配套使用 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.10</version> </dependency> <!-- commons-collections4: 针对java.util包 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency> <!-- logback-classic: 日志依赖 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- commons-codec:算法加密 --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.13</version> </dependency> </dependencies> <build> <finalName>springmvc-05</finalName> </build> </project>
2.web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <!-- 默认加载/WEB-INF/applicationContext.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-root.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- POST请求编码过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 支持表单发送put和delete请求的过滤器 --> <filter> <filter-name>rest</filter-name> <filter-class> org.springframework.web.filter.HiddenHttpMethodFilter </filter-class> </filter> <filter-mapping> <filter-name>rest</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 前端控制器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.spring-root.xml
<?xml version="1.0" encodispringng="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <context:component-scan base-package="com.etoak"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${db.driver}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.user}"></property> <property name="password" value="${db.pwd}"></property> </bean> <bean class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- SqlSessionFactoryBean --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 1.dataSource --> <property name="dataSource" ref="dataSource"></property> <!-- 2.typeAliasesPackage --> <property name="typeAliasesPackage" value="com.etoak.bean"></property> <!-- 3.mapperLocations --> <!-- mappers下所有.xml文件 --> <property name="mapperLocations" value="classpath:mappers/*.xml"></property> <!-- 4.plugins: 通用分页拦截器--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <props> <prop key="reasonable">true</prop> </props> </property> </bean> </array> </property> </bean> <!-- MapperScannerConfigurer: 扫描指定包,为接口创建代理对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.etoak.mapper"></property> </bean> <!-- 第一种声明配置 --> <!-- 1.事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 2.事务通知 --> <tx:advice id="tx-advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*"/> <tx:method name="update*"/> <tx:method name="del*"/> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.etoak.service..*.*(..))" id="tx-point"/> <aop:advisor advice-ref="tx-advice" pointcut-ref="tx-point" /> </aop:config> </beans>
4.spring-mvc.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:p="http://www.springframework.org/schema/p" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- ControllerAdvice里包含了组件注解 --> <context:component-scan base-package="com.etoak"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <mvc:default-servlet-handler /> <mvc:resources location="/static/" mapping="/js/**" /> <mvc:resources location="classpath:js/" mapping="/js/**" /> <mvc:resources location="file:d:/图片素材/" mapping="/pic/**"></mvc:resources> <!-- Thymeleaf --> <!-- 加头加尾 --> <!-- SpringResourceTemplateResolver --> <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="classpath:/templates/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML" /> <property name="characterEncoding" value="UTF-8" /> <property name="cacheable" value="false" /> </bean> <!-- SpringTemplateEngine --> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"></property> </bean> <!-- ThymeleafViewResolver --> <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine" /> <property name="characterEncoding" value="UTF-8" /> </bean> <!-- 文件上传解析器 CommonsMultipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1048576" /> </bean> <!-- 访问path跳name --> <mvc:view-controller path="/index2" view-name="index2"/> <!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 拦截所有请求 --> <mvc:mapping path="/**" /> <!-- 不拦截请求 --> <mvc:exclude-mapping path="/user/toLogin" /> <mvc:exclude-mapping path="/user/login" /> <mvc:exclude-mapping path="/user/logout" /> <mvc:exclude-mapping path="/s/**" /> <mvc:exclude-mapping path="/js/**" /> <mvc:exclude-mapping path="/pic/**" /> <!-- 配置拦截器 --> <bean class="com.etoak.interceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
5.logback.xml日志配置
<?xml version="1.0" encoding="UTF-8"?> <!--scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。 scanPeriod: 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。 当scan为true时,此属性生效。默认的时间间隔为1分钟(60 seconds)。 debug: 当此属性设置为true时,将打印出logback内部日志信息,默认值为false。 --> <configuration scan="true" scanPeriod="120 seconds" debug="false"> <!-- 定义日志文件所在目录 --> <property name="LOG_HOME" value="d:/logs/" /> <!--控制台输出 --> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <!--解决乱码问题 --> <charset>UTF-8</charset> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度 %logger输出日志的logger名 %msg:日志消息,%n是换行符 --> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level %logger{36}: %msg%n</pattern> </encoder> </appender> <!--滚动文件 --> <appender name="infoLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- ThresholdFilter:临界值过滤器,过滤掉 TRACE 和 DEBUG 级别的日志 <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> --> <!-- 过滤掉ERROR日志 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <charset>UTF-8</charset> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level %logger{36}: %msg%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/info.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory><!--保存最近30天的日志 --> </rollingPolicy> </appender> <!--滚动文件 --> <appender name="errorLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- ThresholdFilter:临界值过滤器,过滤掉 TRACE 和 DEBUG 级别的日志 --> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>error</level> </filter> <encoder> <charset>UTF-8</charset> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level %logger{36}: %msg%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.log </fileNamePattern> <maxHistory>30</maxHistory><!--保存最近30天的日志 --> </rollingPolicy> </appender> <!--这里如果是info,spring、mybatis等框架则不会输出:TRACE < DEBUG < INFO < WARN < ERROR --> <!--root是所有logger的祖先,均继承root,如果某一个自定义的logger没有指定level,就会寻找 父logger看有没有指定级别,直到找到root。 --> <root level="info"> <appender-ref ref="stdout" /> <appender-ref ref="infoLogFile" /> <appender-ref ref="errorLogFile" /> </root> <!-- 为某个包单独配置logger 比如定时任务,写代码的包名为:com.seentao.task 步骤如下: 1、定义一个appender,取名为task(随意,只要下面logger引用就行了) appender的配置按照需要即可 2、定义一个logger: <logger name="com.seentao.task" level="DEBUG" additivity="false"> <appender-ref ref="task" /> </logger> 注意:additivity必须设置为false,这样只会交给task这个appender,否则其他appender也会打印com.seentao.task里的log信息。 3、这样,在com.seentao.task的logger就会是上面定义的logger了。 private static Logger logger = LoggerFactory.getLogger(Class1.class); --> <appender name="mybatisSQL" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <charset>UTF-8</charset> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level %logger{36}: %msg%n</pattern> </encoder> </appender> <logger name="com.etoak.mapper" level="DEBUG" additivity="false"> <appender-ref ref="mybatisSQL" /> </logger> </configuration>