jeesite 框架如何将office表和user表进行关联
时间: 2024-05-06 10:22:13 浏览: 162
Yii2.0框架模型多表关联查询示例
在Jeesite框架中,可以通过在Office实体类中添加一个外键字段来关联User表。具体步骤如下:
1. 在Office实体类中添加一个外键字段,例如userId:
```
public class Office extends DataEntity<Office> {
private static final long serialVersionUID = 1L;
// 省略其他字段
private User user; // 外键关联User表
private String userId;
// 省略getter和setter方法
}
```
2. 在OfficeMapper.xml文件中添加外键关联的SQL语句:
```
<resultMap id="BaseResultMap" type="com.thinkgem.jeesite.modules.sys.entity.Office">
<association property="user" javaType="com.thinkgem.jeesite.modules.sys.entity.User">
<result property="id" column="user_id" />
<result property="name" column="user_name" />
<result property="loginName" column="user_login_name" />
<!-- 省略其他字段 -->
</association>
</resultMap>
<select id="findList" resultMap="BaseResultMap">
SELECT o.*, u.id as user_id, u.name as user_name, u.login_name as user_login_name
FROM sys_office o
LEFT JOIN sys_user u ON o.user_id = u.id
WHERE o.del_flag = #{delFlag}
</select>
```
3. 在OfficeController中添加获取关联数据的方法:
```
@Controller
@RequestMapping(value = "${adminPath}/sys/office")
public class OfficeController extends BaseController {
@Autowired
private OfficeService officeService;
@Autowired
private UserService userService;
@ModelAttribute("office")
public Office get(@RequestParam(required=false) String id) {
if (StringUtils.isNotBlank(id)){
return officeService.get(id);
}
return new Office();
}
@RequestMapping(value = {"list", ""})
public String list(Office office, HttpServletRequest request, HttpServletResponse response, Model model) {
List<Office> list = officeService.findList(office);
for (Office o : list) {
User user = userService.get(o.getUserId());
o.setUser(user);
}
model.addAttribute("list", list);
return "modules/sys/officeList";
}
// 省略其他方法
}
```
通过以上步骤,就可以在Jeesite框架中实现Office表和User表的关联查询。在查询Office列表时,会自动查询关联的User数据,并将其设置到Office实体类中。
阅读全文