association的使用

association----------第6条使用

1.目录结构

2.Mybatis-config.xml配置

<!DOCTYPE configuration  
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  <!-- xml核心配置 -->
<configuration> 
<!-- 日志的具体实现 -->
    <settings>
    <setting name="logImpl" value="LOG4J"/>
    </settings>

<!-- 环境配置 -->
   <environments default="mysql">
      <environment id="mysql">
           <!-- 指定事务回滚 -->
           <transactionManager type="JDBC"></transactionManager>
           <!-- 指数据源配置 -->
           <dataSource type="POOLED">
              <property name="driver" value="com.mysql.jdbc.Driver"/>
              <property name="url" value="jdbc:mysql://127.0.0.1:3306/user"/>
              <property name="username" value="root"/>
              <property name="password" value="123456"/>
           </dataSource>
      </environment>
   </environments>
<!-- 告诉mybatis去哪里找映射文件 -->
   <mappers>
   <!-- <mapper resource="org/mapper/UserMapper.xml"/> -->
   <mapper resource="org/mapper/studentMapper.xml"/>
   </mappers>
</configuration>

3.班级类

package org.domain;

public class ClassGrade {
   private int id;           
   private String code;   //班级编号
   
public ClassGrade() {
	super();
}
public ClassGrade(int id, String code) {
	super();
	this.id = id;
	this.code = code;
}
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getCode() {
	return code;
}
public void setCode(String code) {
	this.code = code;
}
   
}

4.学生类

package org.domain;
public class student {
    private int id;
    private String name;
    private String sex;
    private int age;
    private int class_id;
    private ClassGrade classGrade;
	public student() {
		super();
	}
	public student(int id, String name, String sex, int age) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	public student(int id, String name, String sex, int age, int class_id) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.class_id = class_id;
	}
	public student(int kid, String name, String sex, int age,ClassGrade classGrade) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.classGrade = classGrade;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getClass_id() {
		return class_id;
	}
	public void setClass_id(int class_id) {
		this.class_id = class_id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public ClassGrade getClassGrade() {
		return classGrade;
	}
	public void setClassGrade(ClassGrade classGrade) {
		this.classGrade = classGrade;
	}
}

5.

sqlSessionFactory类
package org.factory;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class sqlSessionFactory {
	 //SqlSessionFactory:单个数据库的映射关系编译之后的内存镜像
	 //功能:是创建SqlSession 的工厂
     private static SqlSessionFactory factory=null;
     
     
     /*
      * 获取 SqlSessionFactory对象的方法
      */
    static {
    	 //读取mybatis-config.xml文件
    	 InputStream input = null;
		try {
			input = Resources.getResourceAsStream("mybatis-config.xml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 //初始化mybatis
         factory=new SqlSessionFactoryBuilder().build(input);
     }
     
     
     /*
      * 获取sqlSession对象
      */
     public static SqlSession getSqlSession() {
    	
		return  factory.openSession(); 
     }
     
     
     /*
      * 获取sqlSessionFactory
      */
     public static SqlSessionFactory getSqlSessionFactory() {
    	 return factory;
     }
     
}

6.studentMapper.xml配置

(配置文件中每一个select针对各自的表)

第一种方式select嵌套

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

  <!-- 对象result  tMap学生映射-->
  <resultMap id="studentResultMap" type="org.domain.student" >
      <id property="id" column="id"/>
      <result property="name" column="name"/>
      <result property="sex" column="sex"/>
      <result property="age" column="age"/>
      <association property="classGrade"  column="class_id" javaType="org.domain.ClassGrade" select="selectClasses">
      <result property="id" column="id"/>
      <result property="code" column="code"/>
      </association>
  </resultMap>
 
  <!--班级查询  -->
  <select id="selectClasses"  resultType="org.domain.ClassGrade">
     select * from class_tb where id=#{id}
  </select>
  <!--学生查询语句  -->
  <select id="selectStudents" resultMap="studentResultMap">
      select * from student
  </select>
  
</mapper>

(配置文件中每一个select针对连接一个以上的表)

第二种方式resultMap嵌套

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


  <!-- 对象result  tMap学生映射-->
  <resultMap id="studentResultMap" type="org.domain.student" >
      <id property="id" column="id"/>
      <result property="name" column="name"/>
      <result property="sex" column="sex"/>
      <result property="age" column="age"/>
      <association property="classGrade"  column="class_id" resultMap="class"/>  
  </resultMap>
  
  <resultMap  id="class"  type="org.domain.ClassGrade">
    <result property="id" column="id"/>
    <result property="code" column="code"/>
  </resultMap>
  <!--关联查询学生及班级  -->
  <select id="aSelect"  resultMap="studentResultMap">
       select  student.id,student.name,student.sex,student.age,class_tb.id as oid,class_tb.code
       from student left outer join class_tb  on student.class_id=class_tb.id; 
  </select>
</mapper>

第三种方式对象嵌套(跟第二种方式没有太大差别只是对象也不需要再关联查询):

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

  <!-- 对象result  tMap学生映射-->
  <resultMap id="studentResultMap" type="org.domain.student" >
      <id property="id" column="id"/>
      <result property="name" column="name"/>
      <result property="sex" column="sex"/>
      <result property="age" column="age"/>
      <association property="classGrade"  column="class_id" javaType="org.domain.ClassGrade">  
      <result property="id" column="id"/>
      <result property="code" column="code"/>
      </association>
  </resultMap>
  <!--关联查询学生及班级  -->
  <select id="aSelect"  resultMap="studentResultMap">
       select  student.id,student.name,student.sex,student.age,class_tb.id as oid,class_tb.code
       from student left outer join class_tb  on student.class_id=class_tb.id; 
  </select>
</mapper>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值