Mybatis(三)返回值
一. Mybatis返回值
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。
在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。
①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当提供的返回类型属性resultType的时候,MyBatis自动的给对应的值赋给resultType所指定对象的属性。
②当提供的返回类型是resultMap时,因为Map不能很好表示领域模型,就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用(association,Collection),
<resultMap type="com.softjx.model.User" id="UserMap">
<result column="t_id" property="id"/>
<result column="t_username" property="username" />
<result column="t_password" property="password"/>
</resultMap> <!-- 查询数据表中的所有记录,并封装User对象集合 -->
<select id="selectAll" resultMap="UserMap">
select * from t_user
</select> <!-- 根据id查询数据表中的一条记录,并封装User对象 -->
<select id="selectById" resultMap="UserMap">
select * from t_user where t_id=#{id};
</select> //统计id>?记录数
public int coutUser(Integer id); //统计id>?记录数 <select id="coutUser" resultType="int">
select count(*) from t_user where t_id>#{id};
</select>
二. Mybatis注解配置
Mybatis中使用注解,就不需要编写mapper.xml文件,直接在接口代码中使用注解。
1、mybatis的全局配置文件要修改,指向接口文件路
<mappers>
<mapper class="com.softjx.dao.UserMapper"/>
</mappers>
2、不用编写mapper.xml文件,写接口,在接口中使用注解
@Select("select t_id as id,t_username username,t_password as password from t_user")
//@Select("select * from t_user")//不好自动填充javabean中的数据
//@ResultType(User.class)//不写没有关系
public List<User> selectAll();
@Select("select t_id as id,t_username username,t_password as password from t_user where t_id=#{id}")
//@Select("select * from t_user where t_id=#{id}")//不好自动填充javabean中的数据
//@ResultType(User.class)//不写没有关系
public User selectById(int id);
@Select("select t_id as id,t_username username,t_password as password from t_user where t_id > #{a} and t_username=#{b}")
@ResultType(User.class)
public List<User> selectByNameId(Map<String, Object> map);
@Select("select t_id as id,t_username username,t_password as password from t_user where t_id > #{0} and t_username=#{1}")
@ResultType(User.class)
public List<User> selectByNameId1(Integer id,String ame);
@Select("select t_id as id,t_username username,t_password as password from t_user where t_id > #{id} and t_username=#{name}")
@ResultType(User.class)
public List<User> selectByNameId2(@Param("id")Integer id,@Param("name")String name);
@Select("select * from t_user")
@Results({@Result(property="id",column="t_id")
,@Result(property="username",column="t_username")
,@Result(property="password",column="t_password")
})
public List<User> selectAllUsers();
@Insert("insert into t_user (t_username,t_password) values (#{username},#{password})")
public int insertUser(User user);
@Update("update t_user set t_username=#{username},t_password=#{password} where t_id=#{id}")
public int updateUser(User user);
@Delete("delete from t_user where t_id=#{a}")
public int deleteUser(int id);
3、编写测试类与以前一样,没有区别。
Mybatis(三)返回值的更多相关文章
- Mybatis select返回值为map时,选取表字段的两列作为key,value
项目需要从ibatis升级到MyBatis,dao中有一个方法返回Map类型,具体是查询语句查询两个字段,将结果列表字段A的值作为key字段B的值作为value存入Map中作为结果返回: ibatis ...
- mybatis update 返回值
mybatis sql: <update id="test" parameterType="map"> update test_0731 set n ...
- mybatis之返回值总结
mybatis框架让我们能在编程中只需要编写一个接口,然后再编写mapper映射文件,无需编写接口的实现类就可以实现从数据库检索数据.这是mybatis通过动态代理,把mapper映射文件的内容转化为 ...
- 深入了解MyBatis返回值
深入了解MyBatis返回值 想了解返回值,我们须要了解resultType,resultMap以及接口方法中定义的返回值. 我们先看resultType和resultMap resultType和r ...
- Mybatis 实用篇(四)返回值类型
Mybatis 实用篇(四)返回值类型 一.返回 List.Map List<User> getUsers(); <select id="getUsers" re ...
- Mybatis执行sql(insert、update、delete)返回值问题
数据库:Mysql 在使用mybatis的过程中对执行sql的返回值产生疑问,顺手记录一下. 结论: insert: 插入n条记录,返回影响行数n.(n>=1,n为0时实际为插入失败) up ...
- MyBatis查询结果resultType返回值类型详细介绍
一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 String getEmpNameById( ...
- mybatis中查询结果为空时不同返回类型对应返回值
今天在别人的代码基础上实现新需求,看到对于mybatis查询结果的判断不是很正确,如果查询结果为空就会异常,不知道大家有没有这样的疑惑:mybatis中resultType有多种返回类型,对于每种不同 ...
- iOS用三种途径实现一方法有多个返回值
以前觉得这种标题有点偏向于理论,实际开发中怎么会有这种诡异的需求,但是真正遇到了这种硬需求时觉得还是有那么点价值的,理论付诸了实践在此也就做了个整理. 以我私下开发中的一处代码为例,本意是希望有这么一 ...
随机推荐
- spark-shell启动报错:Yarn application has already ended! It might have been killed or unable to launch application master
spark-shell不支持yarn cluster,以yarn client方式启动 spark-shell --master=yarn --deploy-mode=client 启动日志,错误信息 ...
- python函数前篇
函数:函数是指将一组语句的集合通过一个函数名封装起来,要想执行这个函数,只需调用其函数名即可 函数特性: 减少重复代码 使程序变得可扩展 使程序变得易维护 什么是函数? 函数就是具备某一特定功能的工具 ...
- ArrayList 源码(基于Java1.8)
ArrayList 源码 ArrayList 基于数组实现,也就是类对变量 Object[]系列操作,封装为常用的add,remove,indexOf, contains本质是通过 size 计数器对 ...
- SpringBoot初体验(续)
1.如果你还不知道SpringBoot的厉害之处,或者你不知道SpringBoot的初级用法,请移步我的上一篇文章,传送门 2.SpringBoot中的表单验证 所谓验证,无非就是检验,对比,正如ja ...
- getSystemService详解
android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监听是否有SD卡安装及移除,ClipboardServi ...
- 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...
- asp.net core webapi文件上传
最近开发一个新项目,使用了asp.net core 2.0,采用webapi开发后台,postgresql为数据库.最先来的问题就是上传文件的问题. POST文件的一些坑 使用默认模板创建webapi ...
- 使用 Go-Ethereum 1.7.2搭建以太坊私有链
目录 [toc] 1.什么是Ethereum(以太坊) 以太坊(Ethereum)并不是一个机构,而是一款能够在区块链上实现智能合约.开源的底层系统,以太坊从诞生到2017年5月,短短3年半时间,全球 ...
- 入门到熟练-SpringBoot
Spring Boot概述 1.1. Spring Boot是什么 Spring Boot是一套基于Spring框架的微服务框架. 1.2. Spring Boot框架出现的背景 由于Spring是一 ...
- Scrum Meeting Alpha - 5
Scrum Meeting Alpha - 5 NewTeam 2017/10/20 地点:主楼与4号楼之间的走廊2楼 任务反馈 团队成员 完成任务 计划任务 安万贺 完成了对班级作业部分的API的包 ...