zoukankan      html  css  js  c++  java
  • Mybatis的应用1 Mybatis和logback的应用配置

    首先新建一个module, 然后,在pom文件里面添加一些引用的项。

    pom.xml

    <?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">
    <parent>
        <artifactId>JavaInterfaceTest</artifactId>
        <groupId>com.peixm.code</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    
    <artifactId>Chapter11</artifactId>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
    
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.38</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
    
    </dependencies>
    </project>

    在resourses下面新建一个application.yml文件

    application.yml

    server:
      port: 8888   # 配置端口号
    
    
    logging:
      path: logs   #在同级目录下生成logs文件夹
      file: mylog.log  # 生成文件
    
    spring:   #spring里面配置mysql地址之类的
      application:
        name: myTest   #仅仅是application名字
      datasource:  #mysql的配置
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/course    #本地的地址➕端口号➕数据库名称
        username: root   #数据库用户名和密码
        password: 123456Aa
    
    mybatis:
      type-alises-package: com.course.model  #javabean,做映射的
      mapper-locations:
        - mapper/*    #文件夹mapper  加载下面的所有xml文件

    logback.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    
    <configuration>
    
        <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
        <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n"/>
        <property name="LOG_PATH" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}"/>
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_PATH}/${LOG_FILE}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
            </rollingPolicy>
            <encoder charset="UTF-8">
                <pattern>${FILE_LOG_PATTERN}</pattern>
            </encoder>
        </appender>
    
    
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${FILE_LOG_PATTERN}</pattern>
            </encoder>
        </appender>
    
    
        <appender name="CRAWLER_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_PATH}/event.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${LOG_PATH}/event.%d{yyyy-MM-dd}.log</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>%msg%n</pattern>
            </encoder>
        </appender>
    
    
        <logger name="com.business.intelligence.util.CrawlerLogger" level="INFO" additivity="false">
            <appender-ref ref="CRAWLER_LOG"/>
        </logger>
    
        <root level="INFO">   <!--记录日志的级别-->
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="FILE"/>
        </root>
    
    
    </configuration>

    mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!--
           Copyright 2015-2016 the original author or authors.
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
              http://www.apache.org/licenses/LICENSE-2.0
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
    -->
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">  <!--遵照那个模版-->
    
    <configuration>
        <typeAliases>
            <package name="com.course.model"/>   <!--映射到那个包里面-->
        </typeAliases>
        <mappers>
            <mapper resource="mapper/mysql.xml"/>   <!--mapper可以是多个   一般都是mapper/***.xml-->
        </mappers>
    </configuration>

    以下是目录结构

  • 相关阅读:
    数学趣题——数字的全排列
    数学趣题——谁在说谎
    数学趣题——移数字游戏
    数学趣题——魔幻方阵
    数学趣题——亲密数
    数学趣题——数字翻译器
    数学趣题——求圆周率的近似值
    数学趣题——完全数
    Linux:CentOS 7 连接ssh方法
    Linux:CentOS 7 服务器之间ssh无密码登录、传输文件
  • 原文地址:https://www.cnblogs.com/peiminer/p/9708403.html
Copyright © 2011-2022 走看看