当前位置: 首页 > news >正文

Springboot+MyBatis使用

目录

依赖

配置信息

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.Driverurl: jdbc:mysql://127.0.0.1:3306/wddatabase?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=trueusername: rootpassword: 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/*.xmlconfig-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 根据不同的数据库方言生成不同的分页查询语句。常见的取值有 mysqloraclesqlserver 等。

  • pagehelper.reasonable:设置为 true 时,启用合理化参数,默认为 false。当设置为 true 时,如果 pageNum 参数小于 1,则自动将其设置为 1;如果 pageNum 大于总页数,则自动将其设置为总页数。

  • pagehelper.supportMethodsArguments:设置为 true 时,支持通过 Mapper 接口中的方法参数传递分页参数,默认为 false。如果设置为 true,在 Mapper 接口中的方法中可以直接传递 pageNumpageSize 参数,而不需要通过 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 语句。

http://www.lryc.cn/news/97664.html

相关文章:

  • WebSocketServer的使用(@ServerEndpoint)
  • 【yolov8系列】将yolov8-seg 模型部署到瑞芯微RK3566上
  • Java类的默认构造函数
  • 华为openGauss数据库入门 - gsql用法
  • The Sandbox 重新上线,带来全新体验!
  • 动态内存管理面试题
  • 树莓派外设开发编程
  • Java从入门到精通(一)
  • Electron从构建到打包程exe应用
  • 3分钟学会设计模式 -- 单例模式
  • 《面试1v1》Kafka与传统消息系统区别
  • 【算法第十三天7.27】平衡二叉树,二叉树所有路径,左叶子之和
  • arm架构cloudstack的agent报错No more available PCI slots如何解决
  • day43-Feedback Ui Design(反馈ui设计)
  • TypeScript基础篇 - TS日常类型 上篇
  • 量化交易——python数据分析及可视化
  • 微服务网关
  • 【打卡】Datawhale暑期实训ML赛事
  • 【python脚本】python实现:目标检测裁剪图片样本,根据类标签文件进行裁剪保存
  • Mac 终端美化显示
  • 信息安全:密码学基本理论.
  • 【linux升级ssh】 利用rpmbuild工具对ssh打包为rpm包进场安装升级
  • UCloud上线可商用LLaMA2镜像,助力AGI应用发展
  • Linux推出Debian 12.1,并进行多方面系统修复
  • Spring 事务的使用、隔离级别、@Transactional的使用
  • Top命令
  • (三)RabbitMQ七种模式介绍与代码演示
  • ElasticSearch Java API 操作
  • 【Qt】QML-01:使用QtCreator10创建QML工程,并讲解第一个程序:Hello World
  • Docker的安装与部署