Mybatis 增删改查等操作的返回值

Mybatis 增删改查等操作的返回值

之前一篇文章 mybatis 传递参数的7种方法 讲了mybatis中传递入参的7中方式,这节讲下增删改查等操作的返回值都代表什么意思,以及怎么使用这些返回值

1.更新 update

update: 返回值为匹配数据库的条数(不论最终是否对数据进行了修改,只要某条记录符合匹配条件,返回值就加1)

2.插入 insert

insert:如果成功返回值为插入数据库的条数,失败返回的是exception,所以需要对异常进行处理

    public int insertDept(Department department)
    {
        try{
            return  departmentMapper.insertAutoId(department);
        }catch (Exception e )
        {
            return -1;
        }
    }
@ApiOperation(value = "新增部门")
@PostMapping("new")
public ResultMsg newDepartment(@RequestBody Department department) {
    department.setId(idWorker.nextId());
    int result = departmentService.insertDept(department);
    return ResultMsg.getStrMsg(result > 0 ? "SUCCESS" : "FAILED");
}

3.删除 delete

delete:返回值为删除的数据条数

4. 查询 select

四种基本操作中,以查询操作的返回值最为多样化

Mybatis中,通过resultType或resultMap指定返回值

3.1返回一般数据类型

即返回Java基本的数据类型,如String Long int

mapper

String getDeptName(Long id);

xml

<select id="getDeptName" parameterType="java.lang.Long" resultType="string">
  select
   dept_name
  from department
  where id = #{id,jdbcType=BIGINT}
</select>
3.2返回javaBean类型

也可直接返回实体类,resultType指定实体类的路径

mapper

Department getDeptById(Long id);

xml

<select id="getDeptById" parameterType="java.lang.Long" resultType="com.wg.demo.po.Department">
  select
  *
  from department
  where id = #{id,jdbcType=BIGINT}
</select>

当实体类字段与数据库字段不一致时,可以使用resultMap

<select id="getDeptById" parameterType="java.lang.Long" resultMap="BaseResultMap">
  select
  *
  from department
  where id = #{id,jdbcType=BIGINT}
</select>
<resultMap id="BaseResultMap" type="com.wg.demo.po.Department">
  <id column="id" jdbcType="BIGINT" property="id" />
  <result column="dept_name" jdbcType="VARCHAR" property="deptName" />
  <result column="descr" jdbcType="VARCHAR" property="descr" />
  <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>

resultType方式:当使用resulType做返回结果处理时,SQL所查询的字段必须与相应的pojo中的字段相对应;因此对于单表查询用resultType是最合适的。

resultMap方式:当javabean和数据库表字段不一致时,可使用resultMap指定返回结果类型;一个Mapper文件可定义多个resultMap

在一个查询中,resultType跟resultMap不能同时存在

3.3返回List类型

当返回数据有多条时,可通过List方式返回

mapper

List<Department> selectAll();

xml

<select id="selectAll"   resultMap="BaseResultMap">
  select
  *
  from department
</select>
3.4 返回Map类型

该类型返回值可分为两种方式:

1.返回单条数据,map对应表中字段

Map getDeptAsMap(Long id);
<select id="getDeptAsMap" parameterType="java.lang.Long" resultType="map">
  select
  *
  from department
  where id = #{id,jdbcType=BIGINT}
</select>

2.返回多条数据

以Map形式返回多条数据,map中每一条记录对应一条查询记录,(该方式与List返回值类似,不过是数据的组织方式不同而已),这里需要指定使用哪个属性作为Map主键

mapper

    @MapKey("id") //指定Map的主键为返回结果的id
    Map getDeptAsMaps();

xml

<select id="getDeptAsMaps"   resultType="map">
  select
  *
  from department
</select>
3.5返回JSONObject

以JSONObject的形式返回查询结果,多用于查询结果列数不定的情况(就是说查询结果是动态的)

controller

@ApiOperation(value = "返回JSONObject")
@PostMapping("findJSONObject")
public ResultMsg findJSONObject( )
{
    return ResultMsg.getMsg(employeeMapper.findJSONResult());
}

mapper

JSONArray findJSONResult();

xml

<select id="findJSONResult" resultType="com.alibaba.fastjson.JSONObject" >
  SELECT * from employee
</select>
总结:

好了至此,mybatis中增删改查请求参数的方式,以及返回结果各种形式及含义就算是讲完了,弄懂了这些,在平时开发中我们就可以在做增删改查操作时,从容的选择入参和出参。

下节我讲下Mybatis中如何实现一对一和一对多的查询操作

  1. 一对一查询 assocation

  2. 一对多查询collection

  • 13
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值