Mybatis学习03

title: Mybatis学习03
date: 2020-01-19 13:03:20
tags:

Mybatis学习的第二天,内容有日志和分页。

<!--more-->

1、日志

1、日志的设置

在mybatis核心配置文件中设置,在<settings></settings>标签中使用<setting></setting>来配置。

Mybatis内置了多个日志,有:

  • SLF4J

  • LOG4J (常用)

  • LOG4J2

  • JDK_LOGGING

  • COMMONS_LOGGING

  • STDOUT_LOGGING (常用,标准日志)

  • NO_LOGGING

例如STDOUT_LOGGING的配置则需在配置文件中加入:

<!--    mybatis的设置-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

 

效果如下:

2、LOG4J

log4j的解释:

  • Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等 。

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

  • 可以通过一个配置文件来灵活地进行配置

 

开始使用:

  • Maven添加依赖

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>

     

  • mybatis设置log4j为日志

    <settings>
            <setting name="logImpl" value="LOG4J"/>
        </settings>

     

  • 编写log4j的配置文件(放在resoures中,名为log4j.properties)

    ### set log levels ###
    log4j.rootLogger = INFO , console , debug , error 
    ​
    ### console ###
    log4j.appender.console = org.apache.log4j.ConsoleAppender 
    log4j.appender.console.Target = System.out 
    log4j.appender.console.layout = org.apache.log4j.PatternLayout 
    log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n 
    ​
    ### log file ###
    log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender 
    log4j.appender.debug.File = ./log/log4j.log
    log4j.appender.debug.Append = true 
    log4j.appender.debug.Threshold = INFO 
    log4j.appender.debug.layout = org.apache.log4j.PatternLayout 
    log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n 
    ​
    ### exception ###
    log4j.appender.error = org.apache.log4j.DailyRollingFileAppender 
    log4j.appender.error.File = ./log/log4j.log
    log4j.appender.error.Append = true 
    log4j.appender.error.Threshold = ERROR 
    log4j.appender.error.layout = org.apache.log4j.PatternLayout 
    log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n 
    ​
    ​
    ###需要声明,然后下方才可以使druid sql输出,否则会抛出log4j.error.key not found
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
    log4j.appender.stdout.Target=System.out 
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
    log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %l %c%n%p: %m%n 
    ​
    ### druid sql ###
    log4j.logger.druid.sql=warn,stdout 
    log4j.logger.druid.sql.DataSource=warn,stdout 
    log4j.logger.druid.sql.Connection=warn,stdout 
    log4j.logger.druid.sql.Statement=warn,stdout 
    log4j.logger.druid.sql.ResultSet=warn,stdout

     

  • java代码中使用log4j

        
    @Test
        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语句)

 

  • 接口

     //通过分页获取用户信息
        List<User> getUserByLimit(Map<String,Integer> map);

     

  • xml的语句

    <!--    获取分页-->
        <select id="getUserByLimit" parameterType="map" resultType="User">
            select * from user limit #{startIndex},#{pageSize}
        </select>

     

  • 测试

      @Test
        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代码层面)

  • 接口

    //rowbounds实现分页
        List<User> getUserByRowBounds();

     

  • xml文件

    <!--    rowbounds实现分页-->
        <select id="getUserByRowBounds" resultType="User">
            select * from user
        </select>

     

  • 测试代码

    @Test
        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();
        }

     

 

 

posted @ 2020-01-19 14:38  Nevesettle  阅读(112)  评论(0编辑  收藏  举报