原创

Mybatis关联关系和动态Sql

在MyBatis的框架中可以进行关联关系的映射,但是种类没有Hibernate那么多

只有二类关联关系

第一类: 对一 有一个实体类需要映射

第二类: 对多 有一个集合需要映射

1、对一的关系映射

1.数据库中关联关系

2.实体对象之间的关系

现在只处理对一的情况,所以在Goods类中有一个GoodsType类的对象

public class GoodsType {
	private int typeId;
	private String typeName;
}
public class Goods {
	private int goodsId;
	private String goodsName;
	private double goodsPrice;
	private int goodsNum;
	private GoodsType goodsType;//表明对一关系
}

3. 编写Mapper接口

public interface GoodsMapper {
	public List<Goods> findAll();
}

4 .编写XML文件

在这个XML文件中配置关联对象的映射,在MyBatis中有三种解决方案可选

4.1 第一种:直接通过对象打点调用属性的方式做映射

​ 要求: 查询语句要使用连接查询

<!-- 第一种方案:使用连接查询 -->
	<select id="findAll" resultMap="resultGoods">
		select * from goods
		left join goods_type
		on goods_type.typeId = goods.goods_type
	</select>
	<resultMap type="com.zpark.tea_mgr.domain.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.typeId" column="typeId" javaType="int" />
		<result property="goodsType.typeName" column="typeName" javaType="java.lang.String" />
	</resultMap>

4.2 第二种:引用关联关系对象的Mapper映射

GoodsTypeMapper.xml文件
<resultMap type="com.zpark.tea_mgr.domain.GoodsType" id="resultGoodsType">
	<id property="typeId" column="typeId" javaType="int" />
	<result property="typeName" column="typeName" 
javaType="java.lang.String" />
</resultMap>
GoodsMapper.xml文件
<association property="goodsType"
	javaType="com.zpark.tea_mgr.domain.GoodsType"			resultMap="com.zpark.tea_mgr.mapper.GoodsTypeMapper.resultGoodsType" />

4.3 第三种:调用关联关系对象的Mapper的查询方法

public interface GoodsTypeMapper {
	public GoodsType findById(int typeId);
}
<select id="findById" parameterType="int" resultMap="resultGoodsType">
	select * from goods_type where typeId = #{id}
</select>
GoodsMapper.xml中
<!-- 第三种方案: 查自己就好了 -->
<select id="findAll" resultMap="resultGoods">
	select * from goods
</select>
<!-- 第三种方案: 使用关联关系对象的查询的方法 -->
<association property="goodsType"
	javaType="com.zpark.tea_mgr.domain.GoodsType" 
	select="com.zpark.tea_mgr.mapper.GoodsTypeMapper.findById" 
	column="goods_type" />

2、对多的关联映射

一个类型有多个商品 设计为: 在类型类中有一个商品的集合

1.编写实体类

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

2. 编写GoodsTypeMapper接口

public interface GoodsTypeMapper {
	public List<GoodsType> findAll();
}

3.编写XML文件

映射从一个类型到多个商品

3.1第一种,连接查询所有商品信息

<select id="findAll" resultMap="resultGoodsType">
		select * from goods_type
		left join goods
		on goods.goods_type = goods_type.typeId
</select>
<resultMap type="com.zpark.tea_mgr.domain.GoodsType" id="baseGoodsType">
	<id property="typeId" column="typeId" javaType="int" />
	<result property="typeName" column="typeName" 
javaType="java.lang.String" />
</resultMap>
<resultMap type="com.zpark.tea_mgr.domain.GoodsType" id="resultGoodsType" 
extends="baseGoodsType">
<collection property="goodsList" javaType="java.util.List" 
			ofType="com.zpark.tea_mgr.domain.Goods" 
			resultMap="com.zpark.tea_mgr.mapper.GoodsMapper.resultGoods"
 /> 
</resultMap>

3.2 第二种 调用关联关系对象的Mapper的查询方法

public interface GoodsMapper {
	public List<Goods> findByType(int goodsTypeId);
}

<select id="findByType" parameterType="int" resultMap="resultGoods">
	select * from goods where goods_type = #{id}
</select>
<!-- 第二种方案 --> 
<collection property="goodsList" javaType="java.util.List" 
		ofType="com.zpark.tea_mgr.domain.Goods"
		select="com.zpark.tea_mgr.mapper.GoodsMapper.findByType" 
		column="typeId" />

3、动态Sql

1.set标签在update语句中的使用

使用动态SQL,我们要修改商品,可以不必先从数据库中查询出来

public interface GoodsMapper {
	public void update(Goods goods);
	public Goods findById(int goodsId);
	public void updateByDynamicSQL(Goods goods);
}
<update id="updateByDynamicSQL"
              parameterType="com.zpark.tea_mgr.domain.Goods">
		update goods
		<set>
			<if test="goodsName != null">
				goods_name = #{goodsName},
			</if>
			<if test="goodsPrice != null">
				goods_price = #{goodsPrice},
			</if>
			<if test="goodsNum != null">
				goods_num = #{goodsNum},
			</if>
			<if test="goodsType != null and goodsType.typeId != null">
				goods_type = #{goodsType.typeId},
			</if>
		</set>
		where goods_id = #{goodsId}
	</update>

2. where标签在查询语句中的使用

public class GoodsSearchVO {
	private String goodsName;
	private Double minPrice;
	private Double maxPrice;}

public List<Map<String, Object>> findBySearchVO(GoodsSearchVO goodsSearchVO);
<select id="findBySearchVO"
parameterType="com.zpark.tea_mgr.vo.GoodsSearchVO"
	        resultType="java.util.Map">
		select
		goods_name as 'goodsName',
		goods_price as 'goodsPrice',
		goods_num as 'goodsNum',
		typeName as 'typeName'
		from goods
		left join goods_type
		on goods_type.typeId = goods.goods_type
		<where>
			<if test="goodsName != null">
				goods_name like concat('%', #{goodsName}, '%')
			</if>
			<if test="minPrice != null">
				and goods_price &gt; #{minPrice}
			</if>
			<if test="maxPrice != null">
				and goods_price &lt; #{maxPrice}
			</if>
		</where>
	</select>

3.foreach标签的使用

3.1批量插入

public void saveBat(List<Goods> goodsList);
<insert id="saveBat" parameterType="java.util.List">
	insert into goods (goods_name, goods_price, goods_num, goods_type)
	values
	<foreach collection="list" item="goods" separator=",">
		(#{goods.goodsName}, #{goods.goodsPrice}, #{goods.goodsNum},
#{goods.goodsType.typeId})
	</foreach>
</insert>

3.2 批量删除

public void delBat(List<Integer> seq);
<delete id="delBat" parameterType="java.util.List">
	delete from goods
	where goods_id in 
	<foreach collection="list" item="id" separator="," open="(" close=")"
 index="index">
		#{id}
	</foreach>
</delete>


  • 作者:管理员(联系作者)
  • 发表时间:2020-04-20 01:31
  • 版权声明:自由转载-非商用-非衍生-保持署名
  • 公众号转载:请在文末添加作者公众号二维码
  • 评论