MyBatis五、MyBatis的各种查询功能

MyBatis的各种查询功能

一、查询一个实体类对象
  1. 其结果可被接收的方式

    可通过实体类对象接收

    可通过List集合接收:即第二种情况

    可通过Map结构:即第四种情况

  2. Mapper接口
    public interface SelectMapper {
        User getUserById(@Param("id") Integer id);
    }
    
  3. Mapper映射文件
    <mapper namespace="com.atguigu.mybatis.mapper.SelectMapper">
        <select id="getUserById" resultType="user">
            select * from t_user where id = #{id}
        </select>
    </mapper>
    
  4. 测试
    @Test
    public void getUserByIdTest() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        User userById = mapper.getUserById(1);
        // 输出结果:User{id=1, username='admin', pwd='123456', age=23, sex=男, email='12345@qq.com'}
        System.out.println(user);
    }
    
二、查询一个List集合
  1. 其结果可被接收的方式

    可通过List集合接收

    可通过Map结构

  2. Mapper接口
    public interface SelectMapper {
        List<User> getAllUser();
    }
    
  3. Mapper映射文件
    <mapper namespace="com.atguigu.mybatis.mapper.SelectMapper">
        <select id="getAllUser" resultType="user">
            select * from t_user 
        </select>
    </mapper>
    
  4. 测试
    @Test
        public void getAllUserTest() {
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
            List<User> allUser = mapper.getAllUser();
            allUser.forEach(user -> System.out.println(user));
        }
    
三、查询单个数据
  1. mybatis自带的typeAlias
    TypeIntegerStringMapListDoubleIterator
    Aliasint/integerstringmaplistdoubleiterator
  2. Mapper接口
    public interface SelectMapper {
        int getCount();
    }
    
  3. Mapper映射文件

    注:一定要有resultType

    <select id="getCount" resultType="Integer">
        select count(*) from t_user
    </select>
    
  4. 测试
    @Test
    public void getCountTest() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        int count = mapper.getCount();
        System.out.println("count = " + count);
    }
    
四、查询一条数据为Map集合
  1. mapper接口
    public interface SelectMapper {
        Map<String,Object> getUserByIdToMap(@Param("id") Integer id);
    
    }
    
  2. mapper映射文件
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>
    
  3. 测试
    @Test
    public void getUserByIdToMapTest() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> userByIdToMap = mapper.getUserByIdToMap(1);
        // 输出结果:{sex=男, id=1, pwd=123456, age=23, email=12345@qq.com, username=admin}
        System.out.println(userByIdToMap);
    }
    
五、查询多条数据为Map集合
  1. @MapKey()

    用来设置Map集合的键值对

    会将查询出来的某条数据的某一个字段作为Map集合的键,

    会将查询出来的某条数据作为Map集合的值

  2. Mapper接口
    public interface SelectMapper {
        @MapKey("id")
        Map<String,Object> getAllToMap();
    }
    
  3. Mapper映射文件
    <select id="getAllToMap" resultType="map">
        select * from t_user
    </select>
    
  4. 测试
    @Test
    public void getAllToMapTest() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> allToMap = mapper.getAllToMap();
        /*	输出结果:
        	{1={sex=男, id=1, pwd=123456, age=23, email=12345@qq.com, username=admin},
            2={sex=男, id=2, pwd=123456, age=23, email=12345@qq.com, username=admin}, 
            3={sex=男, id=3, pwd=123456, age=23, email=12345@qq.com, username=admin}, 
            4={sex=男, id=4, pwd=123456, age=23, email=12345@qq.com, username=李四},
            10={sex=女, id=10, pwd=123456, age=23, email=zhangsan@qq.com, username=张三}}
    
        */
        System.out.println(allToMap);
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e_nanxu

感恩每一份鼓励-相逢何必曾相识

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值