目录
依赖
配置信息
xml文件
mapper接口
打印日志
分页查询
依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>配置信息
数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/wddatabase?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root其他配置
#SpringBoot整合mybatis
mybatis:
  #加载指定的xml映射文件
  mapper-locations: classpath:mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true指定的xml映射文件一定要跟target目录下的路径保持一致

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.wd.admin.mapper.PmsProductMapper">
    <select id="getById" parameterType="long" resultType="com.wd.admin.entity.PmsProduct">
        select * from pms_product where id = #{id,jdbcType=BIGINT};
    </select>
</mapper>mapper接口
public interface PmsProductMapper{
    /**
     * 根据id查询
     *
     * @param id Long
     * @return PmsProduct
     */
    PmsProduct getById(Long id);
}打印日志
第一种方式:
logging:
  level:
    com.wd.admin.mapper: debug #mapper类存放的路径实际打印效果

第二种方式:
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl打印效果:

第三种打印:
这种和第二种是不能兼容的会报错
property 'configuration' and 'configLocation' can not specified with together
mybatis-config.xml
<?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>
    <!-- 开启驼峰映射,为自定义的sql语句服务-->
    <!--设置启用数据库字段下划线映射到java对象的驼峰命名属性,默认为false-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
</configuration>mybatis:
  #加载指定的xml映射文件
  mapper-locations: classpath:mappers/*.xml
  config-location: classpath:mybatis-config.xml
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl打印效果

分页查询
查到的数据放到pageInfo里面
PageInfo<PmsProduct> pageInfo = new PageInfo<>(productList);
if (CollectionUtils.isEmpty(productList)) {
	return new PageVo<>(queryListReq.getPageNum(), queryListReq.getPageSize(), 0, 0, Collections.emptyList());
}
return PageUtils.rebuildPageData(pageInfo, item -> {
	PmsProductVo pmsProductVo = new PmsProductVo();
	BeanUtils.copyProperties(item, pmsProductVo);
	return pmsProductVo;
});这边用lambda表达式操作一波
public static <T,R> PageVo<R> rebuildPageData(PageInfo<T> pageInfo, Function<T, R> function) {
	List<T> list = pageInfo.getList();
	List<R> returnList = list.stream().map(function).collect(Collectors.toList());
	return new PageVo<R>(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(), pageInfo.getPages(),returnList);
}请求返回

看一下分页查询的参数
-  pagehelper.helperDialect:指定数据库方言,PageHelper 根据不同的数据库方言生成不同的分页查询语句。常见的取值有mysql、oracle、sqlserver等。
-  pagehelper.reasonable:设置为true时,启用合理化参数,默认为false。当设置为true时,如果 pageNum 参数小于 1,则自动将其设置为 1;如果 pageNum 大于总页数,则自动将其设置为总页数。
-  pagehelper.supportMethodsArguments:设置为true时,支持通过 Mapper 接口中的方法参数传递分页参数,默认为false。如果设置为true,在 Mapper 接口中的方法中可以直接传递pageNum和pageSize参数,而不需要通过PageHelper.startPage()方法来设置分页参数。
-  pagehelper.params:设定的参数可以传递到具体的数据库方言进行解析,默认为空。该属性可以用于传递一些特殊的参数给数据库方言解析器。
这些属性可以通过在配置文件中使用 <property> 标签进行设置,或者通过代码中的属性配置进行设置。例如,在 Spring Boot 中可以在 application.properties 或 application.yml 文件中进行配置:
# PageHelper 配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params= count=countSql`count=countSql` 是 MyBatis 中配置分页插件 PageHelper 时的一个常见设置,用于指定在进行分页查询时,PageHelper 使用的 SQL 语句是否是通过 `countSql` 属性指定的 SQL 语句来获取总记录数。
默认情况下,PageHelper 会通过拦截原始的查询 SQL 语句,自动生成一个对应的统计总记录数的 SQL 语句。这个生成的 SQL 语句会将原始的查询 SQL 语句作为子查询,并在外层进行 count 操作。
然而,在某些复杂的查询场景下,生成的统计总记录数的 SQL 语句可能无法正确地获取到正确的结果,或者性能较差。为了解决这种情况,可以将 `countSql` 设置为 `true`,即 `count=countSql`。
当设置为 `countSql` 时,PageHelper 会使用用户自定义的 SQL 语句获取总记录数。用户需要在分页插件的配置中指定 `countSql` 属性,并提供一个 SQL 语句来获取总记录数。这个 SQL 语句可以是自定义的,一般情况下是针对原始的查询 SQL 语句进行改造得到。
通过指定 `countSql` 来获取总记录数可以解决一些特殊情况下的问题,但同时也需要注意确保自定义的 SQL 语句能够正确地获取到正确的总记录数,并且性能较好。在大多数情况下,使用 PageHelper 自动生成的统计总记录数的 SQL 语句就可以满足需求。只有在遇到特殊情况时才需要使用 `countSql` 来自定义获取总记录数的 SQL 语句。



















