Mybatis查询结果封装到属性

对于数据库表来说,字段设计的规则一般由下划线组成

user_name   user_age

而实体类属性一般遵循驼峰命名法

userName   userAge

那么如何将数据库查询结果封装到实体类呢

select user_name,user_age from user

提供如下两种方法:
1:配置文件中配置Mabatis查询结果表字段,转驼峰

mybatis.configuration.map-underscore-to-camel-case=true

2:在mapper文件中使用映射map,将数据库对应的字段信息映射在实体类的属性中

<resultMap id="studentMap" type="com.wkcto.domain.Student">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="email" property="email"/>
        <result column="address" property="address"/>
        <result column="sex" property="sex"/>
        <result column="age" property="age"/>
        <result column="crtTime" property="crt_time"/>
        <result column="updTime" property="upd_time"/>
    </resultMap>

在查询方法定义返回值为这个map

<select id="allList" resultMap="studentMap">
        select * from student order by id
    </select>
发布了27 篇原创文章 · 获赞 1 · 访问量 846

猜你喜欢

转载自blog.csdn.net/weixin_44971379/article/details/104911124
今日推荐