关于Mybatis中查询结果和java对象的映射的调查

        由于大多数数据库不区分大小写,所以我们在设计数据库表的字段名时,常常以下划线进行两个单词的区分,如pass_word,create_date_time,而在java实体对象中,一般使用驼峰式命名规则,如passWord,createDateTime。

        为了实现ORM,有两种手动的方式,第一种是在xml文件中使用resutMap标签,如下:

    <select id="getBookByNameAndTypeId" parameterType="book" resultMap="typeAndBook">
        select a.id,bookName,author,description,press,inventory,typeId,bookCode,picturePath,price,salesNumber,saleState,createDate,typeName
        from
        book_tb a,book_type_tb b
        <where>
        a.typeId = b.id
            <if test="typeId != null and typeId != ''">
                and typeId = #{typeId}
            </if>
            <if test="bookName != null and bookName !=''">
                and bookName like concat('%',#{bookName},'%')
            </if>
        </where>
    </select>
    <resultMap id="typeAndBook" type="book">
        <id property="id" column="id"></id>
        <result property="bookName" column="bookName"></result>
        <result property="author" column="author"></result>
        <result property="description" column="description"></result>
        <result property="press" column="press"></result>
        <result property="inventory" column="inventory"></result>
        <result property="typeId" column="typeId"></result>
        <result property="bookCode" column="bookCode"></result>
        <result property="picturePath" column="picturePath"></result>
        <result property="price" column="price"></result>
        <result property="salesNumber" column="salesNumber"></result>
        <result property="saleState" column="saleState"></result>
        <result property="createDate" column="createDate"></result>
        <association property="bookType" javaType="bookType">
            <result property="typeName" column="typeName"></result>
        </association>
    </resultMap>

        或者我们在sql语句中为查询的字段设置别名,别名对应java对象的命名,如下

    @Select({
            " select id id,bookName bookName,author author,description description,press press,createDate createDate,inventory inventory,typeId typeId,bookCode bookCode,picturePath picturePath,price price,salesNumber salesNumber,saleState saleState",
            " from book_tb",
            " where author = #{selectParams.authorStr} and inventory = #{inventory} and createDate >= #{selectParams.getCreateDateFrom}"
    })
    List<Book> selectBookByParam(@Param(value = "selectParams") SelectParams selectParams, @Param(value = "inventory") Integer inventory);

        为了简化代码,Mybatis提供了一个全局属性mapUnderscoreToCamelCase,将其设置为true可以自动将以下划线方式命名的数据库映射到java对象的驼峰式命名属性中,这个属性的默认值为false,使用时需手动开启。

xml文件中配置如下:

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

yml文件中配置如下:
 

mybatis:
  #实体文件别名
  type-aliases-package: com.itheima.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #映射文件路径
    map-underscore-to-camel-case: true
  #mapper-locations: classpath:mapper/*.xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值