posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
如果实体类的属性名称和数据库中的字段名称不一致,比如属性productName,数据库字段product_name。
这时候mybatis查询返回的结果需要跟实体类自动映射 就需要配置一下映射关系。
如果列名和属性名一样,那就不用配置映射关系了,直接使用resultType指定类就行。
如果不想输入全类名,需要配置mybatis包扫描路径。
复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xcg.webapp.mapper.ProductionMapper">
    <!--当实体类中的 property 名称 和 数据库中的 column 名称不一致的时候,需要配置一个映射关系-->
    <resultMap id="BaseResultMap" type="com.xcg.webapp.model.entity.Production">
        <id property="productionId" column="production_id" jdbcType="INTEGER" javaType="int"/>
        <id property="productionCode" column="production_code" jdbcType="VARCHAR" javaType="String"/>
        <id property="productionName" column="production_name" jdbcType="VARCHAR" javaType="String"/>
        <id property="imgUrl" column="img_url" jdbcType="VARCHAR" javaType="String"/>
        <id property="spec" column="spec" jdbcType="VARCHAR" javaType="String"/>
        <id property="purchasePrice" column="purchase_price" jdbcType="DECIMAL" javaType="BigDecimal"/>
        <id property="salesPrice" column="sales_price" jdbcType="DECIMAL" javaType="BigDecimal"/>
        <id property="productionStatus" column="production_status" jdbcType="VARCHAR" javaType="String"/>
    </resultMap>

    <!--保存-->
    <insert id="create" parameterType="com.xcg.webapp.model.entity.Production" useGeneratedKeys="true" keyProperty="production_id">
        insert into production(production_code,production_name,img_url,spec,purchase_price,sales_price,production_status) values(#{serial},#{production_code},#{production_name},#{img_url},#{spec},#{purchase_price},#{sales_price},#{production_status});
    </insert>

    <!--获取集合-->
    <!--resultMap="BaseResultMap"-->
    <!--resultType="com.xcg.webapp.model.entity.Production"-->
    <select id="getAllList" resultType="com.xcg.webapp.model.entity.Production">
        select * from production limit 10;
    </select>

    <!--获取单个-->
    <select id="getModelById" parameterType="INTEGER" resultType="com.xcg.webapp.model.entity.Production">
        select * from production where production_id=#{id};
    </select>
</mapper>
复制代码

 也可以sql查询字段 as 成类中的属性名称,这样使用resultType就行,比如:select pro_name as proName from production  

posted on   邢帅杰  阅读(120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
历史上的今天:
2019-04-25 mysql保存乱码(C#)
2017-04-25 阿里云SLB负载均衡与使用SSL域名证书
2016-04-25 阿里云Object Storage Service(OSS)
2016-04-25 js获得URL中的参数
点击右上角即可分享
微信分享提示