mybatis中resulthandler的用法

网友投稿 1658 2022-11-02

mybatis中resulthandler的用法

mybatis中resulthandler的用法

目录resulthandler的用法创建实体类sql,返回值类型要写Mapper,返回值必须是voidTestResultHandler(传入map返回map)测试代码实现xml

resulthandler的用法

ResultHandler,顾名思义,对返回的结果进行处理,最终得到自己想要的数据格式或类型。也就是说,可以自定义返回类型。下面通过一个例子讲解它的使用方法:

创建实体类

package com.ccb.demossm.entity;

import com.ccb.demossm.annotation.MyAnnotation;

import org.springframework.stereotype.Component;

@Component

public class Users {

//用户ID

private String userId;

//用户名

private String userName;

//用户密码

private String userPwd;

//用户性别

public String sex;

//备注

protected String mark;

public String getMark() {

return mark;

}

public void setMark(String mark) {

this.mark = mark;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserPwd() {

return userPwd;

}

public void setUserPwd(String userPwd) {

this.userPwd = userPwd;

}

public Users() {

}

public Users(String userId) {

this.userId = userId;

}

}

sql,返回值类型要写

select u.userId,u.userName, u.userPwd from users u

Mapper,返回值必须是void

void getUserResult(ResultHandler resultHandler);package com.ccb.demossm.mapper;

import com.ccb.demossm.entity.Users;

import org.apache.ibatis.annotations.Param;

import org.apache.ibatis.session.ResultHandler;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.stereotype.Repository;

import java.util.List;

import java.util.Map;

@Repository

@SpringBootApplication

public interface UsersMapper {

void getUserResult(ResultHandler resultHandler);

}

Test

package com.ccb.demossm;

import com.ccb.demossm.entity.Users;

import com.ccb.demossm.mapper.UsersMapper;

import org.apache.ibatis.session.ResultContext;

import org.apache.ibatis.session.ResultHandler;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest

@RunWith(SpringRunner.class)

public class TestResultHandle {

@Autowired

private UsersMapper usersMapper;

@Test

public void test() {

usersMapper.getUserResult(new ResultHandler() {

@Override

public void handleResult(ResultContext extends Users> resultContext) {

Users resultObject = resultContext.getResultObject();

String userId = resultObject.getUserId();

//写数据处理业务

System.out.println(userId);

}

});

}

}

ResultHandler(传入map返回map)

测试代码

package com.xwhbshweb.service;

import java.util.HashMap;

import java.util.Map;

import org.apache.ibatis.session.ResultContext;

import org.apache.ibatis.session.ResultHandler;

public interface OrdersService {

/**

* 返回某日订单成交数及当日成交总金额 day 的格式为 2018/01/30

*

* 若 day 为 null 则返回总成交订单及总成交金额

*

* @param id

* 商户号

* @param day

* @return

*/

public Map getOrderCountTotal(String id, String day);

/**

* 处理 select 结果

*

* @author Administrator

*

*/

class OrdersResultHandler implements ResultHandler {

private final Map mappedResults = new HashMap();

@Override

public void handleResult(ResultContext resultContext) {

Map map = (Map) resultContext.getResultObject();

if (map.get("count") instanceof Long) {

System.out.println("count Long ");

if (null == String.valueOf((Long) map.get("count"))

|| String.valueOf((Long) map.get("count")).equals("")) {

mappedResults.put("count", "0");

} else {

mappedResults.put("count", String.valueOf((Long) map.get("count")));

}

}

if (map.get("total") instanceof Double) {

System.out.println("total Long ");

String total = String.valueOf((Double) map.get("total")).split("\\.")[0];

if (total.equals("0")) {

mappedResults.put("total", "0");

}else {

float yuan = Float.valueOf(total) / 1000;

mappedResults.put("total", String.valueOf(yuan));

}

}

}

public Map getMappedResults() {

return mappedResults;

}

}

}

实现

package com.xwhbshweb.service.imp;

import java.util.HashMap;

import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.springframework.stereotype.Service;

import com.xwhbshweb.config.MybatisSqlSessionFactory;

import com.xwhbshweb.service.OrdersService;

@Service

public class OrdersServiceI implements OrdersService {

private SqlSessionFactory sqlSessionFactory = MybatisSqlSessionFactory.getOne();

@Override

public Map getOrderCountTotal(String id,String day) {

SqlSession session = sqlSessionFactory.openSession(true);

Map params = new HashMap();

params.put("id", id);

if (null == day) {

params.put("day", "%");

}else {

params.put("day", day+"%");

}

OrdersService.OrdersResultHandler handler = new OrdersService.OrdersResultHandler();

session.select("com.xwhbank-.config.mybatis.namespace.ordersMapper.selectDayOrderCountTotal", params, handler);

session.close();

Map map =handler.getMappedResults();

return map;

}

}

xml

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from orders where the_super_id = #{id};

select count(*) as count,SUM(money_to_tenant) as total from orders where the_super_id = #{id} and order_time like #{day} and state='2';

insert into orders (id,tenant_order_no,the_super_id,state,money_amount,cut,money_to_tenant,money_to_xwh,pay_way,good_info,asy_notify_url,sy_return_url,sign_,order_time,sharding_id) values ("next value for MYCATSEQ_order",#{tenant_order_no},#{the_super_id},#{state},#{money_amount},#{cut},#{money_to_tenant},#{money_to_xwh},#{pay_way},#{good_info},#{asy_notify_url},#{sy_return_url},#{sign_},#{order_time},#{sharding_id});

注意传入 map参数 及 传出map参数 的颜色对应

map传入值时 若是不同类型 将 map的值改为Object就好了

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:分享一种系统事故&问题处理反馈方式(COE)
下一篇:微信公众号支付报文示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~