parameterType与resultType

如果注册过类型别名的,可以直接使用别名。没有注册过的必须,使用全限定类名。
基本类型和String我们可以直接写类型名称,也可以使用包名

定义别名
在SqlMapConfig.xml中定义

    <typeAliases>
        <!-- 单个别名定义 -->
        <typeAlias alias="user" type="com.itheima.domain.User"/>
        <!-- 批量别名定义,扫描整个包下的类,别名为类名(首字母大写或小写都可以) -->
        <package name="org.example.pojo"/>
    </typeAliases>

当数据库表字段名与实体类属性类型不一致时
1、使用别名

<select id="findAll" resultType="com.itheima.domain.User">
    select id as userId,username as userName,birthday as userBirthday,
    sex as userSex,address as userAddress from user
</select>

2、定义resultMap

<!-- 建立 User 实体和数据库表的对应关系
type 属性:指定实体类的全限定类名
id 属性:给定一个唯一标识,是给查询 select 标签引用用的。
-->
<resultMap type="com.itheima.domain.User" id="userMap">
    <id column="id" property="userId"/>
    <result column="username" property="userName"/>
    <result column="sex" property="userSex"/>
    <result column="address" property="userAddress"/>
    <result column="birthday" property="userBirthday"/>
</resultMap>
id 标签:用于指定主键字段
result 标签:用于指定非主键字段
column 属性:用于指定数据库列名
property 属性:用于指定实体类属性名称
posted @   max_yan  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示