首页 > 其他分享 >spring和Mybatis的各种查询

spring和Mybatis的各种查询

时间:2024-06-09 10:58:33浏览次数:19  
标签:deptId 映射 spring 查询 dept emp Mybatis id

目录

、MyBatis的各种查询功能

6.1、查询一个实体类的对象

6.2、查询一个list集合

  • 当查询的数据为多条记录时,不能使用实体类作为返回值,否则会抛出异常:TooManyResultsException,但是如果查询的数据只有一条,可以使用实体类或集合作为返回值

6.3、查询单个数据

6.4、查询一条数据为Map集合

6.5、查询多条数据为Map集合

  1. 方式一

    • 将表中的数据以map集合的方式查询,一条数据对应一个map;如果有多条数据,就会产生多个map集合,此时可以将这些map放在一个list集合中获取
  2. 方式二

    • 如果有多条数据,可以将每条数据转换的map集合放在一个大的map集合中,但是必须要通过@MapKey注解来完成。将查询的某个字段的值作为大的Map集合的键(key)

七、特殊SQL的执行

7.1、模糊查询

    <select id="方法名" resultType="返回值类型">
        SELECT *
        from user
        where username like "%"#{name}"%"
    </select>

7.2、批量删除

    <delete id="deleteAll">
        delete
        from user
        where id in #{ids}
    </delete>

7.3、动态设置表名

    <select id="getUserList" resultType="object">
        SELECT *
        from ${tableName}
    </select>

7.4、添加功能获取自增的主键

    <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO user (username, password)
        VALUES (#{username}, #{password})
    </insert>

八、自定义映射resultMap 一对一、多对一、一对多、多对多

8.1、resultMap处理字段和属性的映射关系

如果字段名和实体类中的属性名不一致的情况下,可以通过resultMap设置自定义映射。

  1. 可以通过为字段起别名的方式,别名起成和属性名一致。保证字段名和实体类中的属性名一致
<select id="getEmpByEmpId" resultType="emp">
    select emp_id empId,emp_name empName,age,sex from emp where emp_id = #{empId}
</select>

  1. 如果字段名和实体类中的属性名不一致的情况下,但是字段名符合数据库的规则(使用_),实体类中使用的属性名符合java的规则(使用驼峰命名),可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中的数据时,自动将带下划线“_”的字段名转为驼峰命名
    • user_name:userName
    • emp_id:empId
<settings>
    <!--将数据库字段名的下划线映射为驼峰-->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--Emp getEmpByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpByEmpId" resultType="emp">
    select * from emp where emp_id = #{empId}
</select>
  1. 使用resutlMap自定义映射处理
<!--
    resultMap:设置自定义的映射关系
    id:唯一标识
    type:处理映射关系的实体类的类型 一般使用MyBatis的别名
    常用的标签
    id:处理主键和实体类中属性的映射关系
    result:处理普通字段和实体类中属性的映射关系
    column:设置映射关系中的字段名,必须是sql查询出的某个字段
    property:设置映射关系中的属性的属性名,必须是处理的实体类型中的属性名
-->
<resultMap id="empReslutMap" type="emp">
    <id property="empId" column="emp_id"></id>
    <result property="empName" column="emp_name"></result>
    <result property="age" column="age"></result>
    <result property="sex" column="sex"></result>
</resultMap>

8.2、一对一映射处理

人与身份证号

  1. 级联方式处理
/**
 * 根据id查询人员信息
 * @param id
 * @return
 */
Person findPersonById(@Param("id") Integer id);

<!-- Person findPersonById(Integer id);-->
<select id="findPersonById" resultMap="IdCardWithPersonResult">
    SELECT person.*,idcard.code
    FROM person,idcard
    WHERE person.card_id=idcard.id AND person.id=#{id}
</select>

<resultMap id="IdCardWithPersonResult" type="person">
    <id property="id" column="id"></id>
    <result property="name" column="name"></result>
    <result property="age" column="age"></result>
    <result property="sex" column="sex"></result>
    <result property="card.id" column="id"></result>
    <result property="card.code" column="code"></result>
</resultMap>

  1. association
<resultMap id="IdCardWithPersonResult2" type="person">
    <id property="id" column="id"></id>
    <result property="name" column="name"></result>
    <result property="age" column="age"></result>
    <result property="sex" column="sex"></result>
    <association property="card" javaType="IdCard">
        <id property="id" column="id"></id>
        <result property="code" column="code"></result>
    </association>
</resultMap>

  1. 分步查询
<!--分步查询第一步-->
<!-- Person findPersonById3(@Param("id") Integer id);-->
<select id="findPersonById3" resultMap="IdCardWithPersonResult3">
    select * from person where id=#{id}
</select>

<resultMap id="IdCardWithPersonResult3" type="person">
    <id property="id" column="id"></id>
    <result property="name" column="name"></result>
    <result property="age" column="age"></result>
    <result property="sex" column="sex"></result>

    <!--
    association:处理一对一或者多对一的映射关系(处理的实体类类型的属性)
    property:设置需要处理映射关系的属性的属性名
    javaType:设置要处理的属性的类型
    column:第一步传递给第二步的字段,映射关系的表关联的字段
    -->
    <association property="card" javaType="IdCard" column="card_id"
                 select="com.qcby.mybatis.mapper.IdCardMapper.findCodeById">

    </association>
</resultMap>

<!--分步查询的第二步-->
<!--IdCard findCodeById(@Param("id") Integer id);-->
<select id="findCodeById" resultType="idcard">
    SELECT * from idcard where id=#{id}
</select>

8.3、多对一映射处理

场景模拟:

查询员工信息以及员工所对应的部门信息

使用resultMap自定义映射处理
处理多对一的映射关系:

  • 级联方式处理

  • association

  • 分步查询

  1. 级联方式处理
/**
 * 获取员工以及所对应的部门信息
 * @param empId
 * @return
 */
Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);

<!-- Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
    SELECT emp.*,dept.*
    FROM emp
    LEFT JOIN dept
    ON emp.dept_id=dept.dept_id
    WHERE emp.emp_id=#{empId}
</select>

<resultMap id="empAndDeptResultMap" type="emp">
     <id column="emp_id" property="empId"></id>
     <result column="emp_name" property="empName"></result>
     <result column="age" property="age"></result>
     <result column="sex" property="sex"></result>
     <result column="dept_id" property="dept.deptId"></result>
     <result column="dept_name" property="dept.deptName"></result>
 </resultMap>

@Test
public void testGetEmpAndDeptByEmpId(){
    Emp emp = empMapper.getEmpAndDeptByEmpId(1);
    System.out.println(emp);
}

  1. association
<resultMap id="empAndDeptResultMap" type="emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="sex" property="sex"></result>
   <association property="dept" javaType="dept">
       <id column="dept_id" property="deptId"/>
       <result column="dept_name" property="deptName"></result>
   </association>
</resultMap>

  1. 分步查询

  2. 根据员工id查询员工信息

emp接口中

/**
 * 通过分步查询来查询员工以及所对应部门信息的第一步
 * @param empId
 * @return
 */
Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);

dept接口中

/**
 * 通过分步查询来查询员工以及所对应部门信息的第二步
 * @param deptId
 * @return
 */
Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);

emp映射文件中

<!--Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
    select * from emp where emp_id = #{empId}
</select>

<resultMap id="empAndDeptByStepResultMap" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="sex" property="sex"></result>

    <association property="dept" column="dept_id"
                 select="com.qcby.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                 ></association>
</resultMap>

dept映射文件中

<!--Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
    select * from dept where dept_id = #{deptId}
</select>

测试

@Test
public void testGetEmpAndDeptByStepOne(){
    Emp emp = empMapper.getEmpAndDeptByStepOne(1);
    System.out.println(emp);
}

分步查询的优点:可以实现延迟加载(懒加载),但是必须在核心配置文件中设置全局配置信息:

lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有管理对象都会延迟加载

aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载,此时就可以实现按需加载,获取的数据是什么,就会执行相应的sql语句

此时可以通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType=“lazy(延迟加载)|eager(立即加载)”

8.4、一对多映射处理 部门 -->员工

没有级联方式的查询,只有collection 和分步查询

8.4.1 collection

dept接口

/**
 * 查询部门以及部门中的员工信息
 * @param deptId
 * @return
 */
Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);

dept映射文件中

<!--Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
    SELECT *
    FROM dept
    LEFT JOIN emp
    ON dept.dept_id=emp.dept_id
    WHERE dept.dept_id=#{deptId}
</select>

<resultMap id="deptAndEmpResultMap" type="dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>
    <!--ofType:设置集合类型的属性中存储的数据的类型-->
    <collection property="emps" ofType="emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="sex" property="sex"></result>
    </collection>
</resultMap>

测试方法

@Test
public  void testGetDeptAndEmpByDeptId(){
    Dept dept = deptMapper.getDeptAndEmpByDeptId(1);
    System.out.println(dept);
}

8.4.1 分步查询

⑴查询部门信息

/**
 * 通过分步查询进行查询部门及部门中的员工信息的第一步:查询部门信息
 * @param deptId
 * @return
 */
Dept getDeptAndEmpBySetpOne(@Param("deptId") Integer deptId);

<!-- Dept getDeptAndEmpBySetpOne(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpBySetpOne" resultMap="deptAndEmpResultMapByStep">
    select * from dept where dept_id = #{deptId}
</select>

<resultMap id="deptAndEmpResultMapByStep" type="dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>

    <collection property="emps" column="dept_id"
                select="com.qcby.mybatis.mapper.EmpMapper.getDeptAndEmpBySetpTwo"></collection>
</resultMap>

⑵查询员工信息

/**
 * 通过分步查询进行查询部门及部门中的员工信息的第二步:查询员工信息
 * @param deptId
 * @return
 */
List<Emp> getDeptAndEmpBySetpTwo(@Param("deptId")Integer deptId);

<!-- List<Emp> getDeptAndEmpBySetpTwo(@Param("deptId")Integer deptId);-->
<select id="getDeptAndEmpBySetpTwo" resultType="emp">
    select  * from  emp where dept_id = #{deptId}
</select>

⑶测试方法

@Test
public void testGetDeptAndEmpBySetp(){
    Dept dept = deptMapper.getDeptAndEmpBySetpOne(2);
    System.out.println(dept);
}

8.5、多对多映射关系

8.5.1、pojo

8.5.2、collection

8.5.3 分步查询

⑴查询订单信息

/**
 * 通过分步查询进行查询订单以及订单中的商品信息的第一步
 * @param id
 * @return
 */
List<Orders>  findOrdersWithProduct2(@Param("id") Integer id);

<!-- List<Orders>  findOrdersWithProduct2(@Param("id") Integer id);-->
<select id="findOrdersWithProduct2" resultMap="OrdersWithProductResult2">
    select * from  orders where id = #{id}
</select>
<resultMap id="OrdersWithProductResult2" type="orders">
    <id column="id" property="id"></id>
    <result column="number" property="number"></result>

    <collection property="productList" column="id" ofType="product"
                select="com.qcby.mybatis.mapper.ProductMapper.findProductById">
    </collection>
</resultMap>

⑵查询商品信息

/**
 * 通过分步查询进行查询订单以及订单中的商品信息的第二步
 * @param id
 * @return
 */
List<Product> findProductById(@Param("id") Integer id);

<!--List<Product> findProductById(@Param("id") Integer id);-->
<select id="findProductById" resultType="product">
    select * from product where id in(
        select product_id from ordersitem where orders_id = #{id}
    )
</select>

⑶测试

@Test
public void testFindOrdersWithProduct2(){
    List<Orders> orders = ordersMapper.findOrdersWithProduct2(1);
    orders.forEach(System.out::println);
}

标签:deptId,映射,spring,查询,dept,emp,Mybatis,id
From: https://www.cnblogs.com/ning23/p/18239329

相关文章

  • springboot启动过程、自动装配原理、内置Tomcat启动原理
    一、springboot的启动原理springboot的启动通过在main方法的SpringApplication.run()方法启动@SpringBootApplicationpublicclassShuaApplication{publicstaticvoidmain(String[]args){SpringApplication.run(ShuaApplication.class,args);}}......
  • postgresql 基本查询
    建表语句--========sys_dict_typecreatetablesys_dict_type(idbigintprimarykey,namevarchar(100),typevarchar(100),group_codevarchar(100),statuschar(1));commentontablesys_dict_typeis'系统字典类型表';commentoncolumnsys_dict_type.nam......
  • SpringBoot架构图
    文章目录前言一、SpringBoot架构模块二、SpringBoot架构图总结前言提示:这里可以添加本文要记录的大概内容:例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。提示:以下是本篇文章正文内容,下面案......
  • Java项目-基于springboot+vue的音乐网站与分享平台 (源码+数据库+文档)​
    如需完整项目,请私信博主基于SpringBoot+Vue的音乐网站与分享平台开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven音乐网站与分享平台的主要使用者分为管理员和用户,实现功能包括管理员:首页、个人中心、用户管理、音乐资讯管理、音乐......
  • Java项目-基于springboot+vue的影城管理系统 (源码+数据库+文档)​
    如需完整项目,请私信博主基于SpringBoot+Vue的影城管理系统开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven影城管理系统的主要使用者分为管理员和用户,实现功能包括管理员:首页、个人中心、用户管理、电影类型管理、放映厅管理、电影......
  • MyBatis代码生成器的实用
    MyBatis代码生成器可以帮助我们快速的构建一些固定的代码重要:代码生成器生成的代码不能被修改,否则在新增新的字段以后,再次运行代码生成器,修改后的内容可能会丢失在项目中引入代码生成器的主要步骤:新建生成器模块,引入相关插件<!--mybatisgenerator自动生成代码插件-->......
  • Springboot计算机毕业设计疫情下的社区居民管理系统小程序【附源码】开题+论文+mysql+
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景在新冠疫情的影响下,社区管理面临着前所未有的挑战。疫情的快速传播要求社区具备更高效、更精准的管理手段,以保障居民的健康与安全。传统的社区管理方......
  • Springboot计算机毕业设计疫情下社区资源分配小程序【附源码】开题+论文+mysql+程序+
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景在全球新冠疫情的背景下,社区作为防控疫情的重要阵地,面临着巨大的挑战。随着疫情的持续,社区资源的分配和管理变得尤为重要。然而,传统的资源分配方式往......
  • 基于SpringBoot+Vue的学院党员管理系统设计与实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示项目运行截图技术框架后端采用SpringBoot框架前端框架Vue可行性分析系统测试系统测试的目的系统功能测试数据库表设计代码参考数据库脚本为什么选择我?获取源码前言......
  • 基于SpringBoot+Vue的小型企业办公自动化系统设计与实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示项目运行截图技术框架后端采用SpringBoot框架前端框架Vue可行性分析系统测试系统测试的目的系统功能测试数据库表设计代码参考数据库脚本为什么选择我?获取源码前言......