暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Mybatis学习之各种查询功能

原创 嘿嘿嘿 2025-03-23
174

1.测试类SelectMyBatisTest.java

import com.gbasedbt.Mybatis.entities.User;
import com.gbasedbt.Mybatis.mapper.SelectMapper;
import com.gbasedbt.Mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class SelectMyBatisTest {

    /**
     * MyBatis的各种查询功能:
     * 1.若查询出的数据只有一条
     * a> 可以通过实体类对象接收
     * b> 可以通过list集合接收
     * c> 可以通过map集合接收
     * 结果:{password=123456, sex=1, id=2, age=34, email=123456@qq.com, username=jerry}
     * 2.若查询出的数据有多条,一定不能通过实体类对象结束,此时会抛出异常TooManyResult
     * a> 可以通过list集合接收
     * b> 可用通过map类型的list集合List<Map<String, Object>>接收
     * c> MapKey()键值:在mapper接口的方法上添加@MapKey注解,此实就可以将每条数据转换的map集合作为值,以某个唯一字段的值作为键,放在同一个map
     * 集合中
     * TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3
     *
     * Mybatis中设置默认的类型别名
     * java.lang.Integer --> int,integer
     * int --> _int,_integer
     * Map --> map
     * String --> string
     */

    /**
     * 创建测试类selectUserById,根据用户id查询用户数据
     */
    @Test
    public void selectUserById() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);

        User user = mapper.selectUserById(3);

        System.out.println(user);
    }


    /**
     * 创建测试类selectAllUser,查询该表所有数据
     */
    @Test
    public void selectAllUser() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);

        List<User> users = mapper.selectAllUser();
        users.forEach(System.out::println);
    }

    /**
     * 创建测试类selectCountUser,用户查询t_user表的记录数
     */
    @Test
    public void selectCountUser() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);

        int countNum = mapper.selectCountUser();

        System.out.println(countNum);


    }

    @Test
    public void selectUserByIdToMap() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);

        System.out.println(mapper.selectUserByIdToMap(2));


    }

    @Test
    public void selectAllUserToMap() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);

        Map<String, Object> users = mapper.selectAllUserToMap();
        System.out.println(users);
    }
}

2.mapper接口SelectMapper.java

package com.gbasedbt.Mybatis.mapper;

import com.gbasedbt.Mybatis.entities.User;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface SelectMapper {

    /**
     * 根据id查询单个用户数据
     */
    User selectUserById(@Param("id") int id);

    /**
     * 查询所有用户数据
     */
    List<User> selectAllUser();

    /**
     * 查询用户表有多少条记录
     */
    int selectCountUser();

    /**
     * 使用map集合存放数据信息,条件是通过id查询
     */
    Map<String,Object> selectUserByIdToMap(@Param("id") int id);

    /**
     * 使用map集合存放数据信息
     */
    @MapKey("id")
    Map<String,Object> selectAllUserToMap();
}

3.MyBatis中的mapper映射

<?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.gbasedbt.Mybatis.mapper.SelectMapper">

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

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

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

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

<!--    Map<String,Object> selectAllUserToMap(@Param("id") int id);-->
    <select id="selectAllUserToMap" resultType="map">
        select * from t_user
    </select>
</mapper>
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论