Mybatis多对多转化为一对多查询,简单易学(SSM框架)
2017-09-14 10:29
453 查看
<?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">
//命名空间为dao的路径
<mapper namespace="com.sp.dao.GoodsDao">
<resultMap type="Goods" id="goodsmap">
<id property="id" column="id" />
<!-- property="实体类属性" column="数据库字段名" -->
<association property="seller" column="sellerid" javaType="Seller" select="getseller">
<id property="id" column="id"/>
</association>
<collection property="gtype" column="id" ofType="Gtype" select="getgtype"><!-- column="id" 对应下面 goodsid=#{id}中的id 值为商品表的id-->
<id property="id" column="id"/>
</collection>
</resultMap>
<select id="getseller" resultType="Seller">
select * from seller where id=#{id}
</select>
//把多对多转化为一对多(增加一个中间表,详细见下图),根据子查询得到gtype表数据,对应上面select="getgtype"
<select id="getgtype" resultType="Gtype">
select * from gtype where id in (select typeid from goodstype where goodsid =#{id} )
</select>
//返回类型须写成goodsmap,这样就会自动调用goodsmap中的查询,把查询到的gtype,seller自动放到实体类中
<select id="getAll" resultMap="goodsmap">
select * from goods
</select>
</mapper>
!------------------------------------------------------------------------------------------------------------------------------------------------------!
实体类Goods,其他两个没啥特别的就不给大家贴了,主要就是定义一个List<Gtype> gtype;
public class Goods {
Integer id;
String name;
String pic;
Seller seller;
double price;
double wprice;//折扣价
String remark;//备注
State state;
Timestamp addtime;
Integer sort;
List<Gtype> gtype;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getWprice() {
return wprice;
}
public void setWprice(double wprice) {
this.wprice = wprice;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Timestamp getAddtime() {
return addtime;
}
public void setAddtime(Timestamp addtime) {
this.addtime = addtime;
}
public Seller getSeller() {
return seller;
}
public void setSeller(Seller seller) {
this.seller = seller;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public List<Gtype> getGtype() {
return gtype;
}
public void setGtype(List<Gtype> gtype) {
this.gtype = gtype;
}
}
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
//命名空间为dao的路径
<mapper namespace="com.sp.dao.GoodsDao">
<resultMap type="Goods" id="goodsmap">
<id property="id" column="id" />
<!-- property="实体类属性" column="数据库字段名" -->
<association property="seller" column="sellerid" javaType="Seller" select="getseller">
<id property="id" column="id"/>
</association>
<collection property="gtype" column="id" ofType="Gtype" select="getgtype"><!-- column="id" 对应下面 goodsid=#{id}中的id 值为商品表的id-->
<id property="id" column="id"/>
</collection>
</resultMap>
<select id="getseller" resultType="Seller">
select * from seller where id=#{id}
</select>
//把多对多转化为一对多(增加一个中间表,详细见下图),根据子查询得到gtype表数据,对应上面select="getgtype"
<select id="getgtype" resultType="Gtype">
select * from gtype where id in (select typeid from goodstype where goodsid =#{id} )
</select>
//返回类型须写成goodsmap,这样就会自动调用goodsmap中的查询,把查询到的gtype,seller自动放到实体类中
<select id="getAll" resultMap="goodsmap">
select * from goods
</select>
</mapper>
!------------------------------------------------------------------------------------------------------------------------------------------------------!
实体类Goods,其他两个没啥特别的就不给大家贴了,主要就是定义一个List<Gtype> gtype;
public class Goods {
Integer id;
String name;
String pic;
Seller seller;
double price;
double wprice;//折扣价
String remark;//备注
State state;
Timestamp addtime;
Integer sort;
List<Gtype> gtype;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getWprice() {
return wprice;
}
public void setWprice(double wprice) {
this.wprice = wprice;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Timestamp getAddtime() {
return addtime;
}
public void setAddtime(Timestamp addtime) {
this.addtime = addtime;
}
public Seller getSeller() {
return seller;
}
public void setSeller(Seller seller) {
this.seller = seller;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public List<Gtype> getGtype() {
return gtype;
}
public void setGtype(List<Gtype> gtype) {
this.gtype = gtype;
}
}
相关文章推荐
- Mybatis框架搭建与简单查询详解
- SSM-MyBatis框架关联查询实例二
- SSM框架day02-MyBatis——034——符合查询问题演示、035——动态SQL
- ssm框架学习---mybatis中一对多关系的查询
- SSM框架day02-MyBatis——046——关联查询-one2one-共享主键解决方案、047、048关联查询-什么是自关联、one2many-多表连接查询
- 手把手搭建最简单的SSM框架Maven,idea(spring+springmvc+mybatis)附源代码
- SSM框架day02-MyBatis——066——动态SQL查询、067 MyBatis注解-动态SQL增删改、068 MyBatis注解-动态SQL的SQL类
- SSM框架中集成TKMybatis+PageHelper实现高效查询和分页
- SSM框架下实现MyBatis连接查询,一对多和多对一
- SSM框架day02-MyBatis——049——关联查询-自关联-one2many-多表单独查询、050 关联查询-自关联-many2one
- 21、SSM框架-Mybatis多参数查询与列表查询不同方式实现(3)
- 23、SSM框架-Mybatis基于注解实现增删查改和多参数列表查询(5)
- ssm框架创建简单的DVD管理系统(四)spring的配置与mybatis的配置
- 使用idea搭建一个简单的SSM框架:(3)配置spring+mybatis
- ssm框架-简单的分页查询
- SSM框架笔记04:MyBatis关联数据查询
- SSM框架 Mybatis联合查询 多表查询 分页
- myBatis框架建设与简单查询
- SSM框架day02-MyBatis——025——对单表的CURD操作-查询所有
- SSM框架之MyBatis(二)--多表查询及查询结果映射