MyBatis的简单的ResultMaps映射

来源:互联网 发布:帝国cms 使用程序代码 编辑:程序博客网 时间:2024/05/29 08:21

resultMap 元素是 MyBatis 中最重要最强大的元素。ResultMap 的设计就是简单语句不需要明确的结果映射,而很多复杂语句确实需要描述它们的关系。

下面是一个简单的POJO类的定义:City

package com.keymen.domain;public class City {    private int id;    private String cityname;    private String countrycode;    private String district;    private int population;    public City() {        super();    }    public City(int id, String cityname, String countrycode, String district, int population) {        super();        this.id = id;        this.cityname = cityname;        this.countrycode = countrycode;        this.district = district;        this.population = population;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getCityname() {        return cityname;    }    public void setCityname(String cityname) {        this.cityname = cityname;    }    public String getCountrycode() {        return countrycode;    }    public void setCountrycode(String countrycode) {        this.countrycode = countrycode;    }    public String getDistrict() {        return district;    }    public void setDistrict(String district) {        this.district = district;    }    public int getPopulation() {        return population;    }    public void setPopulation(int population) {        this.population = population;    }   }

基于 JavaBean 的规范,上面这个类有 5 个属性:id、cityname、countrycode、district和 population。这些 在 select 语句中会精确匹配到列名。

这样的一个 JavaBean 可以被映射到结果集,就像映射到 HashMap 一样简单。

<select id="selectCities" resultType="com.keymen.domain.City">  select id, cityname, countrycode,district,population  from tb_city  where id = #{id}</select>

可以使用别名来限定resultType中引用的类。代码如下:

<!-- In mybatis-config.xml file --><typeAlias type="com.keymen.domain.City" alias="City"/><!-- In SQL Mapping XML file --><select id="selectCities" resultType="City">  select id, cityname, countrycode,district,population  from tb_city  where id = #{id}</select>

这些情况下,MyBatis 会在幕后自动创建一个 ResultMap,基于属性名来映射列到 JavaBean 的属性上。如果列名没有精确匹配,你可以在列名上使用 select 字句的别名(一个 基本的 SQL 特性)来匹配标签。比如:

<select id="selectCities" resultType="City">  select    id             as "id",    cityname       as "cityname",    countrycode    as "countrycode",    district       as "district",    population     as "population"  from tb_city  where id = #{id}</select>

MyBatis的cityMapper.xml映射文件中resultMap如下:

<resultMap id="cityResultMap" type="City">    <id column="id" property="id" jdbcType="INTEGER" />    <result column="name" property="cityname" jdbcType="VARCHAR" />    <result column="countryCode" property="countrycode" jdbcType="VARCHAR" />    <result column="disctrict" property="district" jdbcType="VARCHAR" />    <result column="population" property="population" jdbcType="VARCHAR" /></resultMap>

引用的语句其实可以很简单:

<select id="getCityByID" resultMap="cityResultMap">        select        id,name,district,population from city where id = #{id}</select><select id="getCityByConditionLike" parameterType="String"        resultType="City">        <![CDATA[        select id,name as cname, countrycode,district,population         from city        where name like concat(#{cname},'%')                ]]></select>
0 0
原创粉丝点击