#resultMap #列名和属性名不相同 @FDDLC

    科技2024-01-05  92

    在实际开发中,可能会碰到下面这种情况:

    对于上面这种情况,如果只配置:

    <select id="findAll" resultType="cn.liuxingchang.domain.Account"> select * from account </select>

    执行findAll的时候,就会:

    看到null了吗?说明account表中的money没有把值传给Account类中的balance!

    怎么办?

    方法很多,这里给出两种。

    方法一:在SQL语句中起别名

    <select id="findAll" resultType="cn.liuxingchang.domain.Account"> select id, name, money as balance from account </select>

    特点:执行效率高;开发效率低:如果涉及的语句很多的话,修改起来会比较麻烦。

     

    方法二:使用resultMap标签

    <resultMap id="accountMap" type="cn.liuxingchang.domain.Account"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="balance" column="money" /> </resultMap> <select id="findAll" resultMap="accountMap"> select * from account </select>

     

    Processed: 0.008, SQL: 8