spring+spring mvc+mybatis plus+jpa+freeMarker+redis+swagger2集成

一、前序

    之前我集成过spring+spring mvc+mybatis+jpa+freeMarker+redis的框架,也使用了一段时间,发现其中的问题也是有很多的,至此我在原来的框架上做了升级和优化,集成了这个新的框架,一个好的框架,对于开发速度来说,是有很大提升的,反之框架不好用,开发速度也会受到很大的影响,此篇博客我会详细说明一些这些框架的坑,以及处理方案;

   框架环境

          maven:3.0版本 阿里公开库(http://maven.aliyun.com/nexus/content/groups/public//)

      jdk:1.8

      tomcat:8.5

      开发工具:idea2017

      数据库:mysql 5.6

     如框架有啥不足之处,希望各位大神指出,我会积极改进优化

二、开始集成

 1、项目结构

spring+spring mvc+mybatis plus+jpa+freeMarker+redis+swagger2集成

 

 

 

 2、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.SPJ</groupId>
    <artifactId>SPJ</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SPJ Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <!-- 设置项目编码编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- spring版本号 -->
        <spring.version>5.2.0.RELEASE</spring.version>
    </properties>

    <dependencies>

        <!-- freemarker模板-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.29</version>
        </dependency>

        <!-- java ee -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>


        <!-- jedis依赖 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-tx</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-data-commons</artifactId>
                    <groupId>org.springframework.data</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 数据库 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
            <scope>runtime</scope>
        </dependency>
        <!-- mybais puls -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.2.0</version>
        </dependency>


        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- 连接池配置包 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>


        <!-- spring data jpa 数据库持久层 -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

        <!-- hibernate核心包 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>javassist</artifactId>
                    <groupId>org.javassist</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- hibernate实体管理包 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.5.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>javassist</artifactId>
                    <groupId>org.javassist</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- optional -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-osgi</artifactId>
            <version>4.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>4.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>4.3.5.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>mchange-commons-java</artifactId>
                    <groupId>com.mchange</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>c3p0</artifactId>
                    <groupId>com.mchange</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-proxool</artifactId>
            <version>4.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-infinispan</artifactId>
            <version>4.3.5.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>jboss-logging</artifactId>
                    <groupId>org.jboss.logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.3.5.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>


        <!-- 上传组件包 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-io</artifactId>
                    <groupId>commons-io</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.13</version>
        </dependency>

        <!-- 分页依赖 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
        </dependency>

        <!-- 解析表格依赖-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-codec</artifactId>
                    <groupId>commons-codec</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--处理2007 excel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>

        <!-- gson 解析 -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>

        <!-- 定时器Quertz -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>c3p0</artifactId>
                    <groupId>com.mchange</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- token使用json -->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
            <exclusions>
                <exclusion>
                    <artifactId>commons-lang</artifactId>
                    <groupId>commons-lang</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 时间格式化-->
        <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.6</version>
            <exclusions>
                <exclusion>
                    <artifactId>jackson-annotations</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>


        <!-- Commons.Lang -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>


        <!-- 事物管理  -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>


        <!-- 请求头参数分析包 -->
        <dependency>
            <groupId>eu.bitwalker</groupId>
            <artifactId>UserAgentUtils</artifactId>
            <version>1.21</version>
        </dependency>
        <!-- 请求头参数分析包 -->
        <dependency>
            <groupId>cz.mallat.uasparser</groupId>
            <artifactId>uasparser</artifactId>
            <version>0.6.2</version>
        </dependency>
        <!-- 请求头参数分析包 -->
        <dependency>
            <groupId>net.sourceforge.jregex</groupId>
            <artifactId>jregex</artifactId>
            <version>1.2_01</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.10</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-codec</artifactId>
                    <groupId>commons-codec</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>jackson-annotations</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>swagger-annotations</artifactId>
                    <groupId>io.swagger</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>swagger-models</artifactId>
                    <groupId>io.swagger</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
            <exclusions>
                <exclusion>
                    <artifactId>jackson-annotations</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>SPJ</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!-- 设置JDK版本 -->
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3、spring-mvc.xml(扫描bean,配置静态资源,加载配置文件,加载ftl模板,json转换,日期转换,拦截器配置,线程池配置,配置log切面日志)

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                            http://www.springframework.org/schema/cache
                            http://www.springframework.org/schema/cache/spring-cache.xsd
                            xmlns:task=http://www.springframework.org/schema/task
                            http://www.springframework.org/schema/task/spring-task-3.2.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 扫描web相关的controller -->
    <context:component-scan base-package="com.spj.controller"/>
    <!-- 扫描web相关的service -->
    <context:component-scan base-package="com.spj.service"/>
    <!-- 扫描web相关的jpa -->
    <context:component-scan base-package="com.spj.jpa"/>
    <!-- 扫描web相关的task -->
    <context:component-scan base-package="com.spj.task"/>
    <!-- 扫描web相关的config-->
    <context:component-scan base-package="com.spj.config"/>

    <!-- 映入redis配置文件 -->
    <import resource="spring-redis.xml"/>

    <!-- 静态资源默认servlet配置 -->
    <mvc:default-servlet-handler/>

    <!-- 处理静态资源文件 -->
    <mvc:resources mapping="/static/**" location="/static/" />


    <!-- 加载系统参数配置文件 -->
    <context:property-placeholder location="classpath:sysconfig.properties" ignore-unresolvable="true"/>

    <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8"/>
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000"/>
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960"/>
    </bean>

    <!-- 配置freeMarker视图解析器 -->
    <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
        <property name="contentType" value="text/html; charset=UTF-8"/>
        <property name="exposeRequestAttributes" value="true"/>
        <property name="exposeSessionAttributes" value="true"/>
        <property name="exposeSpringMacroHelpers" value="true"/>
        <property name="requestContextAttribute" value="request"/>
        <property name="allowSessionOverride" value="true"/>
        <property name="cache" value="true"/>
        <property name="suffix" value=".ftl"/>
        <property name="prefix" value=""/>
        <property name="order" value="0"/>
    </bean>

    <!-- 配置freeMarker的模板路径 -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <!--<property name="templateLoaderPath" value="/WEB-INF/ftl/"/>-->
        <property name="templateLoaderPaths">
            <list>
                <value>/WEB-INF/views/</value>
            </list>
        </property>
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape"/>
            </map>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">1</prop>
                <prop key="number_format">#.##</prop>
                <prop key="object_wrapper">freemarker.ext.beans.BeansWrapper</prop>
                <prop key="template_exception_handler">com.spj.config.MyFtlTemplateExceptionHandler</prop>
            </props>
        </property>
    </bean>
    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>



    <!-- json转换器 -->
    <bean id="jsonConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
        <property name="fastJsonConfig">
            <bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
                <property name="features">
                    <list>
                        <value>AllowArbitraryCommas</value>
                        <value>AllowUnQuotedFieldNames</value>
                        <value>DisableCircularReferenceDetect</value>
                    </list>
                </property>
                <!-- 配置同意json返回的日期格式 -->
                <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>
            </bean>
        </property>
    </bean>

    <mvc:annotation-driven conversion-service="conversionService">
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                        <property name="dateFormat">
                            <bean class="java.text.SimpleDateFormat">
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
                            </bean>
                        </property>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 自定义日期转换类 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <!--日期输入参数转换器 -->
                <bean class="com.spj.common.DateConverter"/>
            </set>
        </property>
    </bean>

    <!-- 自定义拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.spj.interceptor.BaseInterceptor"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/api/**"/>
            <bean class="com.spj.interceptor.PerformanceInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>


    <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
    <cache:annotation-driven cache-manager="redisCacheManager"/>

    <!-- spring线程池-->
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 线程池维护线程的最少数量 -->
        <property name="corePoolSize" value="5" />
        <!-- 线程池维护线程所允许的空闲时间,默认为60s  -->
        <property name="keepAliveSeconds" value="200" />
        <!-- 线程池维护线程的最大数量 -->
        <property name="maxPoolSize" value="20" />
        <!-- 缓存队列最大长度 -->
        <property name="queueCapacity" value="20" />
        <!-- 对拒绝task的处理策略   线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者-->
        <property name="rejectedExecutionHandler">
            <!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->
            <!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->
            <!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
            <!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
            <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
        </property>
        <property name="waitForTasksToCompleteOnShutdown" value="true" />
    </bean>


    <!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!-- Log切面拦截处理类-->
    <bean id="aspectService" class="com.spj.config.LogAspectService"></bean>
    <!-- AOP配置 -->
    <aop:config>
        <!-- 声明一个切面,并注入切面Bean,相当于@Aspect -->
        <aop:aspect id="simpleAspect" ref="aspectService">
            <!-- 配置一个切入点,相当于@Pointcut -->
            <aop:pointcut expression="execution(* com.spj..*.*(..)))" id="simplePointcut"/>
            <aop:after-throwing pointcut-ref="simplePointcut" method="afterThrow" throwing="ex"/>
        </aop:aspect>
    </aop:config>

</beans>

4、spring-mybatis-jpa.xml(这里mybatis puls 和 jpa 使用的是同一个连接池,这里也可以分开使用两个连接池,单数据源使用一个连接池就可以了)

  (1)mybatis puls 官网:https://mp.baomidou.com/config/#基本配置

       (2)jpa 官网:https://spring.io/projects/spring-data-jpa

 

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       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/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       ">


    <!-- 配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
        <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/>
        <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
    </bean>


    <!-- mybatis puls配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="configuration" ref="configuration"/>
        <property name="globalConfig" ref="globalConfig"/>
        <!-- 扫描model包 使用别名 -->
        <property name="typeAliasesPackage" value="com.spj.model"/>
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <value>
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <bean id="configuration" class="com.baomidou.mybatisplus.core.MybatisConfiguration">
        <!-- 是否开启自动驼峰命名映射 -->
        <property name="mapUnderscoreToCamelCase" value="true"></property>
    </bean>
    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <!-- 是否打印mybatis的banner -->
        <property name="banner" value="true"></property>
        <property name="dbConfig" ref="dbConfig"/>
    </bean>
    <bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
        <!-- 全局默认主键类型 -->
        <property name="idType" value="NONE"></property>
    </bean>

    <!-- jpa数据连接管理工厂 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="FastDFSPersistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.spj.model" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!-- 是否自动建表 -->
                <property name="generateDdl" value="false"/>
                <!-- 是否展示sql -->
                <property name="showSql" value="true"/>
                <!-- 必要的数据库库使用的详细信息 -->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                <!-- mysql,自行选择 -->
                <property name="database" value="MYSQL"/>
            </bean>
        </property>
        <!-- 指定JPA属性;如Hibernate中指定是否显示SQL的是否显示、方言等 -->
        <property name="jpaProperties">
            <props>
                <!--启用驼峰命名 -->
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
        </property>
    </bean>


    <!-- mybatis puls配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.spj.dao"/>
    </bean>


    <!-- mybatis配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- mybatis注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- jpa配置启用扫描并自动创建代理的功能  factory-class="com.monk.base.jpa.PeakJpaRepositoryFactory"自己定义的bean注解方式,可以不写,直接注解所有包下的 -->
    <jpa:repositories base-package="com.spj.jpa" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>

    <!-- 配置JPA的注解 -->
    <bean id="transactionJpaManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>
    <!-- jpa配置基于注解的事务 -->
    <tx:annotation-driven transaction-manager="transactionJpaManager" proxy-target-class="true"/>



    <!-- mybatis拦截器方式配置事物 -->
    <tx:advice id="mybatisTransactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>
            <tx:method name="append*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="repair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delAndRepair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="find*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="load*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="search*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="datagrid*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>

    <!-- jpa拦截器方式配置事物 -->
    <tx:advice id="jpaTransactionAdvice" transaction-manager="transactionJpaManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>
            <tx:method name="append*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="repair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delAndRepair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="find*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="load*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="search*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="datagrid*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>
    <!-- mybatis把事务控制在dao层 -->
    <aop:config>
        <aop:pointcut id="mybatisPointcut" expression="execution(* com.spj.dao.*.*(..))"/>
        <aop:advisor pointcut-ref="mybatisPointcut" advice-ref="mybatisTransactionAdvice"/>
    </aop:config>
    <!-- jpa把事务控制在jpa层 -->
    <aop:config>
        <aop:pointcut id="jpaPointcut" expression="execution(* com.spj.jpa.*.*(..))"/>
        <aop:advisor pointcut-ref="jpaPointcut" advice-ref="jpaTransactionAdvice"/>
    </aop:config>

</beans>

5、redis配置

 

<?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"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:util="http://www.springframework.org/schema/util"
       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/util
                           http://www.springframework.org/schema/util/spring-util-3.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                           http://www.springframework.org/schema/cache
                           http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">


    <!-- 连接池基本参数配置,类似数据库连接池 -->
    <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>

    <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
    <cache:annotation-driven cache-manager="redisCacheManager"/>

    <!-- redis连接池 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxActive}"/>
        <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="password" value="${redis.pass}"></property> -->
        <property name="poolConfig" ref="poolConfig"></property>
    </bean>

    <!--redis操作模版,使用该对象可以操作redis  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </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>

    <!-- spring自己的缓存管理器,这里定义了缓存位置名称 ,即注解中的value -->
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <!-- 这里可以配置多个redis -->
                <!-- <bean class="com.cn.util.RedisCache">
                     <property name="redisTemplate" ref="redisTemplate" />
                     <property name="name" value="default"/>
                </bean> -->
                <bean class="com.spj.cache.RedisCache">
                    <property name="redisTemplate" ref="redisTemplate"/>
                    <property name="name" value="common"/>
                    <!-- common名称要在类或方法的注解中使用 -->
                </bean>
            </set>
        </property>
    </bean>

    <!-- 配置缓存 -->
    <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg ref="redisTemplate"/>
        <!--<property name="cacheNames">-->
            <!--<list>-->
                <!--<value>adminUserCache</value>-->
                <!--<value>lotteryTypeCache</value>-->
                <!--<value>lotteryPlayMethodCache</value>-->
                <!--<value>adminNoticeCache</value>-->
            <!--</list>-->
        <!--</property>-->
        <!--默认缓存超时时间(秒) -->
        <property name="defaultExpiration" value="${redis.defaultExpiration}"/>
        <!-- 给特定的cacheName设置超时时间 (秒)-->
        <!--<property name="expires">-->
            <!--<util:map>-->
                <!--<entry value="1800" key="adminUserCache"/>-->
                <!--<entry value="1800" key="lotteryTypeCache"/>-->
                <!--<entry value="1800" key="lotteryPlayMethodCache"/>-->
                <!--<entry value="86400" key="adminNoticeCache"/>-->
            <!--</util:map>-->
        <!--</property>-->
    </bean>

</beans>

6、spring-task.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
                            http://www.springframework.org/schema/task  
                            http://www.springframework.org/schema/task/spring-task-3.2.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <!-- 配置注解扫描 -->
    <context:annotation-config/>
    <!-- Spring定时器注解开关-->
    <task:annotation-driven></task:annotation-driven>

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <!-- 此处对于定时时间的配置会被注解中的时间配置覆盖,因此,以注解配置为准-->
    <!--<task:scheduled-tasks scheduler="myScheduler">-->
    <!--</task:scheduled-tasks>-->


    <!-- 配置线程池 -->
    <task:scheduler id="myScheduler" pool-size="20"/>



</beans>

7、applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/cache
                            http://www.springframework.org/schema/cache/spring-cache.xsd
                            http://www.springframework.org/schema/util
                            http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <aop:aspectj-autoproxy/>
    <!-- 扫描web相关的controller -->
    <context:component-scan base-package="com.spj.controller"/>
    <!-- 扫描web相关的service -->
    <context:component-scan base-package="com.spj.service"/>
    <!-- 扫描web相关的jpa -->
    <context:component-scan base-package="com.spj.jpa"/>
    <!-- 扫描web相关的task -->
    <context:component-scan base-package="com.spj.task"/>
    <!-- 扫描配置-->
    <context:component-scan base-package="com.spj.config"/>


    <!-- 开启SpringMVC注解模式 -->
    <mvc:annotation-driven/>
    <!-- 引入配置文件 -->
    <import resource="classpath*:environment.config.xml"/>
    <!-- 配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
    <context:property-placeholder location="classpath:sysconfig.properties" ignore-unresolvable="true"/>
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
        <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/>
        <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
    </bean>

    <!-- jpa数据连接管理工厂 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="FastDFSPersistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.spj.model" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!-- 是否生成ddl文件 -->
                <property name="generateDdl" value="true"/>
                <!-- 是否展示sql -->
                <property name="showSql" value="false"/>
                <!-- 必要的数据库库使用的详细信息 -->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                <!-- mysql,自行选择 -->
                <property name="database" value="MYSQL"/>
            </bean>
        </property>
        <!-- 指定JPA属性;如Hibernate中指定是否显示SQL的是否显示、方言等 -->
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
        </property>
    </bean>

    <!-- mybatis配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="configuration" ref="configuration"/>
        <property name="globalConfig" ref="globalConfig"/>
        <!-- 扫描model包 使用别名 -->
        <property name="typeAliasesPackage" value="com.spj.model"/>
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <value>
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <bean id="configuration" class="com.baomidou.mybatisplus.core.MybatisConfiguration">
        <!-- 是否开启自动驼峰命名映射 -->
        <property name="mapUnderscoreToCamelCase" value="true"></property>
    </bean>
    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <!-- 是否打印mybatis的banner -->
        <property name="banner" value="true"></property>
        <property name="dbConfig" ref="dbConfig"/>
    </bean>
    <bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
        <!-- 全局默认主键类型 -->
        <property name="idType" value="NONE"></property>
    </bean>

    <!-- mybatis自动扫描mapper接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.spj.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
    </bean>


    <!-- mybatis配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- mybatis注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- JPA配置启用扫描并自动创建代理的功能  factory-class="com.monk.base.jpa.PeakJpaRepositoryFactory"自己定义的bean注解方式,可以不写,直接注解所有包下的 -->
    <jpa:repositories base-package="com.spj.jpa" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
    <!-- JPA配置的事务管理器 -->
    <bean id="transactionJpaManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>
    <!-- JPA配置基于注解的事务 -->
    <tx:annotation-driven transaction-manager="transactionJpaManager"/>



    <!-- mybatis拦截器方式配置事物 -->
    <tx:advice id="mybatisTransactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>
            <tx:method name="append*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="repair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delAndRepair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="find*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="load*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="search*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="datagrid*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>

    <!-- JPA拦截器方式配置事物 -->
    <tx:advice id="jpaTransactionAdvice" transaction-manager="transactionJpaManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>
            <tx:method name="append*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="repair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delAndRepair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="find*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="load*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="search*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="datagrid*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>
    <!-- mybatis把事务控制在service层 -->
    <aop:config>
        <aop:pointcut id="mybatisPointcut" expression="execution(* com.spj.service.*.*(..))"/>
        <aop:advisor pointcut-ref="mybatisPointcut" advice-ref="mybatisTransactionAdvice"/>
    </aop:config>

    <!-- jpa把事务控制在service层 -->
    <aop:config>
        <aop:pointcut id="jpaPointcut" expression="execution(* com.spj.jpa.*.*(..))"/>
        <aop:advisor pointcut-ref="jpaPointcut" advice-ref="jpaTransactionAdvice"/>
    </aop:config>

    <!-- rest client -->
    <bean id="httpClientFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">
        <property name="connectTimeout" value="10000"/>
        <property name="readTimeout" value="10000"/>
    </bean>

    <!--RestTemplate-->
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <constructor-arg ref="httpClientFactory"/>
    </bean>

    <import resource="spring-redis.xml"/>

    <!-- spring线程池-->
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 线程池维护线程的最少数量 -->
        <property name="corePoolSize" value="5" />
        <!-- 线程池维护线程所允许的空闲时间,默认为60s  -->
        <property name="keepAliveSeconds" value="200" />
        <!-- 线程池维护线程的最大数量 -->
        <property name="maxPoolSize" value="20" />
        <!-- 缓存队列最大长度 -->
        <property name="queueCapacity" value="20" />
        <!-- 对拒绝task的处理策略   线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者-->
        <property name="rejectedExecutionHandler">
            <!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->
            <!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->
            <!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
            <!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
            <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
        </property>
        <property name="waitForTasksToCompleteOnShutdown" value="true" />
    </bean>


    <!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!-- Log切面拦截处理类-->
    <bean id="aspectService" class="com.spj.config.LogAspectService"></bean>
    <!-- AOP配置 -->
    <aop:config>
        <!-- 声明一个切面,并注入切面Bean,相当于@Aspect -->
        <aop:aspect id="simpleAspect" ref="aspectService">
            <!-- 配置一个切入点,相当于@Pointcut -->
            <aop:pointcut expression="execution(* com.spj..*.*(..)))" id="simplePointcut"/>
            <aop:after-throwing pointcut-ref="simplePointcut" method="afterThrow" throwing="ex"/>
        </aop:aspect>
    </aop:config>

</beans>

8、jdbc.properties

 

jdbc.driver=com.mysql.jdbc.Driver
#测试数据库
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
#用户名
jdbc.username=root
#密码
jdbc.password=123456
#连接池中保留的最大连接数。Default: 15
c3p0.maxPoolSize=30
#连接池中保留的最小连接数。
c3p0.minPoolSize=10
#连接关闭时默认将所有未提交的操作回滚。默认为false
c3p0.autoCommitOnClose=false
#当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒,默认为0;
c3p0.checkoutTimeout=10000
#定义在从数据库获取新连接失败后重复尝试的次数。Default: 30
c3p0.acquireRetryAttempts=2
#每60秒检查所有连接池中的空闲连接。Default: 0
c3p0.idleConnectionTestPeriod=60
#最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0
c3p0.maxIdleTime=60

9、redis.properties

#访问地址
redis.host=127.0.0.1
redis.database=0
#访问端口
redis.port=6379
#注意,如果没有password,此处不设置值,但这一项要保留
redis.password=
#最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
redis.maxIdle=300
#连接池的最大数据库连接数。设为0表示无限制
redis.maxActive=600
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWait=1000
#在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
redis.testOnBorrow=true
#最大连接数:能够同时建立的“最大链接个数”
redis.maxTotal=1000
#最小空闲数
redis.minIdle=3
#空闲对象被清除需要达到的最小空闲时间
redis.minEvictableIdleTimeMillis=30000
#空闲检测线程,sleep 间隔多长时间,去处理与idle相关的事情
redis.timeBetweenEvictionRunsMillis=30000
#当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
redis.timeout=2000
#默认对象过期时间(秒)
redis.defaultExpiration=3600

10、log4j.properties(我这里的日志工具是log4j,自己封装了一下,通过aop切面处理,进行全局的日志拦截以及打印)

log4j.rootLogger=DEBUG,INFO,ERROR

##控制台日志
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n


#info log
log4j.appender.INFO=org.apache.log4j.RollingFileAppender
log4j.appender.INFO.File=logs/INFOLOG.log
log4j.appender.INFO.Threshold=INFO
log4j.appender.INFO.MaxFileSize=10MB
log4j.appender.INFO.layout=org.apache.log4j.PatternLayout
log4j.appender.INFO.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n 
log4j.appender.INFO.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.INFO.filter.infoFilter.LevelMin=INFO
log4j.appender.INFO.filter.infoFilter.LevelMax=INFO


#debug log
log4j.appender.ERROR=org.apache.log4j.RollingFileAppender
log4j.appender.ERROR.File=logs/ERRORLOG.log
log4j.appender.ERROR.Threshold=ERROR
log4j.appender.ERROR.MaxFileSize=10MB
log4j.appender.ERROR.MaxBackupIndex=3
log4j.appender.ERROR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n
log4j.appender.ERROR.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.ERROR.filter.infoFilter.LevelMin=ERROR
log4j.appender.ERROR.filter.infoFilter.LevelMax=ERROR


#debug sql log
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.com.pro.mapper = DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

 11、sysconfig.properties(系统参数配置文件)

headico=com.mysql.jdbc.Driver

 12、spring mvc 的时间处理(spirng mvc  在接收时间参数的时候,都会默认为字符串参数,在未处理的情况下,请求借口会报400的错误,参数错误)

          处理方式:(自定义时间类,进行时间转换)

         (1)、spring-mvc.xml

                

    <!-- 自定义日期转换类 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <!--日期输入参数转换器 -->
                <bean class="com.spj.common.DateConverter"/>
            </set>
        </property>
    </bean>

  

         (2)、DateConverter.java

package com.spj.common;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.util.Date;

/**
 * Created by ben on 2017-03-07 0007.
 */
public class DateConverter implements Converter<String, Date> {

    private static final String[] patterns = {"yyyy-MM-dd HH:mm:ss" ,"yyyy-MM-dd HH:mm","yyyy-MM-dd","HH:mm"};

    @Override
    public Date convert(String s) {
        if (s == null || s.trim().equals("")){
            return null;
        }
        try {
            return DateUtils.parseDate(s, patterns);
        } catch (ParseException e) {
            throw new IllegalArgumentException("字符串转日期失败", e);
        }
    }
}

13、mybatis puls 和 jpa 的事物控制

          

         (1)问题:两个框架的底层是不一样,mybatis puls 是ibatis 框架, jpa 是hibernate框架,在事物控制上都有自己的一套方式,所以在框架集成的时候,必须得把两个框架的事物分开管理(mybatis plus 事物控制在dao层,jpa事物控制在jpa层,我们在service里面调用mybaits puls  或者jpa 进行数据逻辑处理的话就不会让两个框架对一个进行事物管理了)

           数据连接池:

 

<!-- 配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
        <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/>
        <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>
        <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
    </bean>

 

              mybatis puls 事物管理配置

                     

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="configuration" ref="configuration"/>
        <property name="globalConfig" ref="globalConfig"/>
        <!-- 扫描model包 使用别名 -->
        <property name="typeAliasesPackage" value="com.spj.model"/>
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <value>
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <bean id="configuration" class="com.baomidou.mybatisplus.core.MybatisConfiguration">
        <!-- 是否开启自动驼峰命名映射 -->
        <property name="mapUnderscoreToCamelCase" value="true"></property>
    </bean>
    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <!-- 是否打印mybatis的banner -->
        <property name="banner" value="true"></property>
        <property name="dbConfig" ref="dbConfig"/>
    </bean>
    <bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
        <!-- 全局默认主键类型 -->
        <property name="idType" value="NONE"></property>
    </bean>
<!-- mybatis puls配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.spj.dao"/>
    </bean>

    <!-- mybatis配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- mybatis注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

<!-- mybatis拦截器方式配置事物 -->
    <tx:advice id="mybatisTransactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>
            <tx:method name="append*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="repair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delAndRepair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="find*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="load*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="search*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="datagrid*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>

<!-- mybatis把事务控制在dao层 -->
    <aop:config>
        <aop:pointcut id="mybatisPointcut" expression="execution(* com.spj.dao.*.*(..))"/>
        <aop:advisor pointcut-ref="mybatisPointcut" advice-ref="mybatisTransactionAdvice"/>
    </aop:config>

  

              jpa 事物管理配置

<!-- jpa数据连接管理工厂 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="FastDFSPersistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.spj.model" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!-- 是否自动建表 -->
                <property name="generateDdl" value="false"/>
                <!-- 是否展示sql -->
                <property name="showSql" value="true"/>
                <!-- 必要的数据库库使用的详细信息 -->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                <!-- mysql,自行选择 -->
                <property name="database" value="MYSQL"/>
            </bean>
        </property>
        <!-- 指定JPA属性;如Hibernate中指定是否显示SQL的是否显示、方言等 -->
        <property name="jpaProperties">
            <props>
                <!--启用驼峰命名 -->
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
        </property>
    </bean>

<!-- jpa配置启用扫描并自动创建代理的功能  factory-class="com.monk.base.jpa.PeakJpaRepositoryFactory"自己定义的bean注解方式,可以不写,直接注解所有包下的 -->
    <jpa:repositories base-package="com.spj.jpa" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>

    <!-- 配置JPA的注解 -->
    <bean id="transactionJpaManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>
    <!-- jpa配置基于注解的事务 -->
    <tx:annotation-driven transaction-manager="transactionJpaManager" proxy-target-class="true"/>

<!-- jpa拦截器方式配置事物 -->
    <tx:advice id="jpaTransactionAdvice" transaction-manager="transactionJpaManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>
            <tx:method name="append*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="repair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delAndRepair" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="find*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="load*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="search*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
            <tx:method name="datagrid*" propagation="SUPPORTS" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>

    <!-- jpa把事务控制在jpa层 -->
    <aop:config>
        <aop:pointcut id="jpaPointcut" expression="execution(* com.spj.jpa.*.*(..))"/>
        <aop:advisor pointcut-ref="jpaPointcut" advice-ref="jpaTransactionAdvice"/>
    </aop:config>

 

 14、自定义封装log日志(实现按包,按类,按时间打印日志)

      LogInfoTools.java

package com.spj.common.log;

import com.spj.utils.MyDateUtil;
import org.apache.log4j.*;
import org.apache.log4j.varia.LevelRangeFilter;

import java.util.Date;

/**
 * Created by Miracle on 2018/11/3.
 */
public class LogInfoTools {

    public static Logger getLoggerInfo(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append(MyDateUtil.simpleDateString(new Date(),"yyyy-MM-dd"));
        logFilePath.append("/");
        logFilePath.append(clazz.getSimpleName());
        logFilePath.append("/");
        logFilePath.append(clazz.getCanonicalName());
        logFilePath.append("--INFO.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());

            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.INFO);
            f.setLevelMax(Level.INFO);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.INFO);
        return logger;
    }
    public static Logger getLoggerDebug(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append(MyDateUtil.simpleDateString(new Date(),"yyyy-MM-dd"));
        logFilePath.append("/");
        logFilePath.append(clazz.getSimpleName());
        logFilePath.append("/");
        logFilePath.append(clazz.getCanonicalName());
        logFilePath.append("--ALL.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ALL);
            f.setLevelMax(Level.OFF);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.DEBUG);
        return logger;
    }
    public static Logger getLoggerError(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append(MyDateUtil.simpleDateString(new Date(),"yyyy-MM-dd"));
        logFilePath.append("/");
        logFilePath.append(clazz.getSimpleName());
        logFilePath.append("/");
        logFilePath.append(clazz.getCanonicalName());
        logFilePath.append("--ERROR.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ERROR);
            f.setLevelMax(Level.ERROR);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.ERROR);
        return logger;
    }
    public static Logger getLoggerTime(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append("TIME.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ERROR);
            f.setLevelMax(Level.ERROR);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.ERROR);
        return logger;
    }

    /**
     *
     * @param clazz 类加载器
     * @param paht 日志储存路径  例如:info/test
     * @param name 日志名字 Test
     * @return logger日志对象
     * 返回案例存储位置:logs/info/test/Test.log
     */
    public static Logger getLoggerObjct(Class clazz,String paht,String name){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append(MyDateUtil.simpleDateString(new Date(),"yyyy-MM-dd"));
        logFilePath.append("/");
        logFilePath.append(paht);
        logFilePath.append("/");
        logFilePath.append(name);
        logFilePath.append(".log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.INFO);
            f.setLevelMax(Level.INFO);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.INFO);
        return logger;
    }


    public static Logger getLoggerFtlError(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append("ftl-error.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ERROR);
            f.setLevelMax(Level.ERROR);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.ERROR);
        return logger;
    }


    public static Logger getLoggerAspectError(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append("aspect-error.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ERROR);
            f.setLevelMax(Level.ERROR);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.ERROR);
        return logger;
    }

}

      MyLogger.java

package com.spj.common.log;

import com.spj.utils.MyDateUtil;
import org.apache.log4j.*;
import org.apache.log4j.varia.LevelRangeFilter;

import java.util.Date;

/**
 * Created by Miracle on 2018/11/3.
 */
public class LogInfoTools {

    public static Logger getLoggerInfo(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append(MyDateUtil.simpleDateString(new Date(),"yyyy-MM-dd"));
        logFilePath.append("/");
        logFilePath.append(clazz.getSimpleName());
        logFilePath.append("/");
        logFilePath.append(clazz.getCanonicalName());
        logFilePath.append("--INFO.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());

            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.INFO);
            f.setLevelMax(Level.INFO);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.INFO);
        return logger;
    }
    public static Logger getLoggerDebug(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append(MyDateUtil.simpleDateString(new Date(),"yyyy-MM-dd"));
        logFilePath.append("/");
        logFilePath.append(clazz.getSimpleName());
        logFilePath.append("/");
        logFilePath.append(clazz.getCanonicalName());
        logFilePath.append("--ALL.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ALL);
            f.setLevelMax(Level.OFF);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.DEBUG);
        return logger;
    }
    public static Logger getLoggerError(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append(MyDateUtil.simpleDateString(new Date(),"yyyy-MM-dd"));
        logFilePath.append("/");
        logFilePath.append(clazz.getSimpleName());
        logFilePath.append("/");
        logFilePath.append(clazz.getCanonicalName());
        logFilePath.append("--ERROR.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ERROR);
            f.setLevelMax(Level.ERROR);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.ERROR);
        return logger;
    }
    public static Logger getLoggerTime(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append("TIME.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ERROR);
            f.setLevelMax(Level.ERROR);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.ERROR);
        return logger;
    }

    /**
     *
     * @param clazz 类加载器
     * @param paht 日志储存路径  例如:info/test
     * @param name 日志名字 Test
     * @return logger日志对象
     * 返回案例存储位置:logs/info/test/Test.log
     */
    public static Logger getLoggerObjct(Class clazz,String paht,String name){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append(MyDateUtil.simpleDateString(new Date(),"yyyy-MM-dd"));
        logFilePath.append("/");
        logFilePath.append(paht);
        logFilePath.append("/");
        logFilePath.append(name);
        logFilePath.append(".log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.INFO);
            f.setLevelMax(Level.INFO);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.INFO);
        return logger;
    }


    public static Logger getLoggerFtlError(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append("ftl-error.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ERROR);
            f.setLevelMax(Level.ERROR);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.ERROR);
        return logger;
    }


    public static Logger getLoggerAspectError(Class clazz){
        StringBuffer logFilePath=new StringBuffer(System.getProperty("user.dir")+"/logs/");
        logFilePath.append("aspect-error.log");
        Logger logger = LogManager.getLogger(clazz);
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c.%M(%L) - %m%n");
        try{
            Appender appender = new FileAppender(layout, logFilePath.toString());
            LevelRangeFilter f = new LevelRangeFilter();
            f.setLevelMin(Level.ERROR);
            f.setLevelMax(Level.ERROR);
            appender.addFilter(f);
            logger.addAppender(appender);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.setLevel(Level.ERROR);
        return logger;
    }

}

      log使用

package com.spj.controller.admin;

import com.spj.common.log.MyLogger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Owner on 2018/11/30.
 */
@Controller
@RequestMapping("admin")
public class AdminTestController {


    MyLogger myLogger=new MyLogger(this.getClass());

    @RequestMapping(value = "/loginInfo/admin")
    public String admin(Model model, HttpServletRequest request) throws Exception {
        myLogger.info("111111111111111111");
        myLogger.debug("123456");
        myLogger.error("123456");
        return "admin/test";
    }
}

15、aop切面配置全局日志拦截打印

      spring-mvc.xml

    <!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!-- Log切面拦截处理类-->
    <bean id="aspectService" class="com.spj.config.LogAspectService"></bean>
    <!-- AOP配置 -->
    <aop:config>
        <!-- 声明一个切面,并注入切面Bean,相当于@Aspect -->
        <aop:aspect id="simpleAspect" ref="aspectService">
            <!-- 配置一个切入点,相当于@Pointcut -->
            <aop:pointcut expression="execution(* com.spj..*.*(..)))" id="simplePointcut"/>
            <aop:after-throwing pointcut-ref="simplePointcut" method="afterThrow" throwing="ex"/>
        </aop:aspect>
    </aop:config>

 

      LogAspectService.java

package com.spj.config;

import com.spj.common.log.MyLogger;
import org.aspectj.lang.JoinPoint;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2019/10/24 0024.
 */
public class LogAspectService {


    MyLogger myLogger=new MyLogger(this.getClass());

    //配置抛出异常后通知,使用在方法aspect()上注册的切入点
    public void afterThrow(JoinPoint joinPoint, Exception ex){
        //详细错误信息
        String errorMsg = "";
        StackTraceElement[] trace = ex.getStackTrace();
        for (StackTraceElement s : trace) {
            errorMsg += "\tat " + s + "\r\n";
        }
        String cla=joinPoint.getTarget().getClass().getName();//action
        String method=joinPoint.getSignature().getName();//method
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuffer sb=new StringBuffer();
        sb.append("\r\n-----------"+sdf.format(new Date())+"------------\r\n");
        sb.append("接口方法:["+cla+"."+method+"]\r\n");
        sb.append("详细错误信息:"+ex+"\r\n");
        sb.append(errorMsg+"\r\n");
        myLogger.errorAspect(sb.toString());
    }



}

 16、ftl模板错误日志全局拦截记录(在实际发布的工程,ftl模板的错误也是可以打印保存出来的,我这个全局拦截只是把全局日志集中打印到一个文件里面,方便阅读,且处理后,页面可以正常显示,不会崩溃)

      MyFtlTemplateExceptionHandler.java

package com.spj.config;

import com.spj.common.log.MyLogger;
import freemarker.core.Environment;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.IOException;
import java.io.Writer;

/**
 * Created by Administrator on 2019/10/24 0024.
 */
public class MyFtlTemplateExceptionHandler implements TemplateExceptionHandler {

    MyLogger myLogger=new MyLogger(this.getClass());

    @Override
    public void handleTemplateException(TemplateException te, Environment environment, Writer writer) throws TemplateException {
        try {
            myLogger.errorFtl(te.getMessage());
            writer.write("[ERROR: " + te.getMessage() + "]");
        } catch (IOException e) {
            myLogger.error(e);
            throw new TemplateException("Failed to print error message. Cause: " + e, environment);
        }
    }
}

 17、SwaggerConfig2集成(https://doc.xiaominfo.com/guide/#简介)

      SwaggerConfig.java

package com.spj.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Function;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by Administrator on 2019/10/9 0009.
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
    //    http://localhost:8080/doc.html
    private static final String splitor = ";";
    @Bean
    public Docket createRestApi() {

        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.any())
                //扫描的包
                .apis(basePackage("com.spj.controller.mobile"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API文档")
                .termsOfServiceUrl("HTTP对外开放接口")
                .description("接口详情")
                .version("1.1.1")
                .build();
    }


    //添加token
    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "Authorization", "header"));
    }

    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build()
        );
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference("Authorization", authorizationScopes));
    }


    /**
     * 重写basePackage方法,使能够实现多包访问,复制贴上去
     * @author  teavamc
     * @date 2019/1/26
     * @param basePackage
     * @return com.google.common.base.Predicate<springfox.documentation.RequestHandler>
     */
    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }

    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage)     {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(splitor)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }

}

      集成效果

spring+spring mvc+mybatis plus+jpa+freeMarker+redis+swagger2集成

 

 

 18、使用

  Demo.java(实体类,在数据库没有这个属性的时候,需要把mybatis puls  和 jpa 的实体属性映射去除掉,否则会报错,因为这两个框架目前使用的是配置是自动java驼峰属性改mysql 的下划线字段)

package com.spj.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.spj.utils.PagingParameter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by Administrator on 2019/10/4 0004.
 */
@Entity
@TableName(value = "demo")
@Table(name = "demo")
@ApiModel(value = "用户信息")
public class Demo extends PagingParameter implements Serializable {

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @TableId(value = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @ApiModelProperty(value = "用户id")
    private Integer id;


    @ApiModelProperty(value = "用户名")
    private String userName;

    @ApiModelProperty(value = "用户密码")
    private String passWord;

    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    //mybatis pusl 忽略字段映射
    @TableField(exist = false)
    //jipa 忽略字段映射
    @Transient
    //swagger2 字段注释
    @ApiModelProperty(value = "创建时间")
    private int index;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

  IDemoJpaService.java

package com.spj.jpa;

import com.spj.model.Demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2019/10/4 0004.
 */
@Repository
public interface IDemoJpaService extends JpaRepository<Demo,Integer>, JpaSpecificationExecutor<Demo>, Serializable {

    /**
     * 根据用户名查询
     * @param userName
     * @return
     */
    List<Demo> findByUserName(String userName);

    /**
     * 根据用户名密码查询
     * @param userName
     * @param password
     * @return
     */
    List<Demo> findByUserNameAndPassWord(String userName,String password);



}

  IDemoDao.java

package com.spj.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.spj.model.Demo;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * Created by Administrator on 2019/10/4 0004.
 */
public interface IDemoDao extends BaseMapper<Demo> {


    Demo selectDemoById(@Param("id") Integer id);

    Demo selectDemoById2(@Param("id") Integer id);


    List<Demo> selectDemoPageInfo(@Param("demo") Demo demo);


    int insertDemo(@Param("demo") Demo demo);


}

  IDemoDaoMapper.xml(mybatis的经典写法,也可以采用mybaits puls 的写法)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.spj.dao.IDemoDao">


    <resultMap id="BaseResultMap" type="com.spj.model.Demo">
        <id column="id" property="id"/>
        <result column="user_name" property="userName"/>
        <result column="pass_word" property="passWord"/>
        <result column="create_time" property="createTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,
        user_name,
        pass_word,
        create_time
    </sql>
    <insert id="insertDemo" useGeneratedKeys="true" keyProperty="demo.id" parameterType="com.spj.model.Demo">
        INSERT INTO demo (
        user_name,
        pass_word,
        create_time
        )
        VALUES
        (
        #{demo.user_name},
        #{demo.pass_word},
        #{demo.create_time}
        )
    </insert>
    <select id="selectDemoById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM demo WHERE  id=#{id}
    </select>
    <select id="selectDemoPageInfo" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM demo WHERE 1=1
    </select>
    <select id="selectDemoById2" resultType="com.spj.model.Demo">
        SELECT <include refid="Base_Column_List"></include> FROM demo WHERE  id=#{id}
    </select>


</mapper>

  IBaseService.java(基础服务类,规范下级service的行为)

package com.spj.service;

/**
 * Created by Administrator on 2019/10/4 0004.
 */
public interface IBaseService<T,D> {

    /**
     * 获取jpa持久化对象
     * @return
     */
    public T getJpa();

    /**
     * 获取mybatis持久化对象
     * @return
     */
    public D getDao();

}

    

  IDemoService.java(demo 服务类)

package com.spj.service;

import com.spj.dao.IDemoDao;
import com.spj.model.Demo;
import com.spj.jpa.IDemoJpaService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;

/**
 * Created by Administrator on 2019/10/4 0004.
 */
public interface IDemoService extends IBaseService<IDemoJpaService,IDemoDao>{

    Demo selectDemoById(@Param("id") Integer id);

    PageInfo<Demo> selectDemoPageInfo(@Param("demo") Demo demo);

    int insertDemo(@Param("demo") Demo demo);

}

  DemoService.java

package com.spj.service.impl;

import com.spj.dao.IDemoDao;
import com.spj.model.Demo;
import com.spj.service.IDemoService;
import com.spj.jpa.IDemoJpaService;
import com.spj.utils.PagingParameter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/10/4 0004.
 */
@Service
public class DemoService implements IDemoService {

    @Autowired
    IDemoJpaService iDemoJpaService;
    @Autowired
    IDemoDao iDemoDao;

    @Override
    public IDemoJpaService getJpa() {
        return iDemoJpaService;
    }

    @Override
    public IDemoDao getDao() {
        return iDemoDao;
    }


    @Override
    public Demo selectDemoById(Integer id) {
        if(id!=null){
            return iDemoDao.selectDemoById(id);
        }
        return null;
    }

    @Override
    public PageInfo<Demo> selectDemoPageInfo(Demo demo) {
        PagingParameter.setDefault(demo);
        //查询第一页,每页10条
        PageHelper.startPage(demo.getPageNum(), demo.getPageSize());
        List<Demo> adminUsers = iDemoDao.selectDemoPageInfo(demo);
        return new PageInfo<>(adminUsers);
    }

    @Override
    public int insertDemo(Demo demo) {
        demo.setCreateTime(new Date());
        return iDemoDao.insertDemo(demo);
    }
}

    MobileDemoController.java

package com.spj.controller.mobile;

/**
 * Created by Administrator on 2019/10/10 0010.
 */

import com.github.pagehelper.PageInfo;
import com.spj.model.Demo;
import com.spj.service.IDemoService;
import com.spj.utils.ResponseResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("mobile")
@Api(value = "api接口", tags="用户相关接口")
public class MobileDemoController {

    @Autowired
    IDemoService iDemoService;



    @PostMapping(value = "/getUserInfoAll")
    @ResponseBody
    @ApiOperation(value = "查询所有用户", notes = "暂无接口描述")
    public ResponseResult<List<Demo>> getUserInfoAll(Model model, HttpServletRequest request, String userId) throws Exception {
        List<Demo> demoList=iDemoService.getJpa().findAll();
        Demo dem=iDemoService.getJpa().findById(1).get();
        Demo demo=iDemoService.getDao().selectById(1);
        return ResponseResult.successResult(demoList);
    }


    @PostMapping(value = "/getUserInfoById")
    @ResponseBody
    @ApiOperation(value = "根据id查询用户", notes = "请求携带token")
    @ApiImplicitParam(name = "userId",value = "用户ID")
    public ResponseResult<Demo> getUserInfoById(Model model, HttpServletRequest request, Integer userId) throws Exception {
        Demo demoList=iDemoService.getJpa().findById(userId).get();
        return ResponseResult.successResult(demoList);
    }

    @PostMapping(value = "/getUserInfoByUserNameAndPassWord")
    @ResponseBody
    @ApiOperation(value = "根据用户名", notes = "暂无接口描述")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名(1==正常 2=非常 3=锁定)",required = true),
            @ApiImplicitParam(name = "passWord",value = "密码",required = true)
    })
    public ResponseResult<Demo> getUserInfoByUserNameAndPassWord(Model model, HttpServletRequest request, String userName,String passWord) throws Exception {
        List<Demo> demo=iDemoService.getJpa().findByUserNameAndPassWord(userName,passWord);
        return ResponseResult.successResult(demo.get(0));
    }



    @PostMapping(value = "/getUserInfoPageInfo")
    @ResponseBody
    @ApiOperation(value = "用户查询分页", notes = "暂无接口描述")
    public ResponseResult<PageInfo<Demo>> getUserInfoPageInfo(Model model, HttpServletRequest request, Demo demo) throws Exception {
        PageInfo<Demo> pageInfo=iDemoService.selectDemoPageInfo(demo);
        return ResponseResult.successResult(pageInfo);
    }


    @PostMapping(value = "/deleteDemoById")
    @ResponseBody
    @ApiOperation(value = "根据id删除demo", notes = "暂无接口描述")
    public ResponseResult<PageInfo<Demo>> getUserInfoPageInfo(Model model, HttpServletRequest request, Integer id) throws Exception {
        iDemoService.getJpa().deleteById(id);
        Demo demo=new Demo();
        demo.setCreateTime(new Date());
        demo.setUserName("123456");
        demo.setPassWord("123456");
        iDemoService.insertDemo(demo);
        return ResponseResult.successResult("删除成功");
    }


    @RequestMapping(value="/uploadFile",method = RequestMethod.POST,headers = "content-type=multipart/form-data")
    @ResponseBody
    @ApiOperation(value = "上传你文件", notes = "")
    @ApiImplicitParam(name = "fileName", value = "上传文件", required = true,dataType = "MultipartFile",allowMultiple = true)
    public ResponseResult upload(Model model, HttpServletRequest request,@RequestParam(value = "fileName",required = true) CommonsMultipartFile fileName) throws IOException {
        return ResponseResult.successResult("上传成功");
    }


}

19、总结

  整个框架搭建以及使用差不多说完了,上面的代码,在两个持久化框架之间的协调性,我觉得还有提升的空间,希望大家能给点建议,以及优化方案,我也会在深入研究一下底层,毕竟两个框架优缺点都是不一样的,注重点也不同,希望这个框架能能够给我们开发提供更加便捷的开发速度;

20、项目源码 

链接:https://pan.baidu.com/s/1lRPK2GP8z9Q26FY05Jz3rg
提取码:nxmj
复制这段内容后打开百度网盘手机App,操作更方便哦

  

  

 

 

 

 

           

 

 

 

上一篇:freemarker常见语法


下一篇:freemarker最简单的测试小demo