前言:

本文内容:日志工程、Log4j讲解、Limit实现分页

推荐免费MyBatis基础教程视频:【狂神说Java】Mybatis最新完整教程IDEA版通俗易懂_哔哩哔哩_bilibili

日志工程

日志工厂

如果一个数据库操作出现了异常,我们日志帮助我们排错

日志工厂

loglmpl 指定MyBatis所用日志的具体实现,未指定时自动查找 SLF4J / LOG4J / LOG4J2 / JDK_LOGGING / COMMONS_LOGGING / STDOUT_LOGGING / NO_LOGGING 未设置
  • LOG4j(掌握)
  • STDOUT_LOGGING(掌握)

使用那个配置即可

STDOUT_LOGGING标准日志输出

配置

mybatis-config.xml

1
2
3
4
<!--    配置日志工厂-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

配置后运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1840976765.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6dbb137d]
==> Preparing: select * from user where id=?
==> Parameters: 1(Integer)
<== Columns: id, name, pwd
<== Row: 1, admin, 123
<== Total: 1
User{id=1, username='admin', password='123'}
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6dbb137d]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6dbb137d]
Returned connection 1840976765 to pool.

Process finished with exit code 0

Log4j讲解

注意Log4j被爆出存在漏洞,建议使用最新版本或者1.x版本

最后:实际开发已经弃用log4j了,这里仅作了解(时间2022/4/28)

LOG4j

Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件等

我们可以控制每一条日志的输出格式

通过定义每一条日志信息的级别,我们能够更加细致的控制日志的生成过程

通过配置文件灵活的进行配置,而不需要修改应用的代码。

先导包

父工程的pom.xml

1
2
3
4
5
6
7
<!--        log4j-->
<!-- 注意:log4j目前存在高危漏洞,不建议在正式项目中使用-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>

新建

resources下新建log4j.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#将等级为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/jokerdig.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

1
2
3
<settings>
<setting name="logImpl" value="LOG4j"/>
</settings>

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 1782580546.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6a400542]
[com.jokerdig.mapper.UserMapper.getUserById]-==> Preparing: select * from user where id=?
[com.jokerdig.mapper.UserMapper.getUserById]-==> Parameters: 1(Integer)
[com.jokerdig.mapper.UserMapper.getUserById]-<== Total: 1
User{id=1, username='admin', password='123'}
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6a400542]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6a400542]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 1782580546 to pool.

Process finished with exit code 0

简单使用

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.jokerdig.test;

import com.jokerdig.mapper.UserMapper;
import com.jokerdig.pojo.User;
import com.jokerdig.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

/**
* @author Joker大雄
* @data 2022/4/28 - 14:08
**/
public class Log4jTest {
static Logger logger =Logger.getLogger(MapperTest.class);
@Test
public void log4j(){
logger.info("info:进入了log4j");
logger.debug("debug:进入了log4j");
logger.error("error:进入了log4j");
}
}

运行结果

1
2
3
4
5
[com.jokerdig.test.MapperTest]-info:进入了log4j
[com.jokerdig.test.MapperTest]-debug:进入了log4j
[com.jokerdig.test.MapperTest]-error:进入了log4j

Process finished with exit code 0

Limit实现分页

为什么分页?

  • 减少数据处理量
  • 优化用户体验

使用Limit分页

语法

1
select * from user limit startIndex.pageSize;

使用myBatis分页

  1. 接口

    UserMapper.java

    1
    2
    // 分页
    List<User> getUserByList(Map<String,Integer> map);
  2. Mapper.xml

    UserMapper.xml

    1
    2
    3
    4
    <!--    分页查询-->
    <select id="getUserByList" parameterType="map" resultMap="userMap">
    select * from user limit #{startIndex},#{pageSize}
    </select>
  3. 测试

    MapperTest.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    // 分页
    @Test
    public void getUserByLimit(){
    SqlSession sqlSession = MyBatisUtil.getSqlSession();

    UserMapper mapper = sqlSession.getMapper(UserMapper.class);

    Map<String,Integer> userMap = new HashMap<String, Integer>();

    userMap.put("startIndex",0);
    userMap.put("pageSize",2);

    List<User> userByList = mapper.getUserByList(userMap);

    for (User user : userByList) {
    System.out.println(user);
    }
    sqlSession.close();
    }
  4. 运行结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    .......
    Opening JDBC Connection
    Created connection 1702940637.
    Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6580cfdd]
    ==> Preparing: select * from user limit ?,?
    ==> Parameters: 0(Integer), 2(Integer)
    <== Columns: id, name, pwd
    <== Row: 1, admin, 123
    <== Row: 2, abc, 123
    <== Total: 2
    User{id=1, username='admin', password='123'}
    User{id=2, username='abc', password='123'}
    Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6580cfdd]
    Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6580cfdd]
    Returned connection 1702940637 to pool.

    Process finished with exit code 0