0.前言
mybaits中sql映射文件是一个xml文件,里面记录的和数据库交互的各种信息,相当于sql语句,在写这些语句的时候,遇到很多不同的参数,比如resultType,这篇文章简单介绍下resultType。
resultType是返回的类型。实际上是resultType将查询到结果映射封装成pojo类型中,前提是该pojo类的属性名和查询到的数据库表的字段名一致。这种映射封装mybatis帮我们自动做好了,不需要我们自己考虑。所以看起来我们是我们查询一些数据,然后返回类型是pojo。实际上内部映射封装不需要我们实现。
本文github地址 (注:数据库自己配置,或者看sql文件导入)
1.resultType解析
resultType是你sql语句的返回值类型。有以下不同的用法
- 基本类型
- JavaBean类型
- List类型
- Map类型
其中,个人认为JavaBean和List很类似,可能是知识面不够,感觉要结合起来用
1.基本类型举例:
场景:比如返回数据库中某一个的字段值
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cat.dao.StudentsMapper">
<!-- 指定 resultType 返回值类型时 String 类型的,-->
<!-- string 在这里是一个别名,代表的是 java.lang.String-->
<select id="listAllStudents" resultType="String">
select * from students where id = #{id};
2.JavaBean类型
场景:查询数据库中一条信息,把这条信息封装成某个JavaBean类型。
比如,我的pojo中定义了一个Students
然后在xml文件中的resultType可以这样写
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cat.dao.StudentsMapper">
<select id="listAllStudents" resultType="com.cat.pojo.Students">
3.List类型
场景:有时候我们要查询的数据不止一条,比如:模糊查询,全表查询等,这时候返回的数据可能不止是一条数据,对于多数据的处理可以存放在List
集合中。
Students接口:
StudentsMapper接口:
import com.cat.pojo.Students;
public interface StudentsMapper {
List<Students> listAllStudents();
StudentsMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cat.dao.StudentsMapper">
<select id="listAllStudents" resultType="com.cat.pojo.Students">
4.Map类型
场景:如果查询的结果是一条,我们可以把查询的数据以{表字段名, 对应的值}
方式存入到Map
中。
StudentsMapper
import com.cat.pojo.Students;
public interface StudentsMapper {
List<Students> listAllStudents();
public Map<String, Object> getStudent(int id);
StudentsMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cat.dao.StudentsMapper">
<select id="listAllStudents" resultType="com.cat.pojo.Students">
<select id="getStudent" resultType="map">
select *from students where id= #{id}
test文件
import com.cat.dao.StudentsMapper;
import com.cat.pojo.Students;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MyBatisTest {
public void getStudent() throws IOException{
InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession session =factory.openSession();
StudentsMapper studentsMapper = session.getMapper(StudentsMapper.class);
Map<String,Object> map = studentsMapper.getStudent(1000);
结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具