【MyBatis】对多的关联映射查询
本文最后更新于 1530 天前,其中的信息可能已经有所发展或是发生改变。

1.创建实体类。
[sourcecode language=”java” title=”com.bean.Goods.java”]
package com.bean;

public class Goods {
private int goodsId;
private String goodsName;
private Double goodsPrice;
private int goodsNum;
private int goodsType;

public Goods() {
}

public Goods(int goodsId, String goodsName, Double goodsPrice,
int goodsNum, int goodsType) {
super();
this.goodsId = goodsId;
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
this.goodsNum = goodsNum;
this.goodsType = goodsType;
}

public int getGoodsId() {
return goodsId;
}

public void setGoodsId(int goodsId) {
this.goodsId = goodsId;
}

public String getGoodsName() {
return goodsName;
}

public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}

public Double getGoodsPrice() {
return goodsPrice;
}

public void setGoodsPrice(Double goodsPrice) {
this.goodsPrice = goodsPrice;
}

public int getGoodsNum() {
return goodsNum;
}

public void setGoodsNum(int goodsNum) {
this.goodsNum = goodsNum;
}

public int getGoodsType() {
return goodsType;
}

public void setGoodsType(int goodsType) {
this.goodsType = goodsType;
}

@Override
public String toString() {
return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName
+ ", goodsPrice=" + goodsPrice + ", goodsNum=" + goodsNum
+ ", goodsType=" + goodsType + "]";
}
}
[/sourcecode]
[sourcecode language=”java” title=”com.bean.GoodsType.java”]

package com.bean;

import java.util.List;

public class GoodsType {
private int typeId;
private String typeName;
private List<Goods> goodsList;

public GoodsType() {
}

public GoodsType(int typeId, String typeName, List<Goods> goodsList) {
super();
this.typeId = typeId;
this.typeName = typeName;
this.goodsList = goodsList;
}

public int getTypeId() {
return typeId;
}

public void setTypeId(int typeId) {
this.typeId = typeId;
}

public String getTypeName() {
return typeName;
}

public void setTypeName(String typeName) {
this.typeName = typeName;
}

public List<Goods> getGoodsList() {
return goodsList;
}

public void setGoodsList(List<Goods> goodsList) {
this.goodsList = goodsList;
}

@Override
public String toString() {
return "GoodsType [typeId=" + typeId + ", typeName=" + typeName
+ ", goodsList=" + goodsList + "]";
}
}
[/sourcecode]
2.编写GoodsTypeMapper接口。
[sourcecode language=”java” title=”com.bean.GoodsTypeMapper.java”]
package com.mapper;

import java.util.List;

import com.bean.GoodsType;

public interface GoodsTypeMapper {
public List<GoodsType> findAll();
}
[/sourcecode]
3.编写XML文件。
(1)第一种,连接查询所有商品信息。
[sourcecode language=”xml” title=”com.bean.GoodsMapper.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 namespace="com.mapper.GoodsMapper">
<resultMap type="com.bean.Goods" id="resultGoods">
<id property="goodsId" column="goods_id" javaType="int" />
<result property="goodsName" column="goods_name" javaType="java.lang.String" />
<result property="goodsPrice" column="goods_price" javaType="double" />
<result property="goodsNum" column="goods_num" javaType="int" />
<result property="goodsType" column="goods_type" javaType="int" />
</resultMap>
</mapper>
[/sourcecode]
[sourcecode language=”xml” title=”com.bean.GoodsTypeMapper.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 namespace="com.mapper.GoodsTypeMapper">
<select id="findAll" resultMap="resultGoodsType">
select * from goods_type
left join goods
on goods.goods_type = goods_type.typeId
</select>

<resultMap type="com.bean.GoodsType" id="baseGoodsType">
<id property="typeId" column="typeId" javaType="int" />
<result property="typeName" column="typeName" javaType="java.lang.String" />
</resultMap>
<resultMap type="com.bean.GoodsType" id="resultGoodsType" extends="baseGoodsType">
<collection property="goodsList" javaType="java.util.List"
ofType="com.bean.Goods" resultMap="com.mapper.GoodsMapper.resultGoods"/>
</resultMap>
</mapper>
[/sourcecode]
(2)第二种,调用关联关系对象的Mapper的查询方法。
[sourcecode language=”java” title=”com.bean.GoodsMapper.java”]
package com.mapper;

import java.util.List;

import com.bean.Goods;

public interface GoodsMapper {
public List<Goods> findByType(int goodsTypeId);
}
[/sourcecode]
[sourcecode language=”xml” title=”com.bean.GoodsMapper.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 namespace="com.mapper.GoodsMapper">
<select id="findByType" parameterType="int" resultMap="resultGoods">
select * from goods where goods_type = #{id}
</select>
<resultMap type="com.bean.Goods" id="resultGoods">
<id property="goodsId" column="goods_id" javaType="int" />
<result property="goodsName" column="goods_name" javaType="java.lang.String" />
<result property="goodsPrice" column="goods_price" javaType="double" />
<result property="goodsNum" column="goods_num" javaType="int" />
<result property="goodsType" column="goods_type" javaType="int" />
</resultMap>
</mapper>
[/sourcecode]
[sourcecode language=”xml” title=”com.bean.GoodsTypeMapper.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 namespace="com.mapper.GoodsTypeMapper">
<select id="findAll" resultMap="resultGoodsType">
select * from goods_type
</select>

<resultMap type="com.bean.GoodsType" id="baseGoodsType">
<id property="typeId" column="typeId" javaType="int" />
<result property="typeName" column="typeName" javaType="java.lang.String" />
</resultMap>
<resultMap type="com.bean.GoodsType" id="resultGoodsType" extends="baseGoodsType">
<collection property="goodsList" javaType="java.util.List"
ofType="com.bean.Goods" select="com.mapper.GoodsMapper.findByType" column="typeId" />
</resultMap>
</mapper>
[/sourcecode]
4.编写mybatis.xml。
[sourcecode language=”xml” title=”mybatis.xml”]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring?useUnicode=true&amp;characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mapper/GoodsMapper.xml" />
<mapper resource="com/mapper/GoodsTypeMapper.xml" />
</mappers>
</configuration>
[/sourcecode]
5.编写测试类。
[sourcecode language=”java” title=”com.test.test.java”]
package com.test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.bean.GoodsType;
import com.mapper.GoodsTypeMapper;

public class test {

private SqlSession sqlSession;

public void test01() {
try {
GoodsTypeMapper goodsTypeMapper = this.sqlSession.getMapper(GoodsTypeMapper.class);
List<GoodsType> GoodsTypeList = goodsTypeMapper.findAll();
for (GoodsType goodsType : GoodsTypeList) {
System.out.println(goodsType);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
this.sqlSession.close();
}
}

@Test
public void testF(){
test01();
}

@Before
public void before() {
try {
String resource = "mybatis.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
this.sqlSession = ssf.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[/sourcecode]

数据库下载:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/数据库/spring3.sql[/bdbtn]
项目代码:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/源代码/MB007.rar[/bdbtn]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇