Mybatis的Po映射类型

一,基本的数据类型

如一下代码所示,id为Dao能够调用的识别代码,paramesterType为数据传入Sql时的类型,这个类型可以是简单的int,String

等基本类型,也可以是封装好的Interger,也包括定义的实体类对象(pojo)和HashMap对象,集合对象以及数组(这种方式需要使用到foreach进行拼接查询)。

1.基本类型

//基本类型
<select id="findEmpByName" parameterType="int” >
   select * from emp where empno=#{empno}
</select>
//类型打包器
<select id="findEmpByName" parameterType="java.lang.Integer” >
   select * from emp where empno=#{empno}

</select>

2.pojo类型

//pojo类对象
<select id="findEmpByName" parameterType="Po.Emp” >
select * from emp where empno=#{empno}
</select>
//pojo包装类对象
<select id="findEmpByName" parameterType="Po.EmpVo” >
   select * from emp where otherMsg = #{Emp.otherMsg}

</select>

3.hashMap类型

<select id="findUserByHashmap" parameterType="map" resultType="user">
select * from user where id=#{id} and username like '%${username}%'
</select>


//构造查询条件Hashmap对象
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id", 1);

map.put("username", "管理员");

//传递Hashmap对象查询用户列表
List<User>list = userMapper.findUserByHashmap(map);

//这里一般是controller将需要的条件写入到map中,然后将map通过方法调用的形式传入到sql语句实现查询,查询后的数据会返回到list集合里面。

二,ResultType

resulttype是返回类型的定义,可以将查询结果映射为定义的pojo类型。

1.基本类型

<select id="findEmpCount" resultType="int">
select count(1) from emp
</select>
//原始Dao开发方式
int i = sqlSession.selectOne("EmpDao.findEmpCount", null);
//Mapper代理的方式

int count = empMapper.findEmpCount(emp);

2.pojo类型

//输出pojo对象

<select id="findUserById" parameterType="int" resultType="user">
select * from user where id = #{id}
</select>


//输出pojo列表
<select id="findUserByUsername" parameterType="string" resultType="user">
select * from user where username like '%${value}%'

</select>

三,ResultMap结果集

  如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名对应 ,但实质上还需要将查询结果映射到pojo对象中。

  resultMap还可以实现将查询的结果映射为复杂的pojo结果集,完成高级输出结果映射。比如在查询结果映射对象中包括pojo和list实现关联查询。

//定义ResultMap
<resultMap type="po.Emp" id="empResultMap">
<id column="empno" property="id"/>
<result column="ename" property="name"/>
</resultMap>


//引用ResultMap
<select id="findEmpById" parameterType="int" resultType="empResultMap">
select * from emp where empno = #{empno}
</select

(1)定义resultMap的参数含义

             type:resultMap最终映射的java对象类型的 全名 或 别名

             id:对resultMap的唯一标识。

(2) 定义映射关系的参数含义

             <id />:查询结果集的唯一标识。如果是多个字段为复合唯一约束则定义多个<id/>。

            <result />:对普通名映射定义。

            Column:表示sql查询出来的字段名 或 别名。

            property:type指定的pojo类型中的属性名或 别名。


转自https://www.cnblogs.com/sh086/p/8380539.html   点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值