`
zhangfeilo
  • 浏览: 397453 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

Ibatis的两表联查问题

 
阅读更多

实体类:

多方: 

 

public class Employ {   
private int id;   
private String enployName;   
private int salary;   
private Department department;   
  
public Employ() {   
}   
  
public int getId() {   
return id;   
}   
  
public void setId(int id) {   
this.id = id;   
}   
  
public String getEnployName() {   
return enployName;   
}   
  
public void setEnployName(String enployName) {   
this.enployName = enployName;   
}   
  
public int getSalary() {   
return salary;   
}   
  
public void setSalary(int salary) {   
this.salary = salary;   
}   
  
public Department getDepartment() {   
return department;   
}   
  
public void setDepartment(Department department) {   
this.department = department;   
}   
}   

 一方:

 

public class Department {   
private int did;   
private String departmentName;   
private List<Employ> employees;   
  
  
public int getDid() {   
return did;   
}   
  
public void setDid(int did) {   
this.did = did;   
}   
  
public String getDepartmentName() {   
return departmentName;   
}   
  
public void setDepartmentName(String departmentName) {   
this.departmentName = departmentName;   
}   
  
public List<Employ> getEmployees() {   
return employees;   
}   
  
public void setEmployees(List<Employ> employees) {   
this.employees = employees;   
}   
}

 映射:

多方<?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="Employ">  
  
  <!-- Use type aliases to avoid typing the full classname every time. -->  
  <typeAlias alias="Employ" type="com.test.domain.Employ"/>  
  
   <!-- Result maps describe the mapping between the columns returned   
        from a query, and the class properties.   A result map isn't   
        necessary if the columns (or aliases) match to the properties   
        exactly. -->  
  <resultMap id="EmployResult" class="Employ">  
    <result property="id" column="id"/>  
    <result property="enployName" column="employ_name"/>  
    <result property="salary" column="salary"/>  
    <result property="department.did" column="did"/>  
    <result property="department.departmentName" column="department_name"/>  
  </resultMap>  
  
  <!-- Select with no parameters using the result map for Account class. -->  
  <select id="selectAllEmploy" resultMap="EmployResult">  
  <![CDATA[
   select * from employees e, departments d where e.departmentid = d.did
   ]]>  
  </select>  
   <!-- A simpler select example without the result map.   Note the   
        aliases to match the properties of the target result class. -->  
     
  <!-- Insert example, using the Account parameter class -->  
  <insert id="insertEmploy" parameterClass="Employ">  
  <![CDATA[
   insert into employees (employ_name, salary, departmentid) values(#enployName#, #salary#, #department.did#)
   ]]>  
  </insert>  
  
  <!-- Update example, using the Account parameter class -->  
  
  <!-- Delete example, using an integer as the parameter class -->  
</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="Department">  
  
  <!-- Use type aliases to avoid typing the full classname every time. -->  
  <typeAlias alias="Department" type="com.test.domain.Department"/>  
  
   <!-- Result maps describe the mapping between the columns returned   
        from a query, and the class properties.   A result map isn't   
        necessary if the columns (or aliases) match to the properties   
        exactly. -->  
  <resultMap id="DepartmentResult" class="Department">  
    <result property="did" column="did"/>  
    <result property="departmentName" column="department_name"/>  
  </resultMap>  
  
  <!-- Select with no parameters using the result map for Account class. -->  
  <select id="selectDepartmentById" parameterClass="int" resultMap="DepartmentResult">  
  <![CDATA[
   select * from departments where did = #did#
   ]]>  
  </select>  
   <!-- A simpler select example without the result map.   Note the   
        aliases to match the properties of the target result class. -->  
     
  <!-- Insert example, using the Account parameter class -->  
  <insert id="insertDepartment" parameterClass="Department">  
  <![CDATA[
   insert into departments (department_name) values(#departmentName#)
   ]]>  
  </insert>  
  
  <!-- Update example, using the Account parameter class -->  
  
  <!-- Delete example, using an integer as the parameter class -->  
</sqlMap>  

 <typeAlias  alias  = "user"  type  = "model.User"   />
    <resultMap id="userResult" class="user">
        <result property="name" column="uname"/>
        <result property="test.name" column="tname"/>
    </resultMap>
    <select id="getUser1" resultMap="userResult">
        select t_useR.name as uname , test.name as tname from t_useR ,test  where t_useR.test_id=test.id

    </select>

 

 u = (User) exampleMain.getSqlMap().queryForList("getUser1").get(0);
        System.out.println("user.id:"+u.getId()+" , user.name:"+u.getName()+" , test.name: "+u.getTest().getName());
    

out:

user.id:null , user.name:Sat Jul 30 11:28:20 CST 2011 , test.name: 11111111

分享到:
评论

相关推荐

    ibatis学习资料汇总

    你可以创建一个简单的CRUD(创建、读取、更新、删除)应用,或者参与更复杂的业务逻辑,如分页查询、多表联查等。这将帮助你熟悉iBatis的配置、接口调用以及异常处理。 总结,iBatis作为一款强大的数据访问框架,为...

    ibatis入门与ibatis迭代的用法

    此外,iBatis还支持Map迭代,当查询结果中的列名与Java对象的属性不完全匹配,或者需要处理多表联查结果时,Map迭代非常有用。例如: ```java List, Object&gt;&gt; maps = sqlSession.selectList(...

    IBatis.net-IBatis.DataAccess.1.9.2/IBatis.DataMapper.1.6.2

    6. **结果集映射**:可以处理多表联查的结果集,通过配置文件定义复杂的结果集映射规则。 **IBatis.DataAccess.1.9.2** IBatis.DataAccess是另一个关键组件,它提供了与数据库交互的基础服务,包括连接管理、命令...

    Ibatis视频下载

    7. **实战演练**:通过实际项目案例,视频会教你如何将Ibatis应用到实际开发中,解决常见的问题,如多表联查、分页查询等。 8. **最佳实践**:视频会分享一些Ibatis使用的最佳实践,帮助你编写更高效、可维护的代码...

    ibatis mybatis 分页 crud 完整代码

    - **Read(读取)**: 读取记录,常用Select语句,可以使用条件查询、多表联查等。 - **Update(更新)**: 更新已有记录,Update语句配合Where子句指定更新条件。 - **Delete(删除)**: 删除记录,Delete语句配合...

    ibatis用xml配置文件配置使用

    对于复杂场景,如多表联查、分页、存储过程等,你可以继续深入学习XML配置文件的高级用法,例如使用`&lt;association&gt;`, `&lt;collection&gt;`处理嵌套结果,使用`&lt;resultMap&gt;`定义复杂的映射关系等。 总的来说,iBATIS的XML...

    ibatis实现CRUD操作

    6. 映射器(Mapper):Ibatis提供了两种方式定义映射,一是XML配置文件,二是使用注解。映射器定义了SQL语句以及结果集的映射规则。 例如,如果我们想要实现一个用户表的CRUD操作,可以这样进行: 1. 创建User实体...

    ibatis常见案例

    Ibatis通过`&lt;select&gt;`标签和`&lt;association&gt;`、`&lt;collection&gt;`子标签来实现多表联查。例如,可以定义一个映射文件,其中包含两个表的关联查询,使用`left join`或`right join`等SQL语句进行连接操作,然后在Java对象...

    ibatis.rar学习手册

    8. **案例分析**:通过实际案例展示iBatis在不同场景下的应用,如CRUD操作、批量处理、多表联查等。 9. **源码解析**:对于对底层原理感兴趣的开发者,可能包含iBatis源码的解读,帮助理解其工作原理。 10. **进阶...

    ibatis单个对象的各种操作

    而复杂查询可能涉及多表联查、分页、排序等,可以通过`resultMap`来映射复杂结果。 在Java代码中,我们使用SqlSession和Mapper接口来执行这些操作。例如,对于插入操作: ```java SqlSession session = ...

    iBatis技术教程

    另一个可能涉及更复杂的业务逻辑,比如多表联查、事务控制或者缓存的使用,以帮助学习者更直观地了解iBatis的实战应用。 总之,iBatis是一个强大的工具,通过本教程的学习,开发者可以熟练地运用它来构建高效、可...

    ibatis.jar

    然而,它也有其局限性,如对于复杂的多表联查和动态查询,可能需要编写较多的XML映射文件,增加了开发工作量。尽管如此,Ibatis仍然是Java开发中广泛使用的持久层框架之一,尤其适用于那些对SQL有高度定制需求的项目...

    iBatis.Net 入门例子,类似于 HelloWorld

    在实际开发中,iBatis.Net不仅可以用于简单的CRUD操作,还可以处理复杂的多表联查、存储过程调用等任务,它的灵活性和易用性使得它成为.NET平台上的一个热门选择。通过深入学习和实践这个入门例子,你将更好地掌握...

    iBatis 例子(推荐学习)

    你可以利用`&lt;join&gt;`标签或者自定义的SQL片段来处理复杂的联查问题。此外,它还提供了动态SQL的功能,允许你在运行时根据条件动态构建SQL语句,这在处理各种复杂的业务逻辑时非常有用。 在J2EE环境中,iBatis可以很...

    ibatis 视频 配

    10. **实战应用**:通过实际项目练习,如CRUD操作、复杂查询、多表联查等,巩固理论知识并提高实际操作技能。 这些知识点构成了Ibatis学习的主线,通过视频教程和配套资源,学习者可以系统地掌握Ibatis的使用,并在...

    ibatis2讲义

    IBATIS支持两种类型的事务管理: - **本地事务**:通过JDBC连接直接管理事务。 - **全局事务**:通常与容器集成使用,如Spring等。 #### 10. 批处理 批处理是提高性能的一种有效手段,尤其在批量插入、更新大量...

    基于Servlet3.0+IBatis+BootStrip技术构建简单会议管理系统

    第24课 设备管理(删除)+设备信息(多表联查) 第25课 查询设备信息(根据会议室条件查询) 第26课 添加会议室 第27课 会议管理(查询+修改) 第28课 会议室管理(修改+删除) 第29课 会议室查询(查看)+ajax验证...

    40道MyBatis面试题带答案(很全)

    1. 对于复杂的SQL和多表联查,编写工作量可能会增加。 2. SQL语句依赖特定的数据库,可能影响数据库的移植性。 在使用MyBatis时,有时会遇到属性名与字段名不一致的问题,可以通过以下方式解决: 1. 在SQL语句中...

    mybatis 最新jar包

    MyBatis适用于那些对SQL定制需求较高、对性能要求严格的项目,特别是对于复杂的多表联查和大数据量处理,MyBatis提供了更灵活的解决方案。 总的来说,MyBatis 是一款轻量级的持久层框架,它通过简化数据库操作的...

    博客(传琦):初识Mybatis 对应的源码

    - 对复杂SQL支持相对较弱,尤其是多表联查。 总结,Mybatis作为轻量级的持久层框架,通过将SQL与Java代码分离,实现了更清晰的业务逻辑。理解和掌握Mybatis的源码有助于我们更好地利用其特性,提高代码质量,并且在...

Global site tag (gtag.js) - Google Analytics