栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

MyBatis各种查询功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis各种查询功能

* MyBatis的各种查询功能:
* 1、若查询出的数据只有一条
* a>可以通过实体类对象接收
* b>可以通过list集合接收
* c>可以通过map集合接收
* 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, 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
一、Mapper接口
package com.atguigu.mybatis.mapper;

import com.atguigu.mybatis.pojo.User;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;

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

public interface SelectMapper {

    
    User getUserById(@Param("id") Integer id);

    
    List getAllUser();

    
    Integer getCount();

    
    Map getUserByIdToMap(@Param("id") Integer id);

    
//    List> getAllUserToMap();
    @MapKey("id")
    Map getAllUserToMap();




}
二、Mapper.xml接口映射文件





    
        select * from t_user
    


    
        select * from t_user where id = #{id}
    


    


三、测试
package com.atguigu.mybatis.test;

import com.atguigu.mybatis.mapper.SelectMapper;
import com.atguigu.mybatis.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.SqlSessionUtils;

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

public class SelectMapperTest {

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

    @Test
    public void testGetAllUser(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List list = mapper.getAllUser();
//        list.forEach(System.out :: println);
        list.forEach(user -> System.out.println(user));
    }

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

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

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

}

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1036574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号