`
zscomehuyue
  • 浏览: 412035 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

iBATIS的多对多映射配置浅析

阅读更多
    /*==============================================================*/  
    /* 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;  


Java代码

    /** 
    * 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();  
        } 


Java代码

    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();  
        }  


Java代码

    /** 
    * 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代码

    <?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代码

    <?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代码

    <?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> 


Java代码

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


Java代码

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


Xml代码

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


Java代码

    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);  
        }  
    } 


Java代码

    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);  
        }  
    } 


Java代码

    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;  
        }  
    } 
分享到:
评论

相关推荐

    ibatis 学习资料

    3. **iBATIS的多对多映射配置浅析 - 51CTO_COM.mht**:这份资料可能详细解析了iBatis中的多对多映射配置。在处理多对多关系时,iBatis使用标签来建立中间表的关系,实现关联对象的自动加载。 4. **iBATIS.pdf**:这...

    iBATIS教程之快速入门浅析

    iBATIS通过XML配置文件来定义SQL语句与Java对象之间的映射关系。这样,开发者可以在XML中编写SQL,而在Java代码中直接操作对象,从而实现对数据库的CRUD(Create、Read、Update、Delete)操作。 首先,创建一个简单...

    iBATIS教程之入门浅析借鉴.pdf

    iBATIS是一个Java编程语言中的持久层框架,它提供了一种半自动化的方式来处理ORM...通过这样的配置和使用,开发者可以轻松地在Java应用中实现数据访问,同时保留对SQL查询的直接控制,达到高效且灵活的数据库操作。

    网络第一份struts2.0学习文档

    - **Action配置**:在struts.xml中配置Action的映射、拦截器栈等。 #### 四、表单验证 - **手动完成输入校验**: - 在Action中编写逻辑进行校验。 - 使用自定义验证方法。 - **Struts2框架实现数据校验**: -...

    Struts2轻松入门,合适入门的朋友。

    根据给定的信息,本文将对Struts2框架的基础知识点进行详细介绍。Struts2是一个流行的Java Web应用程序框架,它简化了MVC(Model-View-Controller)设计模式的应用,并且易于扩展和集成其他技术。 ### Struts2入门 ...

    struts2经典入门教程

    2. 配置文件:Struts2框架的配置文件主要有struts.xml文件,它定义了Action映射和结果类型等信息,用于引导请求和响应的处理流程。 3. Action配置:Action是Struts2的核心,主要负责业务逻辑的处理和返回结果。...

    浅析Spring和MyBatis整合及逆向工程

    Spring通过生成代理对象,利用SqlSessionFactory创建SqlSession,从而实现对数据库的增删改查操作。此外,所有持久层的Mapper也需要由Spring管理,这样可以在不直接依赖具体数据库操作的情况下,实现业务逻辑。 在...

    struts2入门教程

    - **配置文件详解**:解析`struts.xml`配置文件的作用和编写方式,如何配置Action映射、拦截器栈等。 - **Action配置**:讲解如何定义Action类,Action的生命周期,以及如何返回不同的结果。 #### 四、表单验证 - *...

    struts入门教程

    2. **配置文件**:Struts2的配置主要通过`struts.xml`文件实现,该文件用于定义各种配置信息,如Action映射、拦截器、结果类型等,是框架运行的基础。 3. **Action配置**:Action是Struts2中的业务逻辑处理单元,...

    轻松入门之Struts2

    该文件主要用于配置Action映射、拦截器栈等关键元素。例如: ```xml &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

Global site tag (gtag.js) - Google Analytics