SpringBoot 基础配置Mybatis、Mybatis-plus、 log4j.xml、xml映射文件

配置记录:

SpringBoot

1: application.yml


server:
  port: 9090

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=GMT%2b8&useServerPrepStmts=true&useSSL=false
    username: root
    password: aini616yisi

    
    
    
mybatis:
  mapper-locations: classpath:mappers/*.xml   #mapperXML文件地址
  type-aliases-package: com.yyl.entity  #指定实体目录
  configuration:
      map-underscore-to-camel-case: true  # 开启驼峰命名


# MP开启Sql日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
    
# 文件上传路径
files:
  upload:
    path: E:/SpringBoot/files/
    
    
    
// 启动类注解
@EnableTransactionManagement : 开启事务



# 2、Mybatis-Plus 配置类


@Configuration
@MapperScan("com.yyl.mapper")
public class MybatisPlusConfig {

    // 分页拦截器
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();  //注册壳子
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));   // 注册拦截器
        return interceptor;  // 抛出
    }

}



Mybatis-config.xml

一:database.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf-8
jdbc.username=root
jdbc.password=aini616yisi

二: LOG4J

1、导包


<!--日志依赖-->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>


2、log4j.properties

#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file

#控制台输出的相关设置
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#文件输出的相关设置
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/mylog.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n

#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG


三:Mybatis-config.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--引入 database.properties 文件-->
    <properties resource="database.properties"/>

    <!--实体别名-->
    <typeAliases>
        <package name="com.yyl.springboot.entity"/>
    </typeAliases>

    <settings>
        <!--开启驼峰命名-->
        <setting name="mapUnderscoreToCamelCase" value="true" />

        <!--标准日志配置-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>


        <!--Log4J日志-->
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--mapper.xml映射文件,xml路径一般与mappper接口一致-->
    <mappers>
        <package name="com.yyl.springboot.mapper"/>
    </mappers>
</configuration>


四、Mapper.xml 映射文件


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yyl.dao.UserDao">

 <resultMap id="pageUser" type="com.yyl.dao.UserDao">
 		// 字段名                  // 属性名
        <result column="id" property="id" />
        <result column="username" property="username" />
        <result column="nickname" property="nickname" />
 </resultMap>


// resultMap="pageUser" : 调用字段属性映射(识别ID)
  <select id="login" resultType="User" resultMap="pageUser">
    select * from tb_user where tel = #{tel} and userpass = #{userpass}
  </select>
</mapper>


五、 log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Encoding" value="UTF-8" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" />
        </layout>
    </appender>
    <logger name="java.sql">
        <level value="error" />
    </logger>
    <logger name="org.apache.ibatis">
        <level value="info" />
    </logger>
    <root>
        <level value="debug" />
        <appender-ref ref="STDOUT" />
    </root>
</log4j:configuration>

你可能感兴趣的:(Java,mybatis,spring,boot,log4j,java,xml)