理论的知识我就不说了 官方文档 http://www.mybatis.org/mybatis-3/zh/
直接来案例
多个学生存在一个班级 ====》多对一
一个班级存在多个学生====》一对多
先在数据库中创建一个学生表 一个班级表 班级表中的主键是学生表中的外键
创建学生表的实例
package cn.easytop.lesson03.resultMap.anno;
//多对一
public class Student {
private int sid;
private String sname;
//学生带出班级
private Grade grade;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
创建班级表的实例
package cn.easytop.lesson03.resultMap.anno;
import java.util.ArrayList;
import java.util.List;
//一对多
public class Grade {
private int gid;
private String gname1;
//班级带出学生
List<Student> studentList=new ArrayList<Student>();
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname1() {
return gname1;
}
public void setGname1(String gname1) {
this.gname1 = gname1;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
@Override
public String toString() {
return "Grade [gid=" + gid + ", gname1=" + gname1 + "]";
}
}
创建mybatis的核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- mybatis的核心配置文件
1.数据库的连接的信息(连接池)
-->
<properties resource="oracle.properties"></properties>
<!-- 取别名 -->
<typeAliases>
<!-- 在此包下取别名 默认为类名的首字母小写 -->
<package name="cn.easytop.lesson03.resultMap.anno"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!-- 事务管理器 默认使用jdbc事务 -->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driverClass}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username1}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--
注意 这里是注解映射 不是xml映射 所以路径与xml有区别
使用的class属性 用点
-->
<mapper class="cn.easytop.lesson03.resultMap.anno.GradeMapper"/>
<mapper class="cn.easytop.lesson03.resultMap.anno.StudentMapper"/>
</mappers>
</configuration>
班级接口
package cn.easytop.lesson03.resultMap.anno;
import java.util.List;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
public interface GradeMapper {
//查询班级带出学生
@Results({
//属性名映射到数据库的列名中
@Result(property="gname1",column="gname"),
/*
* 根据@Select("select * from grade where gid=#{0}")查询出来的gid与数据库中学生列表名映射(一致自动映射)
* 把gid传递到 "cn.easytop.lesson03.resultMap.anno.StudentMapper.queryAllStudent" 此方法中
* 查询出来的结果与Grade中的 studentList映射 并指定类型 javaType=List.class
*
*/
@Result(property="studentList",javaType=List.class,column="gid",many=@Many
(select="cn.easytop.lesson03.resultMap.anno.StudentMapper.queryAllStudent"))
}
)
@Select("select * from grade where gid=#{0}")
public Grade queryAllGrade(String gid);
//学生带出班级
/*
* 当只有一个@Result()时不用@Results({})
* 有多个@Result()需要用@Results({@Result(),@Result()})
*/
//属性名与数据库中的列名不一致需要映射
@Results(
@Result(property="gname1",column="gname")
)
@Select("select * from grade where gid=#{0}")
public Grade queryGrade(String gid);
}
学生接口
package cn.easytop.lesson03.resultMap.anno;
import java.util.List;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
public interface StudentMapper {
//班级带出学生
@Select("select * from student where gid=#{0}")
public List<Student> queryAllStudent(String gid);
//@Result(column="sname",property="sname1")
@Results({
@Result(property="grade",column="gid",one=@One(select="cn.easytop.lesson03.resultMap.anno.GradeMapper.queryGrade"))
/*
*根据@Select("select * from student where sid=#{0}")结果 把gid自动映射到数据库中的列名gid(属性名和列名一致自动映射)
*把gid传入到one=@One(select="cn.easytop.lesson03.resultMap.anno.GradeMapper.queryGrade")
* 此方法中查询出结果映射到Student中的grade属性 property="grade"
*
*/
})
@Select("select * from student where sid=#{0}")
public Student queryStudent(String sid);
}
Test测试类
package cn.easytop.lesson03.resultMap.anno;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 Test {
public static SqlSession getSession() throws IOException{
String resource = "cn/easytop/lesson03/resultMap/anno/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
//session操作的是 指向sql语句的一个唯一表示符
return openSession;
}
/*
* 多对一查询 (查出学生带出学生所在的班级)
*
*/
@org.junit.Test
public void testManyToOne() throws IOException{
SqlSession session=getSession();
//获取FoodMapper的实现类
StudentMapper fm=session.getMapper(StudentMapper.class);
Student student = fm.queryStudent("1");
System.out.println(student.getSname()+"======="+student.getGrade().getGname1());
}
/*
* 一对多查询(查询班级带出班级中的学生)
*
*/
@org.junit.Test
public void testOneToMany() throws IOException{
SqlSession session=getSession();
//获取FoodMapper的实现类
GradeMapper fm=session.getMapper(GradeMapper.class);
Grade queryFood = fm.queryAllGrade("1");
System.out.println(queryFood);
for(Student s:queryFood.getStudentList()){
System.out.println(s.getSname());
}
}
}