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>