首页 > 其他分享 >MyBatis的各种查询功能

MyBatis的各种查询功能

时间:2022-09-25 01:12:14浏览次数:50  
标签:map 功能 -- 查询 集合 user MyBatis id

1、查询一个实体类对象

/**
* 根据用户id查询用户信息
* @param id
* @return
*/
User getUserById(@Param("id") int id);
<!--User getUserById(@Param("id") int id);-->
<select id="getUserById" resultType="User">
select * from t_user where id = #{id}
</select>

2、查询一个list集合

/**
* 查询所有用户信息
* @return
*/
List<User> getUserList();
<!--List<User> getUserList();-->
<select id="getUserList" resultType="User">
select * from t_user
</select>

3、查询单个数据

/**
* 查询用户的总记录数
* @return
* 在MyBatis中,对于Java中常用的类型都设置了类型别名
* 例如:java.lang.Integer-->int|integer
* 例如:int-->_int|_integer
* 例如:Map-->map,List-->list
*/
int getCount();
<!--int getCount();-->
<select id="getCount" resultType="_integer">
select count(id) from t_user
</select>

4、查询一条数据为map集合

/**
* 根据用户id查询用户信息为map集合
* @param id
* @return
*/
Map<String, Object> getUserToMap(@Param("id") int id);
<!--Map<String, Object> getUserToMap(@Param("id") int id);-->
<select id="getUserToMap" resultType="map">
select * from t_user where id = #{id}
</select>
<!--结果:{password=123456, sex=男, id=1, age=23, username=admin}-->

5、查询多条数据为map集合

方式一:

/**
* 查询所有用户信息为map集合
* @return
* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,此
时可以将这些map放在一个list集合中获取
*/
List<Map<String, Object>> getAllUserToMap();
<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>

方式二:

/**
* 查询所有用户信息为map集合
* @return
* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,并
且最终要以一个map的方式返回数据,此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的
map集合
*/
@MapKey("id")
Map<String, Object> getAllUserToMap();
<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
结果:
<!--
{
1={password=123456, sex=男, id=1, age=23, username=admin},
2={password=123456, sex=男, id=2, age=23, username=张三},
3={password=123456, sex=男, id=3, age=23, username=张三}
}
-->

根据接口的类型,然后在映射文件中的resultType中填写就行

List<Map<String, Object>> getAllUserToMap() 

<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>

这里可以看到接口的类型是List<Map<String, Object> 但是resultType="map",也就是说查询的结果类型依旧是map,将数据存了map中,自动封装到list中

 

 代理模式,通过反射得到的返回值类型list,然后将数据存入list中

 

实例

public interface SelectMapper {

    /**
     * 根据id查询用户信息
     */
    List<User> getUserById(@Param("id") Integer id);

    /**
     * 查询所有的用户信息
     */
    List<User> getAllUser();

    /**
     * 查询用户信息的总记录数
     */
    Integer getCount();

    /**
     * 根据id查询用户信息为一个map集合
     */
    Map<String, Object> getUserByIdToMap(@Param("id") Integer id);

    /**
     * 查询所有用户信息为map集合
     */
    //List<Map<String, Object>> getAllUserToMap();
    @MapKey("id")
    Map<String, Object> getAllUserToMap();

}
<?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="com.atguigu.mybatis.mapper.SelectMapper">

    <!--User getUserById(@Param("id") Integer id);-->
    <select id="getUserById" resultType="User">
        select * from t_user where id = #{id}
    </select>

    <!--List<User> getAllUser();-->
    <select id="getAllUser" resultType="User">
        select * from t_user
    </select>

    <!--Integer getCount();-->
    <select id="getCount" resultType="_int">
        select count(*) from t_user
    </select>

    <!--Map<String, Object> getUserByIdToMap(@Param("id") Integer id);-->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>

    <!--Map<String, Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>

</mapper>
public class SelectMapperTest {

    /**
     * MyBatis的各种查询功能:
     * 1、若查询出的数据只有一条
     * a>可以通过实体类对象接收
     * b>可以通过list集合接收
     * c>可以通过map集合接收
     * 结果:{password=123456, sex=男, id=3, age=23, [email protected], username=admin}
     * 2、若查询出的数据有多条
     * a>可以通过实体类类型的list集合接收
     * b>可以通过map类型的list集合接收
     * c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为值,以某个字段的值作为键,放在同一个map集合中
     * 注意:一定不能通过实体类对象接收,此时会抛异常TooManyResultsException
     *
     * MyBatis中设置了默认的类型别名
     * java.lang.Integer-->int,integer
     * int-->_int,_integer
     * Map-->map
     * String-->string
     */

    @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getAllUserToMap());
    }

    @Test
    public void testGetUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getUserByIdToMap(3));
    }

    @Test
    public void testGetCount(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getCount());
    }

    @Test
    public void testGetAllUser(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<User> user = mapper.getAllUser();
        System.out.println(1);
    }

    @Test
    public void testGetUserById(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getUserById(3));
    }

}

 

标签:map,功能,--,查询,集合,user,MyBatis,id
From: https://www.cnblogs.com/dzs894330350/p/16727107.html

相关文章

  • springboot中使用mybatisplus自带插件实现分页
    springboot中使用mybatisplus自带插件实现分页1.导入mybatisplus分页依赖<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-......
  • MyBatis获取参数值的两种方式(重点)
    MyBatis获取参数值的两种方式:${}和#{}${}的本质就是字符串拼接,#{}的本质就是占位符赋值${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,......
  • Mybatis的二级缓存
      二级缓存是SqlSessionFactory级别,通过同一个SqlSessionFactory创建的SqlSession查询的结果会被缓存;此后若再次执行相同的查询语句,结果就会从缓存中读取二级缓存开......
  • 关于mybatis-plus字段更新策略的颗粒度问题
    1.三个可以控制字段更新策略的地方及颗粒度区分1.1mybtis-plus全局配置1.2entity的注解1.3baseMapper.update方法的指定set注意:必须判空set,否则非空时出现两次set语......
  • [Jetpack Compose] 使用 Snackbar 实现退出应用再确认功能
    @OptIn(ExperimentalMaterial3Api::class)@ComposablefunDemoFun(){varscope=rememberCoroutineScope()varsnackbarHostState=remember{SnackbarHos......
  • Jupyter Notebook安装代码提示功能
    默认Jupyter Notebook没有安装代码提示功能,但是我们可以可通过如下命令安装和配置使得Jupyter Notebook具备代码提供功能。(确保Anaconda在环境变量里)1、电脑上搜索“Ana......
  • 10、整合Mybatis框架
    mybatis中文文档:https://blog.csdn.net/qq_41182402/article/details/121281405UserMapper.xmlsql语句点击查看代码<?xmlversion="1.0"encoding="UTF-8"?><!DOC......
  • thinkphp 分页查询中order排序失效原因
    $this->db->order("positiondesc")->paginate(2);一个排序功能,根据某字段的大小值排序,发现order失效;原因如下:object(think\paginator\driver\Bootstrap)#20(9){......
  • std::bind()功能学习
    转自:https://blog.csdn.net/Jxianxu/article/details/1073820491.介绍std::bind的头文件是<functional>,它是一个函数适配器,接受一个可调用对象(callableobject),生成一个......
  • 光电功能材料--热点名词02
    1分立中心发光体中被激发的电子跃迁回基态(或与空穴复合)发射出光子的特定中心。他们可以是组成基质的离子、离子团或掺入的杂质。如果被激发的电子没有离开中心而回到基......