Mybatis学习的第二天,内容有日志和分页。
1、日志
1、日志的设置
在mybatis核心配置文件中设置,在
Mybatis内置了多个日志,有:
- SLF4J
- LOG4J (常用)
- LOG4J2
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING (常用,标准日志)
- NO_LOGGING
例如STDOUT_LOGGING的配置则需在配置文件中加入:
1 | <!-- mybatis的设置--> |
效果如下:
2、LOG4J
log4j的解释:
- Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等 。
- 我们也可以控制每一条日志的输出格式 。
- 可以通过一个配置文件来灵活地进行配置
开始使用:
Maven添加依赖
1
2
3
4
5
6<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>mybatis设置log4j为日志
1
2
3<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>编写log4j的配置文件(放在resoures中,名为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
25
26
27
28
29
30
31
32
33
34
35
36
37
38### set log levels ###
INFO , console , debug , error =
### console ###
org.apache.log4j.ConsoleAppender =
System.out =
org.apache.log4j.PatternLayout =
%-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n =
### log file ###
org.apache.log4j.DailyRollingFileAppender =
./log/log4j.log =
true =
INFO =
org.apache.log4j.PatternLayout =
%-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n =
### exception ###
org.apache.log4j.DailyRollingFileAppender =
./log/log4j.log =
true =
ERROR =
org.apache.log4j.PatternLayout =
%-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n =
###需要声明,然后下方才可以使druid sql输出,否则会抛出log4j.error.key not found
org.apache.log4j.ConsoleAppender =
System.out =
org.apache.log4j.PatternLayout =
%d{ISO8601} %l %c%n%p: %m%n =
### druid sql ###
warn,stdout =
warn,stdout =
warn,stdout =
warn,stdout =
warn,stdout =java代码中使用log4j
1
2
3
4
5
6
7
public void TestLog4j(){
Logger logger = Logger.getLogger(UserMapperTest.class);
logger.info("info:进入TestLog4j");
logger.debug("debug:进入TestLog4j");
logger.error("error:进入TestLog4j");
}
2、分页
1、limit分页(SQL语句)
接口
1
2//通过分页获取用户信息
List<User> getUserByLimit(Map<String,Integer> map);xml的语句
1
2
3
4<!-- 获取分页-->
<select id="getUserByLimit" parameterType="map" resultType="User">
select * from user limit #{startIndex},#{pageSize}
</select>测试
1
2
3
4
5
6
7
8
9
10
11
12
13
public void getUserByLimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",0);
map.put("pageSize",3);
List<User> userByLimit = mapper.getUserByLimit(map);
for (User user : userByLimit) {
System.out.println(user);
}
sqlSession.close();
}
2、RowBounds实现(JAVA代码层面)
接口
1
2//rowbounds实现分页
List<User> getUserByRowBounds();xml文件
1
2
3
4<!-- rowbounds实现分页-->
<select id="getUserByRowBounds" resultType="User">
select * from user
</select>测试代码
1
2
3
4
5
6
7
8
9
10
public void getUserByRowBounds(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
RowBounds rowBounds = new RowBounds(1, 3);
List<User> userList = sqlSession.selectList("com.neversettle.dao.UserMapper.getUserByRowBounds", null, rowBounds);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}