• 实体类属性名与数据库列名不一致解决方案


    实体类属性和数据库列名不一致,不能自动封装数据 ,查出的数据就是null

    解决方案

    1 起别名:

    在sql语句中,对不一样的列名起别名,别名和实体类的属性名一样

    1. <select id="selectAll" resultType="Brand">
    2. SELECT id,brand_name as brandName, company_name as companyName, ordered, description, status FROM tb_brand
    3. select>

    还可以使用片段提高复用性

    定义sql片段

        <sql id="brand_column">id,brand_name as brandName, company_name as companyName, ordered, description, statussql>

    在查询即可

    1. <select id="selectAll" resultType="Brand">
    2. select *
    3. from tb_brand;
    4. select>

    2:resultMap

    定义完成不一致的属性名和列名的映射

    步骤 :1 定义标签
    
    
    1. <resultMap id="BrandresultMap" type="brand">
    2. <result column="brand_name" property="brandName">result>
    3. <result column="company_name" property="companyName">result>
    4. resultMap>
    
         2  在