`
wuyoubf
  • 浏览: 38135 次
  • 性别: Icon_minigender_1
  • 来自: 呼和浩特
社区版块
存档分类
最新评论

iBatis2学习笔记:多对多映射(双向)

阅读更多
/*==============================================================*/ 
/* Table: role                                                  */ 
/*==============================================================*/ 
create table role 
( 
   id                   bigint not null auto_increment, 
   rolename             varchar(24), 
   descp                varchar(240), 
   primary key (id) 
); 

alter table role comment '角色'; 

/*==============================================================*/ 
/* Table: tlink                                                 */ 
/*==============================================================*/ 
create table tlink 
( 
   userId               bigint not null, 
   roleId               bigint not null 
); 

alter table tlink comment '连接表'; 

/*==============================================================*/ 
/* Table: user                                                  */ 
/*==============================================================*/ 
create table user 
( 
   id                   bigint not null auto_increment, 
   username             varchar(24), 
   remark               varchar(240), 
   primary key (id) 
); 

alter table user comment '用户'; 

alter table tlink add constraint FK_r foreign key (roleId) 
      references role (id) on delete restrict on update restrict; 

alter table tlink add constraint FK_u foreign key (userId) 
      references user (id) on delete restrict on update restrict; 

 

/** 
* Created by IntelliJ IDEA.<br> 
* <b>User</b>: leizhimin<br> 
* <b>Date</b>: 2008-6-16 0:12:13<br> 
* <b>Note</b>: 用户角色多对多模型:角色 
*/ 
public class User { 
    private Long id; 
    private String username; 
    private String remark; 
    private List<Role> roleList = new ArrayList<Role>(); 


    public String toString() { 
        return "User{" + 
                "id=" + id + 
                ", username='" + username + '\'' + 
                ", remark='" + remark + '\'' + 
                ", roleList='" + roleList.size() + '\'' + 
                '}'; 
    } 

    public String out() { 
        StringBuffer sb = new StringBuffer(); 
        sb.append("User{" + 
                "id=" + id + 
                ", username='" + username + '\'' + 
                ", remark='" + remark + '\'' + 
                ", roleList='" + roleList.size() + '\''); 
        for (Role role : roleList) { 
            sb.append("\n\t").append(role.toString()); 
        } 
        return sb.toString(); 
    }

 

public class Role { 
    private Long id; 
    private String rolename; 
    private String descp; 
    private List<User> userList= new ArrayList<User>(); 

    public String toString() { 
        return "Role{" + 
                "id=" + id + 
                ", rolename='" + rolename + '\'' + 
                ", descp='" + descp + '\'' + 
                ", userList=" + userList.size() + 
                '}'; 
    } 

    public String out(){ 
        StringBuffer sb= new StringBuffer(); 
        if(userList.size()>0){ 
            sb.append("Role{" + 
                "id=" + id + 
                ", rolename='" + rolename + '\'' + 
                ", descp='" + descp + '\'' + 
                ", userList=" + userList.size()); 
            for(User u: userList){ 
                sb.append("\n\t").append(u.toString()); 
            } 
            sb.append("\n}"); 
        } 
        return sb.toString(); 
    } 

 

/** 
* Created by IntelliJ IDEA.<br> 
* <b>User</b>: leizhimin<br> 
* <b>Date</b>: 2008-6-16 0:17:15<br> 
* <b>Note</b>: 用户角色多对多模型:连接表 
*/ 
public class Tlink { 
    private Long id; 
    private Long userId; 
    private Long roleId;

 

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > 
<sqlMap namespace="user"> 
    <typeAlias alias="user" type="com.lavasoft.ssi.domain.User"/> 
    <resultMap id="result_basc" class="user"> 
        <result property="id" column="id"/> 
        <result property="username" column="username"/> 
        <result property="remark" column="remark"/> 
    </resultMap> 

    <resultMap id="result" class="user" extends="result_basc"> 
        <result property="roleList" column="id" select="role.getByUserId"/> 
    </resultMap> 


    <insert id="insert" parameterClass="user"> 
        insert into user(username,remark) values(#username#,#remark#) 
        <selectKey keyProperty="id" resultClass="long"> 
            select LAST_INSERT_ID() 
        </selectKey> 
    </insert> 
    <select id="getById" parameterClass="long" resultMap="result_basc"> 
        select * from user where id = #value# 
    </select> 

    <select id="getWithCashWithRoleList" parameterClass="long" resultMap="result"> 
        select * from user where id = #value# 
    </select> 

    <select id="getByRoleId" parameterClass="long" resultMap="result_basc"> 
        select u.* from user u where u.id in 
        (select userId from tlink where roleId=#value#) 
    </select> 


</sqlMap>

 

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > 
<sqlMap namespace="role"> 
    <typeAlias alias="role" type="com.lavasoft.ssi.domain.Role"/> 
    <resultMap id="result_basc" class="role"> 
        <result property="id" column="id"/> 
        <result property="rolename" column="rolename"/> 
        <result property="descp" column="descp"/> 
    </resultMap> 
    <resultMap id="result" class="role" extends="result_basc"> 
        <result property="userList" column="id" select="user.getByRoleId"/> 
    </resultMap> 
    <insert id="insert" parameterClass="role"> 
        insert into role(rolename,descp) values(#rolename#,#descp#) 
        <selectKey keyProperty="id" resultClass="long"> 
            select LAST_INSERT_ID() 
        </selectKey> 
    </insert> 
    <select id="getById" parameterClass="long" resultMap="result_basc"> 
        select * from role where id = #value# 
    </select> 

    <select id="getRoleByIdWithCashUser" parameterClass="long" resultMap="result"> 
        select * from role where id = #value# 
    </select> 

    <!--为多对多配置--> 
    <select id="getByUserId" parameterClass="long" resultClass="role" resultMap="result_basc"> 
        select r.* from role r where r.id in 
        (select roleId from tlink where userId=#value#) 
    </select> 
</sqlMap>

 

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > 
<sqlMap namespace="tlink"> 
    <typeAlias alias="tlink" type="com.lavasoft.ssi.domain.Tlink"/> 
    <resultMap id="result" class="tlink"> 
        <result property="id" column="id"/> 
        <result property="userId" column="userId"/> 
        <result property="roleId" column="roleId"/> 
    </resultMap> 
    <insert id="insert" parameterClass="tlink"> 
        insert into tlink(userId,roleId) values(#userId#,#roleId#) 
        <selectKey keyProperty="id" resultClass="long"> 
            select LAST_INSERT_ID() 
        </selectKey> 
    </insert> 
    <select id="getByUserId" parameterClass="long" resultMap="result"> 
        select * from tlink where userId = #value# 
    </select> 
    <select id="getByRoleId" parameterClass="long" resultMap="result"> 
        select * from tlink where roleId = #value# 
    </select> 
    <delete id="delete" parameterClass="tlink"> 
        delete from tlink where userId = #userId# and roleId = #roleId# 
    </delete> 

</sqlMap>

 

public interface UserDAO { 
    public Long insert(User user); 
    public Object getById(Long id); 
    public Object getWithCashById(Long id); 
    public User getWithCashWithRoleList(Long id); 
} 

 

public interface RoleDAO { 
    public Long insert(Role role); 
    public Role getById(Long id); 
    public Role getRoleByIdWithCashUser(Long id); 
    public List<Role> getByUserId(Long userId); 
}

 

public interface TlinkDAO { 
    public void insert(Long userId,Long roleId); 
    public int delete(Long userId,Long roleId); 
    public int update(Long userId,Long roleId); 
}

 

public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO { 
    public Long insert(User user) { 
        return (Long) getSqlMapClientTemplate().insert("user.insert",user); 
    } 

    public Object getById(Long id) { 
        return getSqlMapClientTemplate().queryForList("user.getById",id); 
    } 

    public Object getWithCashById(Long id) { 
        return getSqlMapClientTemplate().queryForList("user.getWithCashById",id); 
    } 

    public User getWithCashWithRoleList(Long userId) { 
        return (User) getSqlMapClientTemplate().queryForObject("user.getWithCashWithRoleList",userId); 
    } 
}

 

public class RoleDAOImpl extends SqlMapClientDaoSupport implements RoleDAO { 
    public Long insert(Role role) { 
        return (Long) getSqlMapClientTemplate().insert("role.insert",role); 
    } 

    public Role getById(Long id) { 
        return (Role) getSqlMapClientTemplate().queryForObject("role.getById",id); 
    } 

    public Role getRoleByIdWithCashUser(Long id) { 
        return (Role) getSqlMapClientTemplate().queryForObject("role.getRoleByIdWithCashUser",id); 
    } 

    public List<Role> getByUserId(Long userId) { 
        return getSqlMapClientTemplate().queryForList("role.getByUserId",userId); 
    } 
}

 

public class TlinkDAOImpl extends SqlMapClientDaoSupport implements TlinkDAO { 
    public void insert(Long userId, Long roleId) { 
        Tlink tlink = new Tlink(userId, roleId); 
        getSqlMapClientTemplate().insert("tlink.insert",tlink); 
    } 

    public int delete(Long userId, Long roleId) { 
        Tlink tlink = new Tlink(userId, roleId); 
        return getSqlMapClientTemplate().delete("tlink.delete",tlink); 
    } 

    public int update(Long userId, Long roleId) { 
        return 0; 
    } 
}

 

分享到:
评论

相关推荐

    iBatis2学习笔记

    包括了几部分: ...7.iBatis2学习笔记:多对多映射(双向) .doc 8.iBatis2学习笔记:总结与思考.doc 9.iBatis2实体状态图解.doc 10.iBatis insert操作陷阱.doc 每章都有小例子。 呵呵,希望有所帮助!

    ibatis 学习笔记

    ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记

    ibatis 一对多关系映射

    ibatis 一对多关系映射 ibatis 一对多关系映射ibatis 一对多关系映射

    Ibatis的学习笔记

    Ibatis的学习笔记,说明Ibatis的使用

    ibatis 一对多 多对多完整映射

    NULL 博文链接:https://self4j.iteye.com/blog/906319

    ibatis学习笔记

    ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记

    IBatis学习笔记以及使用心得

    IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得

    持久层框架ibatis学习笔记

    能,我没有去深思,学了iBatis 对以后学习其它持久层框架是有很多好吃的。希望看到我的 这篇学习笔记的同学们也一起学习iBatis。 这篇iBatis 学习笔记是跟着传智播客的视频学习整理的,理解上难免有些错误,请以视频...

    ibatis学习笔记.txt

    ibatis学习笔记.txtibatis学习笔记.txtibatis学习笔记.txt

    ibatis教程学习笔记

    学习ibatis的基础资料~~~ 要好好的利用学习~~~学习别人的笔记是你进步的动力~~

    Ibatis一对一映射提示

    Ibatis一对一映射提示,需要学习的同学请关注,谢谢。

    iBATIS-SqlMaps,ibatis映射文件

    iBATIS-SqlMaps,ibatis映射文件

    iBATIS_In_Action:使用映射语句(一).doc

    iBATIS_In_Action:使用映射语句(一).doc

    iBatis学习笔记

    iBatis学习笔记,有利于初步了解iBatis。

    iBATIS学习笔记

    iBATIS学习笔记 使用 iBATIS 开发近一年了,都是在 Google 中现学现用,是时候为自己总结看看这一年都收获了些什么。无奈的是英文水平实在是太差了,官方文档看起来太吃力,所以到图书馆借了这本《iBATIS 实战》这是 ...

    ibatis学习

    IbatisDemo 博文链接:https://xdjava.iteye.com/blog/1070408

Global site tag (gtag.js) - Google Analytics