mybatis一对多两种mapper写法

mybatis一对多两种mapper写法
第一种

<resultMap type="com.example.demo.model.TuserModel" id="extendMapper">
		<id column="id" property="id" />
		<result column="user_name" property="userName" />
		<result column="nick_name" property="nickName" />
		<result column="avatar" property="avatar" />
		<result column="email" property="email" />
		<result column="signature" property="signature" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
		<result column="del_flag" property="delFlag" />
		<collection property="tpluginModels" ofType="com.example.demo.model.TpluginModel"
			column="id">
			<id column="pid" property="id" />
			<result column="user_id" property="userId" />
			<result column="name" property="name" />
			<result column="icon" property="icon" />
			<result column="vsersion" property="vsersion" />
			<result column="tags" property="tags" />
			<result column="description" property="description" />
			<result column="bcreate_time" property="createTime" />
			<result column="bupdate_time" property="updateTime" />
			<result column="del_flag" property="delFlag" />
		</collection>
		 </resultMap>

sql语句用联表查询

	u.*,p.id
		as
		pid,p.user_id,p.name,p.icon,p.vsersion,p.tags,p.description,p.create_time
		as bcreate_time,p.update_time as bupdate_time,p.del_flag from t_user u
		LEFT
		JOIN t_plugin p ON u.id=p.user_id and u.del_flag=0 and
		p.del_flag=0 WHERE
		u.user_name LIKE CONCAT('%',#{name},'%') OR
		u.nick_name LIKE
		CONCAT('%',#{name},'%')

第二种

<resultMap type="com.example.demo.model.TuserModel" id="extendMapper">
		<id column="id" property="id" />
		<result column="user_name" property="userName" />
		<result column="nick_name" property="nickName" />
		<result column="avatar" property="avatar" />
		<result column="email" property="email" />
		<result column="signature" property="signature" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
		<result column="del_flag" property="delFlag" />
		
		  <collection property="tpluginModels" column="id" ofType="com.example.demo.model.TpluginModel" 
			select="pluginByUid" />   //column='id' 为关联查询所需条件
		</resultMap>

sql语句使用两个sql语句返回结果

 <select id="selectTuserBynameOrNick" resultMap="extendMapper"> SELECT 
		* FROM t_user WHERE del_flag = 0 AND ( user_name LIKE CONCAT( '%', #{name},'%') 
		OR nick_name LIKE CONCAT( '%', #{name},'%')) </select> 
//下个sql语句依赖上个
<select id="pluginByUid" resultType="com.example.demo.model.TpluginModel"> SELECT id,user_id as
		userId,name,icon,vsersion,tags,description,
		create_time as createTime ,update_time as updateTime ,del_flag as delFlag
		FROM t_plugin WHERE del_flag = 0 AND user_id = #{id}
	</select>
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中进行一对多查询可以通过以下步骤实现: 1. 定义实体类:首先,需要定义两个实体类,一个表示一的一方,另一个表示多的一方。例如,假设有一个订单(Order)和多个订单项(OrderItem),可以分别创建Order和OrderItem的实体类。 2. 编写Mapper接口:针对每个实体类,创建对应的Mapper接口。在Mapper接口中定义查询方法,用于执行一对多查询。例如,在OrderMapper接口中定义查询订单及其关联的订单项的方法。 ```java public interface OrderMapper { Order findOrderAndItemsById(Long orderId); } ``` 3. 编写Mapper XML文件:在Mapper XML文件中编写对应的SQL语句,实现一对多查询。使用MyBatis的`<collection>`标签来映射多的一方。例如,在OrderMapper.xml文件中编写查询订单及其关联的订单项的SQL语句: ```xml <select id="findOrderAndItemsById" resultMap="orderResultMap"> SELECT * FROM orders WHERE order_id = #{orderId} </select> <resultMap id="orderResultMap" type="Order"> <id property="orderId" column="order_id" /> <!-- 其他订单属性映射 --> <collection property="orderItems" ofType="OrderItem"> <id property="itemId" column="item_id" /> <!-- 其他订单项属性映射 --> </collection> </resultMap> ``` 4. 执行查询:在业务层中调用Mapper接口的方法,执行一对多查询。 ```java @Autowired private OrderMapper orderMapper; public Order findOrderAndItemsById(Long orderId) { return orderMapper.findOrderAndItemsById(orderId); } ``` 以上就是使用MyBatis进行一对多查询的基本步骤。需要根据具体的业务需求和数据库表结构来进行相应的调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值