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

来源:互联网 发布:色系shipin软件下载 编辑:程序博客网 时间:2024/06/04 20:09

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 0
原创粉丝点击