实体类中的变量与数据库对应表的变量名不一致

实体类中的变量与数据库对应表的变量名不一致,如果sql 中没有添加别名, 那么会找不到对应的字段, 则会报空指针异常。如果实体类和数据库字段一样则不需要加As

User

@Data
@TableName(value = "richfit_myku")
public class User implements Serializable {

    private int userid;
    private String username;
    private int usersex;
    private Double degree;
}

UserInfo

@Data
@TableName(value = "richfit_mykuinfo")
public class UserInfo implements Serializable {

    private int id;
    private String hobby;
    private int userid;
}

第一种方式 as

<select id="queryUserTest" resultType="com.mybatis.api.mybatis.model.UserAndUserInfoResult">
   select b.id,b.my_hobby as hobby,a.user_id as userid,a.user_sex as sex from richfit_myku a,richfit_mykuinfo b
     where 1=1
     <if test="username !=null and username !=''">
         and a.user_name=#{username}
     </if>
     <if test="hobby !=null and hobby !=''">
         and b.my_hobby=#{hobby}
     </if>
     and a.user_id=b.user_id
</select>

第二种方式 resultMap

<resultMap type="com.mybatis.api.mybatis.model.UserInfo" id="UserInfoMap">
        <id column="id" property="id"/>
        <result column="my_hobby" property="hobby"/>
        <result column="user_id" property="userid"/>
    </resultMap>
    <resultMap type="com.mybatis.api.mybatis.model.User" id="UserMap">
        <id column="user_id" property="userid"/>
        <result column="user_name" property="username"/>
        <result column="user_sex" property="user_sex"/>
        <result column="degree" property="degree"/>
    </resultMap>
    <select id="queryUserTest" resultMap="UserInfoMap,UserMap">
        select b.id,b.my_hobby,a.user_id,a.user_sex from richfit_myku a,richfit_mykuinfo b
        where 1=1
        <if test="username !=null and username !=''">
            and a.user_name=#{username}
        </if>
        <if test="hobby !=null and hobby !=''">
            and b.my_hobby=#{hobby}
        </if>
        and a.user_id=b.user_id
    </select>

第三种方式 mapUnderscoreToCamelCase

使用这种配置方式不需要手动指定别名 MyBatis 字段按照 “下画线转驼峰”的方式 自动映射。

首先了解:

printEmployeePaychecks();骆驼式命名法——函数名中的每一个逻辑断点都有一个大写字母来标记

print_employee_paychecks();下划线法-—函数名中的每一个逻辑断点都有一个下划线来标记。

springboot配置:
在这里插入图片描述
sql:

select role name , enabled, create by, create me from sys role 
where id = #{id)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值