• 搭建SpringMVC+MyBatis开发框架四


    在src/main下面新建一个resouces文件夹,我们继续配置一些资源

    1.新增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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd 
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
    
        <description>Spring公共配置</description>
        
        <!-- 开启定时任务 -->
        <task:annotation-driven/>
    
        <!-- MyBatis配置 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 显式指定Mapper文件位置 -->
            <property name="mapperLocations" value="classpath*:/mybatis/*Mapper.xml" />
            <!-- mybatis配置文件路径 -->
            <property name="configLocation" value="classpath:/mybatis/config.xml"/>    
        </bean>
        
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
           <constructor-arg index="0" ref="sqlSessionFactory" />
           <!-- 这个执行器会批量执行更新语句, 还有SIMPLE 和 REUSE -->
           <constructor-arg index="1" value="BATCH" />
        </bean>
    
        <!-- 扫描basePackage接口 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 映射器接口文件的包路径, -->
            <property name="basePackage" value="net.quickcodes.dao" />
        </bean>
    
        <!-- 使用annotation定义事务 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    
        <!-- 数据源配置, 使用Tomcat JDBC连接池 -->
        <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
            <!-- Connection Info -->
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
    
            <!-- Connection Pooling Info -->
            <property name="maxActive" value="${jdbc.pool.maxActive}" />
            <property name="maxIdle" value="${jdbc.pool.maxIdle}" />
            <property name="minIdle" value="0" />
            <property name="defaultAutoCommit" value="false" />
        </bean>
    
        <!-- production环境 -->
        <beans profile="production">
            <context:property-placeholder ignore-unresolvable="true" file-encoding="utf-8" 
            location="classpath:config.properties,classpath:jdbc.properties" />
        </beans>
    
    </beans>

    2.新增jdbc.properties,这个文件用来配置Mysql数据库的jdbc连接信息:

    在resouces文件夹上有鼠标右键选择“New”>"Other":

    选择"Properties File",点击"Next"按钮:

    文件名输入jdbc.properties:

    输入如下配置内容,这是连接MySQL数据库的url,用户名和密码,请根据你自己的实际情况配置值:

    jdbc.driver=com.mysql.jdbc.Driver
    
    jdbc.url=jdbc:mysql://127.0.0.1:3306/lesson-1?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true
    jdbc.username=root
    jdbc.password=123456
    
    #connection pool settings
    # maxIdle是最大的空闲连接数,这里取值为20,表示即使没有数据库连接时依然可以保持20个空闲的连接,它们不会被清除,随时处于待命状态
    jdbc.pool.maxIdle=20
    # maxActive是最大激活连接数,这里取值为190,表示同时最多有190个数据库连接
    jdbc.pool.maxActive=190

    3.同样方法新建一个config.properties文件,这个配置文件暂时不配置内容,以后有用途。

    4.新增一个logback.xml配置文件:

    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration>
        <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
       <!-- 时间滚动输出 level为 DEBUG 日志 -->
        <appender name="file-debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>DEBUG</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY </onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>/export/data/logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern>
                <MaxHistory>30</MaxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
     
        <!-- 时间滚动输出 level为 INFO 日志 -->
        <appender name="file-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY </onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>/export/data/logs/info.%d{yyyy-MM-dd}.log</FileNamePattern>
                <MaxHistory>30</MaxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
        
        <!-- 时间滚动输出 level为 INFO 日志 -->
        <appender name="file-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY </onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>/export/data/logs/error.%d{yyyy-MM-dd}.log</FileNamePattern>
                <MaxHistory>30</MaxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
        
        <!-- 很多人使用Mybatis的时候,控制台不能输出SQL语句,造成调试困难。只需要让DAO层的日志级别调整为DEBUG就可以了 -->
        <logger name="net.quickcodes.dao" level="DEBUG" />
    
        <!-- 指定项目可输出的最低级别日志 -->
        <root level="INFO">
            <appender-ref ref="console" />
            <appender-ref ref="file-debug" />
            <appender-ref ref="file-info" />
            <appender-ref ref="file-error" />
        </root>
    </configuration>

    5.在resources文件夹下建立一个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="cacheEnabled" value="true" />
        </settings>
    
    </configuration>
  • 相关阅读:
    【底层原理】四位计算机的原理及其实现
    ctags的如何生成tags文件
    nginx-1.12.0版本(编译安装)-自定义安装路径
    使用LVS实现负载均衡原理及安装配置详解
    【系统架构】亿级Web系统搭建(1):Web负载均衡
    9个Linux系统常用监控命令
    NetStat命令详解
    SQLSERVER CXPACKET 等待
    什么是PAGELATCH和PAGEIOLATCH
    CentOS环境变量配置并生效
  • 原文地址:https://www.cnblogs.com/quickcodes/p/da-jianSpringMVCMyBatis-kai-fa-kuang-jia-si.html
Copyright © 2020-2023  润新知