学习目的:学习使用注解方式实现多对一查询,查询出所有的Product,通过product_中的cid查询到所属的Category。

Part 1

mapper

CategoryMapper

package cn.vaefun.mapper;import cn.vaefun.pojo.Category;
import org.apache.ibatis.annotations.*;import java.util.List;public interface CategoryMapper {@Insert(" insert into category_ ( name ) values (#{name}) ")public int add(Category category);@Delete("delete from category_ where id=#{id}")public void delete(int id);@Select("select * from category_ where id= #{id} ")public Category get(int id);@Update("update category_ set name=#{name} where id=#{id}")public int update(Category category);@Select(" select * from category_ ")@Results({@Result(property = "id", column = "id"),@Result(property = "products", javaType = List.class, column = "id", many = @Many(select = "cn.vaefun.mapper.ProductMapper.listByCategory") )})public List<Category> list();
}

ProductMapper

package cn.vaefun.mapper;import cn.vaefun.pojo.Product;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;import java.util.List;public interface ProductMapper {@Select("select * from product_ where cid=#{cid}")public List<Product> listByCategory(int cid);@Select("select * from product_")@Results({@Result(property = "category",column = "cid",one = @One(select = "cn.vaefun.mapper.CategoryMapper.get"))})public List<Product> productList();
}

Part 2

在mybatis-config.xml中配置所需的映射:

<mapper class="cn.vaefun.mapper.CategoryMapper"/>
<mapper class="cn.vaefun.mapper.ProductMapper"/>

Part 3

测试代码块:

    ProductMapper productMapper = session.getMapper(ProductMapper.class);.../*** 注解方式多对一查询* @param productMapper*/private static void listCategoryByProduct(ProductMapper productMapper){List<Product> productList = productMapper.productList();for (Product p :productList) {System.out.println(p+"t对应的分类:t"+p.getCategory());}}

测试结果:

mybatis 一对多查询_Java自学之mybatis:使用注解方式多对一查询相关推荐

  1. mybatis like模糊查询_Java自学之mybatis:模糊查询和多条件查询

    学习目的 使用mybatis进行模糊查询:查找category_表中包含cat的记录. 使用mybatis进行多条件查询:查找category_表中id>1,包含cat的记录. Part 1 模 ...

  2. mybatis 大量insert 出现sql io 异常_Java自学之mybatis:使用注解方式实现CURD

    学习目的:在使用配置文件实现CURD的基础上,学会使用注解方式实现CURD. Part 1 新建一个Mapper,在相应的增删改查的方法上添加相应的注释以及SQL语句.与普通方法相比,就是讲普通方法中 ...

  3. 并发查询_java 手写并发框架(一)异步查询转同步的7种实现方式

    序言 本节将学习一下如何实现异步查询转同步的方式,共计介绍了 7 种常见的实现方式. 思维导图如下: 思维导图 异步转同步 业务需求 有些接口查询反馈结果是异步返回的,无法立刻获取查询结果. 比如业务 ...

  4. java下拉框查询_Java Swing应用程序 JComboBox下拉框联动查询

    在web项目中,通过下拉框.JQuery和ajax可以实现下拉框联动查询. 譬如说,当你查询某个地方时,页面上有:省份: 市区: 县乡: 街道: 查询 譬如说,你选择了省:江苏省,那么在市区中只会显示 ...

  5. MyBatis注解实现like模糊查询

    问题描述 SpringBoot下使用mybatis注解方式进行模糊查询时发生错误,结果报错:Parameter index out of range (1 > number of paramet ...

  6. Mybatis执行流程分析_自定义简易Mybatis框架

    自定义简易Mybatis框架 Mybatis执行流程分析 Mybatis代码编写流程: Mybatis配置文件加载过程: 需求分析及技术概述 根据上述的功能结构图, 得出如下需求: 1. 需要具有配置 ...

  7. mybatis java类注解式_Spring整合Mybatis注解方式

    Spring整合Mybatis(注解方式) 环境准备 jar包: Spring所需依赖:spring-context.spring-aspects.aspectjrt.aspectjweaver.sp ...

  8. mybatis一对多关联 创建_MyBatis多对多关联查询(级联查询)

    其实,MyBatis 没有实现多对多级联,这是因为多对多级联可以通过两个一对多级联进行替换. 例如,一个订单可以有多种商品,一种商品可以对应多个订单,订单与商品就是多对多的级联关系,使用一个中间表(订 ...

  9. mybatis一对多关联查询_一对一,一对多,多对多查询及延迟加载(N+1问题)分析

    推荐学习 重识SSM,"超高频面试点+源码解析+实战PDF",一次性干掉全拿走 全网独家的"MySQL高级知识"集合,骨灰级收藏,手慢则无 "吃&qu ...

最新文章

  1. 发表国外期刊注意事项
  2. 关于Python的编译
  3. pyspark读取json文件中的内容
  4. Cloud for Customer的前台请求是怎么发送到后台的
  5. java 检测硬盘原理_深入Java核心 Java内存分配原理精讲
  6. 【转】阿里技术专家详解DDD系列 第二讲 - 应用架构
  7. js条件判断时隐式类型转换
  8. C++中this指针的用法
  9. 用于对Compound协议进行无Gas投票和委托的社区工具发布
  10. 人工智能究竟会不会让程序员失业?
  11. 用例驱动的需求过程实践
  12. SSIM公式:结构相似性计算原理,基于SSIM的图像质量评价
  13. 阿里云Dataworks平台应用
  14. ABB机器人FOR循环指令
  15. 三维激光扫描后处理软件_三维激光扫描——钢结构形变检测的利器
  16. 有什么好的搜图软件推荐吗?
  17. AQS框架原文翻译 - The java.util.concurrent Synchronizer Framework
  18. 聚币网API使用教程 demo
  19. 深度学习应用于脑电信号处理
  20. 去《挪威的森林》之后

热门文章

  1. 软件测试艺术一:程序正确性证明
  2. ajax查找错误信息
  3. Mysql-学习笔记(==》常用函数 八)
  4. Yii CGridView 基本使用(三)关联表相关字段搜索
  5. 如何批量给数字前面加半角单引号[转]
  6. 信息安全系统设计基础第十一周学习总结
  7. 一款基于jquery ui的动画提交表单
  8. 2018.08.27 lucky(模拟)
  9. 谈谈WPF中的CollectionView与CollectionViewSource
  10. [BZOJ4557][JLOI2016]侦查守卫