用easyui时遇到一对一 外键关联时 数据绑定

本文介绍了一个基于Easyui的用户管理界面实现方案,通过一对一双向绑定的方式展示用户及其关联的角色信息,并提供了添加、修改和删除用户的交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//其中role 和 user 是一对一的关系
function init_user() {
    $("#first").datagrid({
        title:"用户管理",
        url:"findAllUser",
        method:"get",
        columns:[[
            {field:"id",checkbox:true},
            {field:"account",title:"账号",width:200},
            {field:"pwd",title:"密码",width:200},
           {
                field: "rname", title: "角色", width: "200",
               formatter: function (value, row) {
                return row.role.rname
            }
            }

        ]],
        toolbar:[
            {text:"添加",iconCls:"icon-add",handler:function(){toUser();}},
            {text:"修改",iconCls:"icon-edit",handler:function (){toEditUser();}} ,
            {text:"删除",iconCls:"icon-remove",handler:function(){delUsers();}}
        ]
    });
}


后台user.xml配置
<mapper namespace="com.test.dao.UserDao">
    <resultMap type="com.test.entity.User" id="userMap">
        <id property="id" column="id"/>
        <result property="account" column="account"/>
       <result property="pwd" column="pwd"/>
        <!-- 嵌套结果,使用一跳sql语句,将查询出的关联的数据直接绑定到对象上
        association 体现“一” 的关系
        property 关联的对象的属性的值
        resultMap property的值对应的resultMap ,格式 namespace + resultMap
        的id 本例中card 关联的是CardMapper.xml中的cardMapper
        -->
        <association property="role" resultMap="com.test.dao.RoleDao.roleMapper"></association>
    </resultMap>
    <!--查寻  改动过 将Type 改为Map resultType="com.test.entity.User"-->
    <select id="findAllUser" resultMap="userMap">
        SELECT  user.*,role.*  FROM USER INNER JOIN  role WHERE  role.id = roleId;
    </select>
role.xml 配置
<mapper namespace="com.test.dao.RoleDao">
    <resultMap type="com.test.entity.Role" id="roleMapper">
        <id property="id" column="id"/>
        <result property="rname" column="rname"/>
    </resultMap>
    <!--查询-->
    <select id="findAllRole" resultMap="roleMapper">
        SELECT  * FROM Role
    </select>

User实体类
public class User implements Serializable{
    private int id;
    private Role role;
    private  String account;
    private String pwd;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值