MyBatis - 实体类的属性名和数据库列名不一致时的两种解决办法!

问题:两者不一致时 , 查询结果无法封装到实体!(也就无法查询出来)
在这里插入图片描述

查询的sql语句中使用别名进行查询.

但要注意: 字段名的别名 要和 实体类的属性名一致!

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

<!--  namespace:接口的全路径名.  -->
<mapper namespace="com.xxx.dao.UserMapper">
	<!-- 使用别名 --> 
    <select id="queryAll" resultType="com.xxx.domain.User">
        select 
        	id as userId,
        	username as userName,
            address as userAddress,
            sex as userSex,
            birthday as userBirthday 
        from user;
    </select>
</mapper>

注: 如果使用别名 , 每一个sql语句都需要加别名 (很麻烦)
故: 一般都使用第二种.

② 使用resultMap

UserMapper.xml

<mapper namespace="com.jxj.dao.UserDao">
    <resultMap id="userResultMap" type="User">
    	<!-- 
    		主键字段  
    		    property: 实体类属性名.
    		    column: 库中表的列名
    		    javaType: 数据类型.
    	--> 
        <id property="userId" column="id" javaType="int"></id>
        <!-- 非主键字段  --> 
        <result property="userSex" column="sex" javaType="string"></result>
        <result property="userAddress" column="address" javaType="string"></result>
        <result property="userBirthday" column="birthday" javaType="date"></result>
        <result property="username" column="username" javaType="string"></result>
    </resultMap>

    <select id="queryAll"  resultMap="userResultMap">
        select * from user
    </select>

注: select中resultMap的属性值 要和 resultMap中id的属性值一样.

测试类: UserMapper.java

@Test
public void queryAll() throws IOException {
    // 1.创建工厂类.
    InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
    // 2.创建sql对象.
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 3.创建接口的实现类对象.
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    // 4.调用接口中的方法 (代理)
    List<User> users = mapper.queryAll();
    for (User user : users) {
        System.out.println(user);
    }
    sqlSession.close();
    in.close();
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus 支持外键关联的操作,具体实现方式可以分为两种: 1. 通过 `@TableField` 注解指定关联属性,使用 `@TableField(exist = false)` 标识该属性为非数据库字段,然后通过 `@TableField(value = "foreign_key")` 指定关联的外键字段。具体代码如下: ```java @Data public class User { private Long id; private String name; private Integer age; @TableField(value = "dept_id") private Long deptId; } @Data public class Dept { private Long id; private String name; } @Mapper public interface UserMapper extends BaseMapper<User> { @Select("select u.*, d.name as dept_name from user u left join dept d on u.dept_id = d.id where u.id=#{id}") User selectUserWithDept(Long id); } ``` 2. 使用 Mybatis-Plus 提供的 `@TableName` 注解的 `excludeProperty` 属性指定不需要插入的属性,然后使用 `@TableId` 注解指定主键,使用 `@TableField` 注解指定外键。具体代码如下: ```java @Data @TableName(excludeProperty = "dept") public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; @TableField("dept_id") private Long deptId; @TableField(exist = false) private Dept dept; } @Data @TableName("dept") public class Dept { @TableId(type = IdType.AUTO) private Long id; private String name; } @Mapper public interface UserMapper extends BaseMapper<User> { @Select("select u.*, d.name as dept_name from user u left join dept d on u.dept_id = d.id where u.id=#{id}") @Results({ @Result(column = "dept_id", property = "deptId"), @Result(column = "dept_name", property = "dept.name") }) User selectUserWithDept(Long id); } ``` 这里使用 `@Results` 注解来指定结果集中的属性映射关系,其中 `column` 指定查询结果中的列名,`property` 指定实体类中的属性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值