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
- MyBatis的简单的ResultMaps映射
- MyBatis的ResultMaps
- MyBatis的ResultMaps之一对一关系
- MyBatis的ResultMaps之一对多关系
- MyBatis的ResultMaps之一对多关系
- [MyBatis日记](5)结果集映射ResultMaps
- Mybatis的输入映射
- Mybatis的输出映射
- Mybatis的映射配置文件
- mybatis的管理映射
- mybatis的映射
- MyBatis使用接口映射的简单HelloWorld小例子
- MyBatis中XML 映射配置文件的简单介绍
- MyBatis的XML映射文件、映射器
- mybatis的SQL映射配置文件
- mybatis的SQL映射配置文件
- mybatis的SQL映射配置文件
- mybatis的xml映射文件**
- spring注解注入的配置
- http://www.jianshu.com/p/a8cce94d508e
- bzoj 2038 莫队算法
- 彻底弄懂 Http 缓存机制 - 基于缓存策略三要素分解法
- C语言日记——递归
- MyBatis的简单的ResultMaps映射
- 判定程序属于计算密集和访存密集的方法
- 微信小程序 案例 学习 1
- Android Dialog样式activity并去掉标题
- Glide库加载图片
- leetcode-119. Pascal's Triangle II
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()对比
- 二维数组查找
- 逐浪CMS后台微信公众号如何配置参数