MyBatis03:ResultMap和分页

本文介绍了在MyBatis中如何处理数据库字段与实体类属性名不一致的情况,通过ResultMap进行结果集映射。同时,讨论了MyBatis的日志工厂配置,包括在mybatis-config.xml中设置日志以及使用Log4j进行详细日志输出。此外,文章还讲解了两种分页实现方式:使用limit和RowBounds,以及PageHelper分页插件的使用。
摘要由CSDN通过智能技术生成

MyBatis03:ResultMap和分页

搭建项目

image-20201109092302736

当数据库字段和实体类属性名不一致时

image-20201109091946241

public class User {
   
    private int id;
    private String name;
    private String password;
    //get\set\有参无参构造
}

查询结果为null

image-20201109092106240

sql映射文件中

<select id="selectUser" resultType="com.wgg.pojo.User">
    select * from user
</select>

相当于

select id,name,pwd from user 
  • mybatis会根据这些查询的列名(会将列名转化为小写,数据库不区分大小写) , 去对应的实体类中查找相应列名的set方法设值 , 由于找不到setPwd() , 所以password返回null ; 【自动映射】

使用结果集映射解决–>ResultMap

UserMapper.xml

<mapper namespace="com.wgg.dao.UserMapper">
    <select id="selectUser" resultMap="UserMap">
        select * from user
    </select>

    <resultMap id="UserMap" type="user">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <!-- column是数据库表的列名 , property是对应实体类的属性名 -->
        <result column="pwd" property="password"/>
    </resultMap
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值