注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码。
在 IService 接口中提供了很多以 get 开头的方法,这些方法用来从数据库中获取一条记录,如:根据ID获取数据记录。当数据库返回多于一条数据,则抛出错误信息。方法定义如下:
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);参数说明:
id:数据库主键 ID
queryWrapper:实体对象封装操作类 QueryWrapper
throwEx:有多个 result 是否抛出异常
entity:实体对象
mapper:转换函数
(1)本示例将根据数据ID、 QueryWrapper、Map 条件查询数据。代码如下:
package com.hxstrive.mybatis_plus.simple_service.get;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.model.UserBean;
import com.hxstrive.mybatis_plus.service.UserService;
import org.junit.jupiter.api.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;
import javax.jws.soap.SOAPBinding;
import java.util.Map;
import java.util.function.Function;
@RunWith(SpringRunner.class)
@SpringBootTest
class Get1Test {
@Autowired
private UserService userService;
@Test
void contextLoads() {
System.out.println("===================== getById ===================");
System.out.println( userService.getById(1) );
System.out.println("===================== getOne ===================");
QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", 2);
System.out.println( userService.getOne(wrapper) );
System.out.println("===================== getMap ===================");
Map<String, Object> resultMap = userService.getMap(wrapper);
System.out.println(JSONObject.toJSONString(resultMap));
System.out.println("===================== getObj ===================");
userService.getObj(wrapper, new Function<Object, UserBean>() {
@Override
public UserBean apply(Object o) {
System.out.println(JSONObject.toJSONString(o));
return null;
}
});
}
}(2)通过指定 throwEx=true 参数,然后测试当返回多条数据时是否抛出异常。代码如下:
package com.hxstrive.mybatis_plus.simple_service.get;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.model.UserBean;
import com.hxstrive.mybatis_plus.service.UserService;
import org.junit.jupiter.api.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;
@RunWith(SpringRunner.class)
@SpringBootTest
class Get2Test {
@Autowired
private UserService userService;
@Test
void contextLoads() {
QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.lt("user_id", 10);
// 第二个参数
// 如果返回的结果集多余一条数据,则抛出如下错误:
// TooManyResultsException
UserBean userBean = userService.getOne(wrapper, true);
System.out.println(userBean);
}
}
6
谢谢支持!