目录
1.4 导入spring-mybatis、spring-mvc、spring-base、spring-redis四种集成文件
·1.6.1 适用于支持java web应用跨资源源共享(CORS)的首个统一解决方案的文件
注:将jkd版本切换至1.8,web.xml的版本切换至3.1
- "1.0" encoding="UTF-8"?>
-
- <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.0modelVersion>
-
- <groupId>com.naragroupId>
- <artifactId>ssmdemoartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>ssmdemo Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.3.18version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>5.3.18version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.3.18version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>5.3.18version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>5.3.18version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.18version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-ormartifactId>
- <version>5.3.18version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>5.3.18version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.6version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.6version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>5.1.2version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.44version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-dbcp2artifactId>
- <version>2.1.1version>
- dependency>
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- <version>2.4.3version>
- dependency>
-
-
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- <version>1.7.36version>
- dependency>
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.2.10version>
- dependency>
-
-
- <dependency>
- <groupId>redis.clientsgroupId>
- <artifactId>jedisartifactId>
- <version>2.9.0version>
- dependency>
-
- <dependency>
- <groupId>org.springframework.datagroupId>
- <artifactId>spring-data-redisartifactId>
- <version>1.7.2.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-coreartifactId>
- <version>2.9.4version>
- dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.9.4version>
- dependency>
-
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.22version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
-
- dependencies>
-
- <build>
- <finalName>ssmdemofinalName>
- <pluginManagement>
- <plugins>
- <plugin>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.1.0version>
- plugin>
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.0version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <version>2.22.1version>
- plugin>
- <plugin>
- <artifactId>maven-war-pluginartifactId>
- <version>3.2.2version>
- plugin>
- <plugin>
- <artifactId>maven-install-pluginartifactId>
- <version>2.5.2version>
- plugin>
- <plugin>
- <artifactId>maven-deploy-pluginartifactId>
- <version>2.8.2version>
- plugin>
- plugins>
- pluginManagement>
- build>
- project>
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/tmpdb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
- jdbc.username=root
- jdbc.password=123456
注:tmpdb是数据库的名称
- "1.0" encoding="UTF-8"?>
- <configuration debug="false">
-
-
- <property name="LOG_HOME" value="d:\\temp\\mylogs" />
-
- <property name="LOG_NAME" value="ssm"/>
-
-
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
-
- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
- encoder>
- appender>
-
-
- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
- <FileNamePattern>${LOG_HOME}/${LOG_NAME}.%d{yyyy-MM-dd}.logFileNamePattern>
-
- <MaxHistory>30MaxHistory>
- rollingPolicy>
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
-
- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
- encoder>
-
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <MaxFileSize>10MBMaxFileSize>
- triggeringPolicy>
- appender>
-
-
- <logger name="com.apache.ibatis" level="TRACE"/>
- <logger name="java.sql.Connection" level="DEBUG"/>
- <logger name="java.sql.Statement" level="DEBUG"/>
- <logger name="java.sql.PreparedStatement" level="DEBUG"/>
-
-
-
- <root level="DEBUG">
- <appender-ref ref="STDOUT" />
- <appender-ref ref="FILE"/>
- root>
-
- configuration>
- redis.host=localhost
- redis.port=6379
- redis.maxTotal=10
- redis.maxIdle=5
- redis.testOnBorrow=true
- "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-3.2.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
-
-
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="dataSource" ref="dataSource"/>
-
-
-
- <property name="mapperLocations" value="classpath:/mapper/**/*.xml"/>
-
- <property name="plugins">
- <list>
- <bean class="com.github.pagehelper.PageInterceptor">
- <property name="properties">
-
- <value>
- helperDialect=mysql
- value>
- property>
- bean>
- list>
- property>
-
- bean>
-
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
-
- <property name="basePackage" value="com/zking/**/mapper"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- bean>
-
- beans>
- "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 http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
-
-
-
- <context:component-scan base-package="com.zking" use-default-filters="false">
-
-
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
-
-
-
-
- <mvc:annotation-driven>mvc:annotation-driven>
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView">property>
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- bean>
-
-
- <mvc:resources location="/static/" mapping="/static/**" cache-period="31536000"/>
-
-
- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
- <property name="messageConverters">
- <list>
- <ref bean="mappingJackson2HttpMessageConverter"/>
- list>
- property>
- bean>
-
-
- <bean id="mappingJackson2HttpMessageConverter"
- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
-
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8value>
- <value>text/json;charset=UTF-8value>
- <value>application/json;charset=UTF-8value>
- list>
- property>
- bean>
- beans>
- "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-3.2.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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.zking"/>
-
-
-
-
-
- <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
-
-
-
- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
-
- <property name="initialSize" value="10"/>
-
- <property name="maxTotal" value="100"/>
-
- <property name="maxIdle" value="50"/>
-
- <property name="minIdle" value="10"/>
-
-
- <property name="maxWaitMillis" value="-1"/>
-
- <property name="testWhileIdle" value="true">property>
- <property name="validationQuery" value="select 1">property>
-
- bean>
-
-
-
-
- <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>
-
-
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- bean>
-
-
- <tx:annotation-driven transaction-manager="transactionManager"/>
-
- beans>
- "1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
-
-
-
-
- <context:property-placeholder location="classpath:redis.properties"
- ignore-unresolvable="true"/>
-
-
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxTotal" value="${redis.maxTotal}" />
- <property name="maxIdle" value="${redis.maxIdle}" />
- <property name="testOnBorrow" value="${redis.testOnBorrow}" />
- bean>
-
-
- <bean id="jedisConnectionFactory"
- class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="hostName" value="${redis.host}">property>
- <property name="port" value="${redis.port}">property>
-
- <property name="poolConfig" ref="poolConfig">property>
- bean>
-
-
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnectionFactory" />
-
-
- <property name="keySerializer">
- <bean
- class="org.springframework.data.redis.serializer.StringRedisSerializer" />
- property>
- <property name="valueSerializer">
- <bean
- class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
- property>
- <property name="hashKeySerializer">
- <bean
- class="org.springframework.data.redis.serializer.StringRedisSerializer" />
- property>
- <property name="hashValueSerializer">
- <bean
- class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
- property>
-
- <property name="enableTransactionSupport" value="true">
- property>
- bean>
-
-
- <cache:annotation-driven order="2" cache-manager="cacheManager" key-generator="customKeyGenerator"/>
-
- <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
- <constructor-arg index="0" ref="redisTemplate">constructor-arg>
-
-
- bean>
-
- <bean id="customKeyGenerator" class="com.zking.ssmdemo.util.CustomKeyGenerator"/>
- beans>
- "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_3_1.xsd"
- version="3.1">
-
-
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath*:spring-*.xmlparam-value>
- context-param>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
- <filter>
- <filter-name>encodingFilterfilter-name>
- <filter-class>com.zking.ssmdemo.util.EncodingFilterfilter-class>
- filter>
- <filter-mapping>
- <filter-name>encodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
- <filter>
- <filter-name>corsFilterfilter-name>
- <filter-class>com.zking.ssmdemo.util.CorsFilterfilter-class>
- filter>
- <filter-mapping>
- <filter-name>corsFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
-
- <servlet>
- <servlet-name>SpringMVCservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath*:spring-mvc.xmlparam-value>
- init-param>
-
- <load-on-startup>1load-on-startup>
-
- <async-supported>trueasync-supported>
- servlet>
- <servlet-mapping>
- <servlet-name>SpringMVCservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
-
- web-app>
- package com.zking.ssmdemo.util;
-
- import javax.servlet.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- /**
- * 配置tomcat允许跨域访问
- *
- * @author Administrator
- *
- */
- public class CorsFilter implements Filter {
-
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- }
-
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
- throws IOException, ServletException {
- HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
- HttpServletRequest req = (HttpServletRequest) servletRequest;
- // Access-Control-Allow-Origin就是我们需要设置的域名
- // Access-Control-Allow-Headers跨域允许包含的头。
- // Access-Control-Allow-Methods是允许的请求方式
- httpResponse.setHeader("Access-Control-Allow-Origin", "*");// *,任何域名
- httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
- httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,jwt");
- //允许客户端,处理一个新的响应头jwt
- httpResponse.setHeader("Access-Control-Expose-Headers", "jwt");
-
- // axios的ajax会发两次请求,第一次提交方式为:option,直接返回即可
- if ("OPTIONS".equals(req.getMethod())) {
- return;
- }
-
- filterChain.doFilter(servletRequest, servletResponse);
- }
-
- @Override
- public void destroy() {
-
- }
- }
- package com.zking.ssmdemo.util;
-
- import org.springframework.cache.interceptor.KeyGenerator;
-
- import java.lang.reflect.Method;
-
- public class CustomKeyGenerator implements KeyGenerator {
- @Override
- public Object generate(Object target, Method method, Object... params) {
- StringBuilder sb = new StringBuilder();
- sb.append(target.getClass().getName());
- sb.append(method.getName());
- for(Object param: params) {
- sb.append(param.toString());
- }
- return sb;
- }
- }
- package com.zking.ssmdemo.util;
-
- import javax.servlet.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- /**
- * 中文乱码处理
- *
- */
- public class EncodingFilter implements Filter {
-
- private String encoding = "UTF-8";// 默认字符集
-
- public EncodingFilter() {
- super();
- }
-
- public void destroy() {
- }
-
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpServletRequest req = (HttpServletRequest) request;
- HttpServletResponse res = (HttpServletResponse) response;
-
- // 中文处理必须放到 chain.doFilter(request, response)方法前面
- res.setContentType("text/html;charset=" + this.encoding);
- if (req.getMethod().equalsIgnoreCase("post")) {
- req.setCharacterEncoding(this.encoding);
- }
-
- chain.doFilter(request, response);
- }
-
- public void init(FilterConfig filterConfig) throws ServletException {
- String s = filterConfig.getInitParameter("encoding");// 读取web.xml文件中配置的字符集
- if (null != s && !s.trim().equals("")) {
- this.encoding = s.trim();
- }
- }
-
- }
- package com.zking.ssmdemo.util;
-
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.stereotype.Component;
-
- /**
- * @author L
- * @site www.xiaomage.com
- * @company xxx公司
- * @create 2022-11-09 15:40
- */
- @Component
- public class SpringUtils implements ApplicationContextAware {
-
- private static ApplicationContext ctx;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- ctx = applicationContext;
- }
-
- public static <T> T getBean(String id) {
- return (T)ctx.getBean(id);
- }
- }
自定义的注解也有(Mylog AOP。。。)
value,name两个自定义参数