mybatis中当一张表关联另一张表时需要注入另一张表的id

1.一个实体类名字叫做WithDraw 

1)新建实体类:

public class WithDraw extends BaseModel{


/**
* 取款完成时间
*/
private Date finishtime;
/**
* 取款用户
*/
private User user;


@JsonSerialize(using = JasonDateFieldFormatter.class)
@JsonIgnore(value = false)
public Date getFinishtime()
{
return finishtime;
}
public void setFinishtime(Date finishtime)
{
this.finishtime = finishtime;
}

public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}

@Override
public String toString() {
return "WithDraw [ finishtime=" + finishtime + ", user=" + user"]";
}

}



2.创建接口:

public interface WithDrawalsMapper {

/**
* 根据传递过来的 条件查找 
* @param params
* @return
*/
public WithDraw findByWithDraw(Map<String,Object> params);

}


创建xml文件:WithDrawalsMapper.xml

1)resultMap是MyBatis中最重要最强大的元素。resultMap封装查询的结果集,type可以是Bean、Object、Primitive type。

2)parameterType用来指定传入参数的类型,比如Bean或Map\List。

文件内容:

<?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.p2p.core.mapper.withdrawals.WithDrawalsMapper">
	<resultMap id="withdrawals" type="com.p2p.core.model.funds.WithDraw" >//构建<span style="font-family: Arial, Helvetica, sans-serif;">WithDraw实体类</span>
		<result property="id" column="id"/>//字段
		
		<result property="finishtime" column="finishtime"/>
		
		<association property="user" resultMap="user" />//注入User实体类
	</resultMap>
	<resultMap id="user" type="com.p2p.core.model.user.User" >
		<result property="user_name" column="user_name"/>
		<result property="useable_funds" column="useable_funds"/>
		<result property="frozen_funds" column="frozen_funds"/>
		<result property="realName" column="realName"/>
		<result property="mobile" column="mobile"/>
		<result property="idCart" column="idCart"/>
	</resultMap>
	<select id="searchWithDraw" resultMap="withdrawals" parameterType="java.util.Map">
		select t1.*,user.*
		from t_withdraw t1 left join t_user user on t1.user_id = user.id
	   	 where t1.status>0
	     	${whereClause}
			order by
			${sortBy} ${order}
			limit
			#{rowNum},#{rowCount}
			
	</select>
	<select id="count" resultType="java.lang.Integer">
		select count(*) 
			from t_withdraw t1 left join t_user user on t1.user_id = user.id
	   	 	where t1.status>0	
	
	</select>
	<select id="findByIdWithDraw"  resultMap="withdrawals"  parameterType="Integer">
		select t1.*,user.*
		from t_withdraw t1 left join t_user user on t1.user_id = user.id
	   	 where t1.status>0 and t1.id=#{id}
	 
	</select>
	<update id="updataWithDraw">
		update t_withdraw  
		<set>
			<if test="status != null">
				status =#{status},
			</if>
			<if test="agreetime != null">
				agreetime =#{agreetime},
			</if>
			<if test="approvaluser != null">
				approvaluser =#{approvaluser},
			</if>
		
		</set>
		where id=#{id}
	</update>


</mapper>









  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值