Mybatis之resultType resultMap parameterType

1.parameterType 可以省略不写,其本身只是一个限制传入的参数类型,如果省略Mybatis本身会自动获取Mapper接口的参数进行自动判断。如果要传入多个参数用@Param注解,通过#{value}来获取参数。

2.resultType 设置返回的类型。sql语句查询的结果封装成POJO返回,这里要求POJO也就是我们的实体类Entity的属性(成员变量)要和所查询的表的字段要一一对应,如果不对应则为null空值。为了解决实体类和表字段的对应,可以开启驼峰映射

mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true  #开启驼峰映射

例如表的一个字段为 phone_num ,实体类为phoneNum;开启了驼峰映射后
phone_num->phoneNum->setPhoneNum(); 把查询的结果封装到对象实现匹配

3.resultMap
其实所有的resultType返回的结果都是经过resultMap转换过来的,返回的Map集合再次封装成对象,可以理解为
resultMap–>resultType
在进行多表查询时候可以通过resultMap设置POJO对象没有的属性返回非常有用

<resultMap id="BaseResultMap" type="com.cwy.mybatisstudy.demo.entity.PersonInfo">
        <id column="id" property="id" javaType="java.lang.String"></id>
        <result column="name" property="name" javaType="java.lang.String"></result>
        <result column="father_name" property="fatherName" javaType="java.lang.String"></result>
        <result column="mother_name" property="motherName" javaType="java.lang.String"></result>
        <result column="phone_brand" property="phoneBrand" javaType="java.lang.String"></result>
        <result column="home_address" property="homeAddress" javaType="java.lang.String"></result>
    </resultMap>
    
    <resultMap id="BaseResultMap2" type="com.cwy.mybatisstudy.demo.entity.Study" >
        <id column="id" property="id" javaType="java.lang.String"></id>
        <result column="name" property="name" javaType="java.lang.String"></result>
        <result column="father_name" property="fatherName" javaType="java.lang.String"></result>
        <result column="mother_name" property="motherName" javaType="java.lang.String"></result>
        <result column="phone_brand" property="phoneBrand" javaType="java.lang.String"></result>
        <result column="home_address" property="homeAddress" javaType="java.lang.String"></result>
        <result column="study_language" property="studyLanguage" javaType="java.lang.String"></result>
    </resultMap>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值