mybatis-ResultMap简单案例

一.数据库字段

二.java bean 属性

//实体类
public class User {
    private int id;
    private String name;
    private String password;
}

三.UserMapper接口方法

//根据id查询用户
User getUser(int id);

四.UserMapper.xml中使用结果集映射->resultMap

<mapper namespace="com.lv.dao.UserMapper">
<!--    结果集映射-->
    <resultMap id="userMap" type="user">
<!--        column数据库中的字段,property实体类中的属性-->
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="pwd" property="password"/>
    </resultMap>
    <select id="getUser" resultMap="userMap">
        select * from mybatis.user where id = #{id}
    </select>
</mapper>

五.测试

@Test
public void test(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    User user = mapper.getUser(1);
    System.out.println(user);
    sqlSession.close();
}

posted @ 2022-01-05 15:35  从0开始丿  阅读(65)  评论(0编辑  收藏  举报