【MyBatis05】MyBatis的各种查询功能

MyBatis的各种查询功能
* 1.若查询的数据只有一条
* a>可以通过实体类对象接受
* b>可以通过list集合接受
* c>可以通过map集合接受(以字段值为键,以字段值为值)
* 2.若查询的数据有多条
* a>可以通过实体类类型的list集合
* b>可以通过map类型的list集合接受
* c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为值,
* 以某个字段的值作为键,放在同一个map集合中

 * 注意:一定不可以通过实体类对象接受,否则会抛出异常TooManyResultException*/

tips:
1.要确定要查询什么数据,然后才能确定返回值的类型
2.先创建mapper接口(以功能命名),再创建xml配置文件(命名空间要与mapper接口的全类名一致)

1.xml代码

<?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接口的全类名一致-->
<mapper namespace="com.sdjzu.mybatis.mapper.SelectMapper">
<!-- User getUserById(@Param("id") Integer id);-->
<!--@Param以值为键,以传入的参数为值-->
<!--select的id中不可以有空格-->
<select id="getUserById" resultType="User">
        select * from t_user where id=#{id};
    </select>
    <!--   java.util.List<User> getAllUser();-->
    <select id="getAllUser" resultType="User">
        select * from t_user
    </select>
    <!--Integer  getCount();-->
    <select id="getCount" resultType="Integer">
        select count(1) from t_user
    </select>
    <!--Map<String , Object> getUserByIdToMap();-->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id=#{id}
    </select>
    <!--java.util.List<Map<String,Object>> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
</mapper>

2.mapper接口

package com.sdjzu.mybatis.mapper;

import com.sdjzu.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<List> {
    /*
    * 根据id查询用户信息
    * */
    /*实体类对象接受*/
    User getUserById(@Param("id") Integer id);
    /*list集合接受*/
    /*List<User> getUserById(@Param("id") Integer id);*/
    /*
    * 查询使用用户信息*/
    java.util.List<User> getAllUser();
    /*查询用户记录*/
    Integer  getCount();
    /*根据id查询用户信息用map集合接受*/
    Map<String , Object> getUserByIdToMap(@Param("id") Integer id);
   /*
   * 查询所用用户信息,用map类型的list接受*/
   /*java.util.List<Map<String,Object>> getAllUserToMap();*/
    @MapKey("id")//用id作为键,用转化为的map作为值
    Map<String,Object> getAllUserToMap();
}

3.测试类

package com.sdjzu.mybatis.test;

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

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

public class SelectMapperTest {
    /*
    * MyBatis的各种查询功能
    * 1.若查询的数据只有一条
    * a>可以通过实体类对象接受
    * b>可以通过list集合接受
    * c>可以通过map集合接受(以字段值为键,以字段值为值)
    * 2.若查询的数据有多条
    * a>可以通过实体类类型的list集合
    * b>可以通过map类型的list集合接受
    * c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为值,
    * 以某个字段的值作为键,放在同一个map集合中

     * 注意:一定不可以通过实体类对象接受,否则会抛出异常TooManyResultException*/
    @Test
    public void testGetUserById(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        User userById = mapper.getUserById(1);
        System.out.println("userById = " + userById);
    }
    @Test
    public void testGetAllUser(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<User> allUser = mapper.getAllUser();
        for(User user:allUser){
            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 = " + count);
    }
    @Test
    public void testGetUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map userByIdToMap = mapper.getUserByIdToMap(1);
        System.out.println("userByIdToMap = " + userByIdToMap);
    }
 /*   @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<Map<String,Object>> ballUserToMap = mapper.getAllUserToMap();
        for(Map<String ,Object> map:ballUserToMap){
            System.out.println("map = " + map);
        }
    }*/
/*
 @Test
 public void testGetAllUserToMap(){
     SqlSession sqlSession = SqlSessionUtils.getSqlSession();
     SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
     Map<String,Object> ballUserToMap = mapper.getAllUserToMap();
     for(String map:ballUserToMap.keySet()){
         System.out.println(map+"=="+ballUserToMap.get(map));
     }
 }
*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus是Mybatis的增强工具,在Mybatis的基础上进行了功能扩展和封装,提供了更加便捷的操作方式。 Mybatis-Plus提供了lambda表达式和条件构造器两种方式进行条件查询,其中条件构造器是Mybatis-Plus最常用的方式,其可以灵活的构造复杂的查询条件。 下面以条件分页查询为例,介绍Mybatis-Plus的条件构造器的用法。 1. 引入Mybatis-Plus依赖 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> ``` 2. 创建条件构造器 ```java QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ``` 3. 添加查询条件 ```java queryWrapper.eq("name", "张三") //等于 .ne("age", 20) //不等于 .gt("age", 18) //大于 .lt("age", 30) //小于 .ge("create_time", "2021-01-01") //大于等于 .le("create_time", "2021-06-30") //小于等于 .like("email", "@qq.com") //模糊查询 .in("id", Arrays.asList(1, 2, 3)) //in查询 .isNull("phone") //为空 .isNotNull("address") //不为空 .orderByAsc("age"); //升序排序 ``` 4. 执行分页查询 ```java Page<User> page = new Page<>(1, 10); //查询第1页,每页10条记录 IPage<User> userPage = userService.page(page, queryWrapper); List<User> userList = userPage.getRecords(); //获取查询结果列表 long total = userPage.getTotal(); //获取查询总记录数 ``` 其中,`page`表示分页信息,`userService`表示Mybatis-Plus生成的service,`queryWrapper`表示查询条件。执行`userService.page(page, queryWrapper)`即可执行分页查询。 以上就是Mybatis-Plus条件分页查询的基本用法。Mybatis-Plus还提供了很多其他的查询方式和操作方式,具体可以参考官网文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值