mybatis之sql查詢配置文件resultType和resultMap

如果實體類的屬性名稱和數據庫中的字段名稱不一致,比如屬性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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章