美文网首页
SSM整合的各类文件

SSM整合的各类文件

作者: IT楠老师 | 来源:发表于2020-07-12 13:56 被阅读0次

    九、全部的配置文件:

    1、pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>ssm</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <!-- 测试相关 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <!-- springmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
            <!-- servlet -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.2</version>
                <scope>provided</scope>
            </dependency>
    
            <!--文件上传-->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.4</version>
            </dependency>
            <!-- fastjson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.68</version>
            </dependency>
    
            <!-- mybatis 相关 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.2</version>
            </dependency>
            <!-- 数据库连接驱动 相关 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
    
            <!-- 提供了对JDBC操作的完整封装 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.1.10.RELEASE</version>
            </dependency>
            <!-- 织入 相关 -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.4</version>
            </dependency>
            <!-- spring,mybatis整合包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.2</version>
            </dependency>
            <!-- 集成德鲁伊使用 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.18</version>
            </dependency>
    
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.18</version>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.30</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>${java.version}</source> <!-- 源代码使用的JDK版本 -->
                        <target>${java.version}</target> <!-- 需要生成的目标class文件的编译版本 -->
                        <encoding>${project.build.sourceEncoding}</encoding><!-- 字符集编码 -->
                    </configuration>
                </plugin>
            </plugins>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
        </build>
    </project>
    

    2、mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <settings>
            <setting name="lazyLoadingEnabled" value="true"/>
            <setting name="aggressiveLazyLoading" value="false"/>
            <!-- 下划线转驼峰式 -->
            <setting name="cacheEnabled" value="true"/>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <setting name="logPrefix" value="sql."/>
        </settings>
    
    
    </configuration>
    

    3、springmvc-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- 让Spring MVC不处理静态资源 -->
        <mvc:default-servlet-handler />
        <!--  让springmvc自带的注解生效  -->
        <mvc:annotation-driven >
            <mvc:message-converters>
                <bean id="fastjson" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
    
        <!--文件上传配置-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
            <property name="defaultEncoding" value="utf-8"/>
            <!-- 上传文件大小上限,单位为字节(10485760=10M) -->
            <property name="maxUploadSize" value="10485760"/>
            <property name="maxInMemorySize" value="40960"/>
        </bean>
    
        <!-- 处理映射器 -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
        <!-- 处理器适配器 -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        <!--视图解析器:DispatcherServlet给他的ModelAndView-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
            <!--前缀-->
            <property name="prefix" value="/WEB-INF/page/"/>
            <!--后缀-->
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
    

    4、db.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=true&useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=root
    
    filters=wall,stat
    maxActive=20
    initialSize=3
    maxWait=5000
    minIdle=3
    maxIdle=15
    timeBetweenEvictionRunsMillis=60000
    minEvictableIdleTimeMillis=300000
    validationQuery=SELECT 'x'
    testWhileIdle=true
    testOnBorrow=false
    testOnReturn=false
    maxOpenPreparedStatements=20
    removeAbandoned=true
    removeAbandonedTimeout=1800
    logAbandoned=true
    

    5、application.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!-- 加载外部的数据库信息 classpath:不叫会报错具体原因下边解释-->
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 加入springmvc的配置 -->
        <import resource="classpath:springmvc-servlet.xml"/>
    
        <context:component-scan base-package="com.xinzhi"/>
    
        <!-- Mapper 扫描器 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 扫描 cn.wmyskxz.mapper 包下的组件 -->
            <property name="basePackage" value="com.xinzhi.dao"/>
        </bean>
    
        <!--配置数据源:数据源有非常多,可以使用第三方的,也可使使用Spring的-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
    
            <property name = "filters" value = "${filters}" />
            <!-- 最大并发连接数 -->
            <property name = "maxActive" value = "${maxActive}" />
            <!-- 初始化连接数量 -->
            <property name = "initialSize" value = "${initialSize}" />
            <!-- 配置获取连接等待超时的时间 -->
            <property name = "maxWait" value = "${maxWait}" />
            <!-- 最小空闲连接数 -->
            <property name = "minIdle" value = "${minIdle}" />
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name = "timeBetweenEvictionRunsMillis" value ="${timeBetweenEvictionRunsMillis}" />
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name = "minEvictableIdleTimeMillis" value ="${minEvictableIdleTimeMillis}" />
            <!--        <property name = "validationQuery" value = "${validationQuery}" />     -->
            <property name = "testWhileIdle" value = "${testWhileIdle}" />
            <property name = "testOnBorrow" value = "${testOnBorrow}" />
            <property name = "testOnReturn" value = "${testOnReturn}" />
            <property name = "maxOpenPreparedStatements" value ="${maxOpenPreparedStatements}" />
            <!-- 打开 removeAbandoned 功能 -->
            <property name = "removeAbandoned" value = "${removeAbandoned}" />
            <!-- 1800 秒,也就是 30 分钟 -->
            <property name = "removeAbandonedTimeout" value ="${removeAbandonedTimeout}" />
            <!-- 关闭 abanded 连接时输出错误日志 -->
            <property name = "logAbandoned" value = "${logAbandoned}" />
        </bean>
    
        <!--配置SqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!--关联Mybatis-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:mappers/*.xml"/>
        </bean>
    
        <!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <!--利用构造器注入-->
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
    
        <!--配置事务通知-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!--配置哪些方法使用什么样的事务,配置事务的传播特性-->
                <tx:method name="add*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="search*" propagation="REQUIRED"/>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
        <!--配置aop织入事务-->
        <aop:config>
            <aop:pointcut id="txPointcut" expression="execution(* com.xinzhi.service.impl.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>
    
    </beans>
    

    6、logback.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="60 seconds" debug="false">
        <!-- 定义参数常量 -->
        <!-- 日志级别 TRACE<DEBUG<INFO<WARN<ERROR -->
        <!-- logger.trace("msg") logger.debug... -->
        <property name="log.level" value="debug" />
        <property name="log.maxHistory" value="30" />
        <property name="log.filePath" value="D:/log" />
        <property name="log.pattern"
                  value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" />
        <!-- 控制台输出设置 -->
        <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${log.pattern}</pattern>
            </encoder>
        </appender>
        <!-- DEBUG级别文件记录 -->
        <appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${log.filePath}/debug.log</file>
            <!-- 滚动日志文件类型,就是每天都会有一个日志文件 -->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gz
                </fileNamePattern>
                <!-- 文件最大保存历史数量 -->
                <maxHistory>${log.maxHistory}</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>${log.pattern}</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>DEBUG</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
        <!-- INFO -->
        <appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${log.filePath}/info.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz
                </fileNamePattern>
                <!-- 文件最大保存历史数量 -->
                <maxHistory>${log.maxHistory}</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>${log.pattern}</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
    
        <!-- com.xinzhi开头的日志对应形式 -->
        <logger name="com.xinzhi" level="${log.level}" additivity="true">
            <appender-ref ref="debugAppender"/>
            <appender-ref ref="infoAppender"/>
        </logger>
    
        <!-- <root> 是必选节点,用来指定最基础的日志输出级别,只有一个level属性 -->
        <root level="info">
            <appender-ref ref="consoleAppender"/>
        </root>
    
        <!-- 捕捉sql开头的日志 -->
        <appender name="MyBatis" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${log.filePath}/sql_log/mybatis-sql.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${log.filePath}/sql_log/mybatis-sql.log.%d{yyyy-MM-dd}</FileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>%thread|%d{yyyy-MM-dd HH:mm:ss.SSS}|%level|%logger{36}|%m%n</pattern>
            </encoder>
        </appender>
    
        <logger name="sql" level="DEBUG">
            <appender-ref ref="MyBatis"/>
        </logger>
    
    </configuration>
    

    相关文章

      网友评论

          本文标题:SSM整合的各类文件

          本文链接:https://www.haomeiwen.com/subject/oozgzhtx.html