读书人

初学者问Mybatis 关联关系 如何配置

发布时间: 2014-01-17 15:01:00 作者: rapoo

菜鸟问Mybatis 关联关系 怎么配置
两张表的实体类:
package model;

import java.util.HashSet;
import java.util.Set;

/**
* Project entity. @author MyEclipse Persistence Tools
*/

public class Project implements java.io.Serializable {

// Fields

private int id;
private String projectname;
private Set workorders = new HashSet(0);

// Constructors

/** default constructor */
public Project() {
}

/** minimal constructor */
public Project(int id, String projectname) {
this.id = id;
this.projectname = projectname;
}

/** full constructor */
public Project(int id, String projectname, Set workorders) {
this.id = id;
this.projectname = projectname;
this.workorders = workorders;
}

// Property accessors

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public String getProjectname() {
return this.projectname;
}

public void setProjectname(String projectname) {
this.projectname = projectname;
}

public Set getWorkorders() {
return this.workorders;
}

public void setWorkorders(Set workorders) {
this.workorders = workorders;
}

}


package model;

import java.util.Date;

/**
* Workorder entity. @author MyEclipse Persistence Tools
*/

public class Workorder implements java.io.Serializable {

// Fields

private int id;
private Project project;
private String executor;
private String description;
private int orderlevel;
private Date createdate;

// Constructors

/** default constructor */
public Workorder() {
}

/** full constructor */
public Workorder(int id, Project project, String executor,
String description, int orderlevel, Date createdate) {
this.id = id;
this.project = project;
this.executor = executor;
this.description = description;
this.orderlevel = orderlevel;
this.createdate = createdate;
}

// Property accessors

public Workorder(Project project, String executor, String description,
int orderlevel, Date createdate) {
super();
this.project = project;
this.executor = executor;
this.description = description;
this.orderlevel = orderlevel;
this.createdate = createdate;
}

public Workorder(String executor, String description, int orderlevel,
Date createdate) {
super();
this.executor = executor;
this.description = description;
this.orderlevel = orderlevel;
this.createdate = createdate;
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public Project getProject() {
return this.project;
}

public void setProject(Project project) {
this.project = project;
}

public String getExecutor() {
return this.executor;
}

public void setExecutor(String executor) {
this.executor = executor;
}

public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

public int getOrderlevel() {
return this.orderlevel;
}

public void setOrderlevel(int orderlevel) {
this.orderlevel = orderlevel;
}

public Date getCreatedate() {
return this.createdate;
}

public void setCreatedate(Date createdate) {
this.createdate = createdate;
}

}

jsp页面:
<html>
<head>

<title>显示</title>
</head>

<body>
<table width="auto" border="1">
<tr>
<td>员工编号</td>
<td>项目名称</td>
<td>执行人</td>
<td>任务描述</td>
<td>级别</td>
<td>创建时间</td>
</tr>


<c:forEach items="${list}" var="u">
<tr>

<td>${u.id}</td>
<td>${u.project.projectname}</td>
<td>${u.executor}</td>
<td>${u.description}</td>
<td>${u.orderlevel}</td>
<td>${u.createdate}</td>

</tr>
</c:forEach>
<tr>
<td colspan="6"> </td>
</tr>
</table>
</body>
</html>

查询结果
初学者问Mybatis 关联关系 如何配置

查询projectname(项目名称)是空 ${u.project.projectname} 查不出??

workorder.xml
<mapper namespace="ttt">

<select id="selectWorkorder" resultType="workorder">
select * from workorder
</select>

<resultMap type="workorder" id="workorder">

<association property="project"/>
</resultMap>
</mapper>


project.xml
<mapper namespace="ttt2">

<select id="selectUser" resultType="project">
select * from project
</select>

<resultMap type="project" id="project">

<collection property="workorders"/>

</resultMap>

</mapper>

请问这两个 xml文件里到底该怎么写 两张表关联关系
[解决办法]
楼主参考一下啊

读书人网 >Java Web开发

热点推荐