Mybatise---ResultSet关联查询(Mybatis查询多条数据返回ResultSet )

Mybatis查询多条数据返回ResultSet

1.封装为List

ResultType如果返回的是一个集合,则返回的是集合中元素的类型,非List类Mybatis会自动将所查询到的数据进行封装,封装为List
EmployeeMapper.java

在这里插入图片描述

EmployeeMapper.xml

在这里插入图片描述

MybatisTest.java

在这里插入图片描述

2.封装为Map
key是列名,value值对应值
多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是封装后的javaBean
EmployeeMapper.java

在这里插入图片描述

EmployeeMapper.xml
在这里插入图片描述

MybatisTest.java

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 中,可以使用自定义的类型处理器来处理日期类型的格式。具体步骤如下: 1. 创建日期类型处理器类,实现 org.apache.ibatis.type.TypeHandler 接口,如下所示: ``` import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeException; import java.sql.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> { @Override public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); } @Override public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException { String value = rs.getString(columnName); if (value != null) { return LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); } return null; } @Override public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String value = rs.getString(columnIndex); if (value != null) { return LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); } return null; } @Override public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String value = cs.getString(columnIndex); if (value != null) { return LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); } return null; } } ``` 2. 在 MyBatis 的配置文件中注册该类型处理器,如下所示: ``` <typeHandlers> <typeHandler handler="com.example.LocalDateTimeTypeHandler" javaType="java.time.LocalDateTime"/> </typeHandlers> ``` 3. 在实体类中使用 @TableField 注解指定日期类型的格式,如下所示: ``` import com.baomidou.mybatisplus.annotation.TableField; import java.time.LocalDateTime; public class User { @TableField(value = "create_time", el = "createTime, typeHandler=com.example.LocalDateTimeTypeHandler") private LocalDateTime createTime; // 省略其他字段和方法 } ``` 这样,在查询数据时,MyBatis-Plus 会自动使用 LocalDateTimeTypeHandler 处理日期类型的格式,返回指定格式的日期数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值