为了账号安全,请及时绑定邮箱和手机立即绑定

mybatis的关联查询能查多层吗?

mybatis的关联查询能查多层吗?

穆男神 2018-04-18 18:00:02
比如我有三个表:表关系是厂家<品牌<型号,我们知道mybatis在mapper.xml中的BaseResultMap里配置好collection和assocation,然后查询语句配置resultMap成BaseResultMap,就可以单层级联查询,就是说,我查厂家同时能查到品牌,但是查不到型号。现在想问一下 怎么配置能查厂家的时候,不止对应的品牌能查到,还能找到每个品牌对应的型号。类似hibernate的查询效果
查看完整描述

3 回答

?
慕沐9307871

TA贡献27条经验 获得超9个赞

首先把所有关联设置好,然后在配置collection和assocation时用select属性调用就可以了。
需要考虑这样的缺点是系统消耗太大,毕竟mybatis不像hibernate缓存考虑的那么完善,mybatis优点就是轻量。

查看完整回答
反对 回复 2018-04-19
  • 穆男神
    穆男神
    具体怎么配置关联呢?
  • 慕沐9307871
    慕沐9307871
    vo里设置关联对象或集合,映射文件里配置collection或assocation就可以了,只不过不配置内部属性映射
?
北七哦

TA贡献33条经验 获得超4个赞

要自己写个你想查询结果的vo实体吧,然后自己写sql语句查出来。把resultMap对应的实体换成自己写的那个

查看完整回答
反对 回复 2018-04-19
?
乌云下的风

TA贡献40条经验 获得超10个赞

https://img1.sycdn.imooc.com//5ad80b530001080611860876.jpg

我这个resultMap是查看详情时一次拿出来的

查看完整回答
反对 回复 2018-04-19
  • 穆男神
    穆男神
    这是查询产品通过懒加载获得的数据吗?如果是 那么 你的产品类的mapper.xml 中 styles这个collection 的select属性对应的sql语句是用的resultMap还是resultType?内容又是什么呢?
?
乌云下的风

TA贡献40条经验 获得超10个赞

	<resultMap id="StyleMap" type="com.ccytsoft.model.pc.ProductStyleModel" >
		<result property="id" column="sid" />
		<result property="pid" column="spid" />
		<result property="name" column="sname" />
		<result property="title" column="stitle" />
		<result property="image" column="simage" />
		<result property="sort" column="ssort" />
		<association property="fitlist" javaType="com.ccytsoft.print.repository.model.Fittings" >
				<result property="id" column="id" />
				<result property="code" column="code" />
				<result property="name" column="name" />
				<result property="title" column="title" />
				<result property="image" column="image" />
				<result property="price" column="price" />
				<result property="close" column="close" />
		</association>
		
	</resultMap>
	
	
public class ProductStyleModel {
	private Integer id;
	private String name;
	private String title;
	private String image;
	private Integer sort;
	private Integer pid;
	private Integer gid;
	private Integer fid;
	private List<FittingGroup> grouplist=new ArrayList<FittingGroup>();
	
	private List<Fittings> fitlist   =  new ArrayList<Fittings>();

这个关系映射是集合(包)集合,你要的数据什么包装的,你想要什么结构什么样子的数据。

查看完整回答
反对 回复 2018-04-19
  • 穆男神
    穆男神
    你的Fittings这个对象,如果还有其他关联的对象怎么解决啊?你这么写 能找到ProductStyleModel对应的Fittings,但是如果Fittings还关联其他对象,查ProductStyleModel的同时能查Fittings,但不能同时查到Fittings关联的对象。 我说的是类似hibernate的无论多少层 一对多 ,都可以通过对象的get方法获取到。
  • 乌云下的风
    乌云下的风
    理论是可以的
  • 乌云下的风
    乌云下的风
    你说的hibernate,针对多层时需要懒加载,我没弄过,不怎么用hibernate了,我现在基本只用mybatis
  • 3 回答
  • 0 关注
  • 2487 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信