美文网首页
控制台打印sql语句

控制台打印sql语句

作者: 墨色尘埃 | 来源:发表于2018-11-26 11:10 被阅读37次

请教logger.info与System.out.print的区别?
一般的项目都很少用out,主要用logger打日志
logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
        </layout>
    </appender>


    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/log.txt</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>log/log-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
            <maxFileSize>5MB</maxFileSize>
            <maxHistory>7</maxHistory>
            <totalSizeCap>20GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>


    <root level="info">
        <appender-ref ref="console"/> <!--指向上面name="console"-->
        <appender-ref ref="file"/> <!--指向上面name="file"-->
    </root> 
</configuration>

mybatis的application.yml配置

mybatis:
  configuration:
    mapUnderscoreToCamelCase: true
    logImpl: org.apache.ibatis.logging.stdout.StdOutImpl   #打印sql语句的查询结果
    typeAliasesPackage: com.jsptpd.gayg.modules
  mapperLocations: classpath:com/jsptpd/gayg/**/*.xml

spring:
  resource:
    static-locations: file:/public/
  http:
    multipart:
      max-file-size: 50Mb
      max-request-size: 80Mb
  profiles:
#    active: dev
    active: prod


session:
  cookieName: Session100136
  headerName: Session100136

mybatis-plus的application.yml配置

mybatis-plus:
  mapper-locations: classpath:com/cicdi/invoice/mapping/**/*Mapper.xml
  typeAliasesPackage: com.cicdi.invoice.**.model
  global-config:
    id-type: 2
    db-column-underline: true #驼峰下划线转换
    logic-delete-value: 101003  #配置逻辑删除字段为1是删除
    logic-not-delete-value: 101001 #配置逻辑删除字段为101001是未删除
    sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector #逻辑删除配置
    meta-object-handler: com.cicdi.invoice.common.config.mybatis.MetaObjectHandlerConfig
  configuration:
    map-underscore-to-camel-case: true

spring:
  resource: 
    static-locations: file:/public/
  http:
    multipart:
      max-file-size: 50Mb
      max-request-size: 80Mb
  profiles:
    active: dev
#    active: prod


session:
  cookieName: Session100136
  headerName: Session100136

控制台打印sql语句:
① 带n个问号的sql语句
主要配置
logImpl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句的查询结果

image.png
② 不带问号的sql语句
主要配置
    @Bean
    @Profile({"dev", "test", "prod"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
image.png

③都显示
两个配置都加


微信图片_20181126104004.png

Time:0 ms - ID:
Execute SQL:
MybatisPlusConfig

package com.cicdi.invoice.common.config.mybatis;

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.text.SimpleDateFormat;

/**
 * MP配置类
 *
 * @author lw
 * @createTime 2018/8/24下午4:30
 */
@Configuration
@MapperScan("com.cicdi.invoice.**.dao")
public class MybatisPlusConfig {
    /*
     * 分页插件,自动识别数据库类型
     * 多租户,请参考官网【插件扩展】
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean
    public ObjectMapper mapper() {
        final ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);

        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));


        return objectMapper;
    }


    @Bean
    @Profile({"dev", "test", "prod"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }

}

相关文章

网友评论

      本文标题:控制台打印sql语句

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