这篇文章主要为大家介绍了Mybatis返回Map数据方式示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
一. 方式1
接口
public interface UserMapper {
List<Map<String, String>> selectTestData1();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.mapper.UserMapper">
<select id="selectTestData1" resultType="java.util.Map">
SELECT
t_user.id as id,
t_user.email as email,
t_user.avatar as avatar
FROM
t_user
</select>
</mapper>
调用
@Service
public class MapTest implements CommandLineRunner {
@Autowired
private UserMapper mapper;
@Override
public void run(String... args) throws Exception {
List<Map<String, String>> listData1 = mapper.selectTestData1();
for (Map<String, String> map : listData1) {
System.out.println(map);
}
}
}
二. 方式2
接口
import org.apache.ibatis.annotations.MapKey;
public interface UserMapper {
// 指定的key必须是唯一的,否则重复的重复map的key会覆盖,如果查询的字段中没有唯一值,可以通过rowno来指定
@MapKey("rowno")
Map<String, Map<String,String>> selectTestData2();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.mapper.UserMapper">
<!--
❗❗❗由于mysql没有oracle中的ROWNUM功能,因此只能通过下面的方式进行模拟
-->
<select id="selectTestData2" resultType="java.util.Map">
SELECT
@rowno := @rowno + 1 AS rowno,
t_user.email AS email,
t_user.avatar AS avatar
FROM
t_user,
( SELECT @rowno := 0 ) t
ORDER BY
rowno DESC
</select>
</mapper>
调用
@Service
public class MapTest implements CommandLineRunner {
@Autowired
private UserMapper mapper;
@Override
public void run(String... args) throws Exception {
Map<String, Map<String, String>> mapData1 = mapper.selectTestData2();
System.out.println(mapData1);
}
}
三. 方式3
接口
import org.apache.ibatis.annotations.MapKey;
public interface UserMapper {
// 指定的key名称必须是User实体类中的属性
@MapKey("id")
Map<String, User> selectTestData3();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.mapper.UserMapper">
<!--由于最终是把数据封装到User实体类中,所以返回值的数据类型是User而不是Map-->
<select id="selectTestData3" resultType="com.example.demo.transactionCom.entity.User">
SELECT
t_user.id as id,
t_user.email as email,
t_user.avatar as avatar
FROM
t_user
</select>
</mapper>
调用
@Service
public class MapTest implements CommandLineRunner {
@Autowired
private UserMapper mapper;
@Override
public void run(String... args) throws Exception {
Map<String, User> mapData2 = mapper.selectTestData3();
Set<Map.Entry<String, User>> entries = mapData2.entrySet();
for (Map.Entry<String, User> entry : entries) {
User user = entry.getValue();
System.out.println(user);
}
}
}
以上就是Mybatis返回Map数据方式示例的详细内容,更多关于Mybatis返回Map数据的资料请关注编程学习网其它相关文章!
沃梦达教程
本文标题为:Mybatis返回Map数据方式示例
猜你喜欢
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- Spring Security权限想要细化到按钮实现示例 2023-03-07
- Java实现顺序表的操作详解 2023-05-19
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
- 深入了解Spring的事务传播机制 2023-06-02
- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- JSP页面间传值问题实例简析 2023-08-03
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
- Java中的日期时间处理及格式化处理 2023-04-18
- JSP 制作验证码的实例详解 2023-07-30