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

来源:互联网 发布:linux 查看数据包 编辑:程序博客网 时间:2024/05/29 11:22
//其中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;
}

原创粉丝点击