MyBatis core configuration file (sqlMapConfig.xml), paging assistant plugins demo, typeHandlers (less use)

1.typeHandlers (less use)

It can be realized in the data table that it is bigint type, and what you store is date type, then you can use typeHanders to customize the date to long and store it in, or you can take bigint from the database to date and then output to java. Console
Insert picture description here
Insert picture description hereInsert picture description hereInsert picture description here

2.plugins pagination assistant

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description hereInsert picture description hereThe code is still something to add to the previous blog

pom.xml joins these two:

<!--分页助手-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>3.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.1</version>
        </dependency>

sqlMapConfig.xml configuration:

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

    <!--通过properties标签加载外部properties文件-->
    <properties resource="jdbc.properties"></properties>
    
    <!--自定义别名-->
    <typeAliases>
        <typeAlias type="com.itheima.domain.Sys_user" alias="user"></typeAlias>
        <!--这里还可以配多个-->
    </typeAliases>

    <!--配置分页助手插件-->
    <plugins>
        <!-- 注意:分页助手的插件 配置在通用馆mapper之前 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 指定方言 -->
            <property name="dialect" value="mysql"></property>
        </plugin>
    </plugins>

    <!--数据源环境-->
    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>

            </dataSource>
        </environment>
    </environments>

    <!--加载映射文件-->
    <mappers>
        <mapper resource="com.itheima.mapper/UserMapper.xml"></mapper>
    </mappers>

    
</configuration>

com.itheima.mapper/UserMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.UserMapper">
   
    <select id="findAllByPageHelper" resultType="user">
        SELECT * from sys_user
    </select>


</mapper>

select * from user Don’t use semicolon, otherwise the following limit will report an error.
Corresponding mapper interface (equivalent to dao interface):
com.itheima.mapper.UserMapper:

package com.itheima.mapper;

import com.itheima.domain.Sys_user;

import java.util.List;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/30 11:06
 */
public interface UserMapper {
    
    
    
    public List<Sys_user> findAllByPageHelper(); //根据分页查询

}

Test code:

package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Sys_user;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/30 11:07
 */
public class ServiceDemo {
    
    

   
    @Test
    //演示分页助手
    public void test3() throws IOException {
    
    

        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

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

        //设置分页相关参数,当前页+每页显示的条数,userMapper.xml的select * from user不要分号
        PageHelper.startPage(1,2);

        List<Sys_user> sys_userList  = mapper.findAllByPageHelper();
        for (Sys_user sys_user : sys_userList) {
    
    
            System.out.println(sys_user);
        }

        //获得与分页相关参数,要把上面的sys_userList放在下面
        PageInfo<Sys_user> pageInfo=new PageInfo<Sys_user>(sys_userList);
        System.out.println("当前页:"+pageInfo.getPageNum());
        System.out.println("每页显示条数:"+pageInfo.getPageSize());
        System.out.println("总条数:"+pageInfo.getTotal());
        System.out.println("总页数:"+pageInfo.getPages());
        System.out.println("上一页:"+pageInfo.getPrePage());
        System.out.println("下一页:"+pageInfo.getNextPage());
        System.out.println("是否是第一页:"+pageInfo.isIsFirstPage());
        System.out.println("是否是最后一页:"+pageInfo.isIsLastPage());

        sqlSession.close();
    }
}

Insert picture description here

3. Summary

This blog only introduces 4 and 5, 123 see what I wrote before
Insert picture description here

Guess you like

Origin blog.csdn.net/GLOAL_COOK/article/details/113486263