惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tor Project blog
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
I
InfoQ
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
美团技术团队
B
Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
TaoSecurity Blog
TaoSecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
T
Threatpost
H
Heimdal Security Blog
爱范儿
爱范儿
博客园_首页
SecWiki News
SecWiki News
腾讯CDC
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
The Register - Security
The Register - Security
N
News | PayPal Newsroom
Recent Commits to openclaw:main
Recent Commits to openclaw:main
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Security Latest
Security Latest
A
Arctic Wolf
P
Privacy & Cybersecurity Law Blog
T
The Blog of Author Tim Ferriss
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
Schneier on Security
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
Attack and Defense Labs
Attack and Defense Labs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
C
Check Point Blog
Y
Y Combinator Blog
T
The Exploit Database - CXSecurity.com
aimingoo的专栏
aimingoo的专栏
I
Intezer
博客园 - 叶小钗
Cisco Talos Blog
Cisco Talos Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
小众软件
小众软件

博客园 - Super Sky

2016年了 14年了,离上一篇2年了,做个记号 忙完这段时间后要做的事情 android比赛 计算机前沿技术造就亿万富翁 模板里面的时间(网上找的) javamail 转 异常 关于异常 xml生成和解析 转载 ibatis 转ibatis多表操作 我想出国读硕士了 迅雷在哪里,迅雷在这里 二维游戏开发的点滴 盟军敢死队 用c语言开发游戏 快乐的痛 笑着哭 所谓机遇 顽固文件删除
转载 联合查询 in ibatis
Super Sky · 2011-09-18 · via 博客园 - Super Sky

ibatis的联合查询

(2011-06-29 11:17:29)

和一般查询区别不同的有三个地方

1、首先是实体里面,以前存的是string 现在肯定要修改为 对象

package bean;

import java.sql.Timestamp;
import java.util.Date;

import baseparam.DeptBean;//存对象

public class StudentBean
{
 private String id;
 
 private String name;
 
 private String email;
 
 private Date birthday;
 
 private DeptBean dept;
 
 public DeptBean getDept()
 {
  return dept;
 }

 public void setDept(DeptBean dept)
 {
  this.dept = dept;
 }

 public String getId()
 {
  return id;
 }

 public void setId(String id)
 {
  this.id = id;
 }

 public Date getBirthday()
 {
  return birthday;
 }

 public void setBirthday(Date birthday)
 {
  this.birthday = birthday;
 }

 public String getName()
 {
  return name;
 }

 public void setName(String name)
 {
  this.name = name;
 }

 public String getEmail()
 {
  return email;
 }

 public void setEmail(String email)
 {
  this.email = email;
 }

 public StudentBean(String name)
 {
  super();
  this.name = name;
 }

 public StudentBean()
 {
  super();
  }
 
}

2、在配置文件中指定部门实体路径(/ibatis/src/SqlMapConfig.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
 <typeAliases>
  <typeAlias type="bean.StudentBean" alias="studentBean" />
  
  <!-- 指定部门实体路径-->
  <typeAlias type="baseparam.DeptBean" alias="deptBean"/>
 </typeAliases>
 
 <environments default="development">
  <environment id="development">
   <transactionManager type="JDBC" />
   <dataSource type="UNPOOLED">
    <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url"
     value="jdbc:oracle:thin:@192.168.17.201:1521:DEPPONIT" />
    <property name="username" value="deppon009" />
    <property name="password" value="dpit20090511" />
   </dataSource>
  </environment>
 </environments>
 <mappers>
  <mapper resource="Student.xml" />
 </mappers>
</configuration>

3、最后一个就是修改mapper文件了(/ibatis/src/Student.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="student">

 <sql id="studentColumn">
  fid,
  fname,
  fbirthday
 </sql>
 <select id="getAll" resultType="studentBean" resultMap="studentResultMap">
  select stu.fid fid,
   stu.fname fname,
   dept.fid deptid,
   dept.fname deptname,
   stu.fbirthday
  from t_student stu
   inner join t_afei_dept dept on dept.fid = stu.fdept
 </select>
 
 <select id="getByID" resultType="studentBean" resultMap="studentResultMap">
  select stu.fid fid,
   stu.fname fname,
   dept.fid deptid,
   dept.fname deptname,
   stu.fbirthday
  from t_student stu
   inner join t_afei_dept dept on dept.fid = stu.fdept
  where stu.fid = #{id}
 </select>
 
 <insert id="add" parameterType="studentBean">
  insert into t_student(fid, fname, fbirthday) values
   newbosid('SFSAFSDF'),
    #{name, jdbcType=VARCHAR},
    #{birthday}
   )
 </insert>
 
 <delete id="deleteById" parameterType="int">
  delete from t_student where fid = #{id}
 </delete>
 
 <update id="updateStudent" parameterType="studentBean">
  update t_student t set t.fname = #{name}, t.fbirthday = #{birthday}
  where t.fid = #{id}
 </update>
 
 <!-- 数据库字段和Bean对象字段映射 -->
 <resultMap type="studentBean" id="studentResultMap">
  <id property="id" column="fid"/>
  <result property="name" column="fname"/>
  <result property="birthday" column="fbirthday"/>
  
  <!-- 联合查询 property中的名字应该和deptBean这个名字一致
   column中的名字应该和查询时的别名一样 -->
  <association property="dept" column="fdept" javaType="deptBean">
   <id property="id" column="deptid"/>
   <result property="name" column="deptname"/>
   <result property="num" column="deptnum"/>
  </association>
 </resultMap>
 </mapper>

其他地方和一般的配置一样

最后测试类

 
 public void getAll()
 {
  StudentDao dao = new StudentDaoImpl();
  
  List<StudentBean> students = dao.getAllStudents();
  
  for(StudentBean c : students)
  {
   DeptBean dept = c.getDept();
   System.out.println("student'id is\t" + c.getId());
   System.out.println("student'name is\t" + c.getName());
   if(dept != null)
   {
    
    System.out.println("dept'name is\t" + dept.getName());
   }
   System.out.println("------------------------------");
  }
 }