Profess.java 一方
package com.pojo; //一对多 一方 import java.util.List; public class Profess { int proid; String professName; List<Student> stus; public int getProid() { return proid; } public void setProid(int proid) { this.proid = proid; } public String getProfessName() { return professName; } public void setProfessName(String professName) { this.professName = professName; } public List<Student> getStus() { return stus; } public void setStus(List<Student> stus) { this.stus = stus; } }
Student.java 多方
package com.pojo; import java.util.Date; //学员和专业 多对一 多方 public class Student { int stuid; String stuName; int stuAge; Date stuDate; String stuSex; String stuProfess;//专业外键字段,可以保留 String status; String professName;//为了方便业务,补充一个专业名字的属性 Profess profess;//专业对象,做级联查询 public int getStuid() { return stuid; } public void setStuid(int stuid) { this.stuid = stuid; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public Date getStuDate() { return stuDate; } public void setStuDate(Date stuDate) { this.stuDate = stuDate; } public String getStuSex() { return stuSex; } public void setStuSex(String stuSex) { this.stuSex = stuSex; } public String getStuProfess() { return stuProfess; } public void setStuProfess(String stuProfess) { this.stuProfess = stuProfess; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getProfessName() { return professName; } public void setProfessName(String professName) { this.professName = professName; } public Profess getProfess() { return profess; } public void setProfess(Profess profess) { this.profess = profess; } }
IStudentDAO.java
package com.dao; import java.util.List; import com.pojo.Student; public interface IStudentDAO { List<Student> getStuByProId(int proid); List<Student> manyToOne(); }
Student.xml
<?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="com.dao.IStudentDAO"> <!-- 学员和专业的多对一映射 --> <resultMap type="com.pojo.Student" id="stu_profess"> <id property="stuid" column="stuid"/> <result property="stuName" column="stuName"/> <result property="stuAge" column="stuAge"/> <result property="stuDate" column="stuDate"/> <result property="stuSex" column="stuSex"/> <result property="stuProfess" column="stuProfess"/> <!--多对一 --> <association property="profess" javaType="com.pojo.Profess"> <id property="proid" column="proid"></id> <result property="professName" column="professName"/> </association> </resultMap> <!-- 根据专业id来查询学员 --> <select id="getStuByProId" parameterType="int" resultType="com.pojo.Student"> select * from student where stuprofess=#{id} </select> <!-- 多对一的查询,查询学员同时查出专业信息 --> <select id="manyToOne" resultMap="stu_profess"> select * from student a,profess b where a.stuProfess=b.proid </select> </mapper>
IProfessDAO.java
package com.dao; import java.util.List; import com.pojo.Profess; public interface IProfessDAO { List<Profess> ontToMany(); }
Profess.xml
<?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="com.dao.IProfessDAO"> <resultMap id="pro_stu" type="com.pojo.Profess"> <id property="proid" column="proid"></id> <result property="professName" column="professName"></result> <collection property="stus" ofType="com.pojo.Student" column="proid" select="com.dao.IStudentDAO.getStuByProId"></collection> </resultMap> <!-- 一对多的查询 --> <select id="ontToMany" resultMap="pro_stu"> select * from profess </select> </mapper>
多对一测试代码:
Test.java
package com.pojo; import java.io.IOException; import java.io.Reader; import java.util.Date; 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; import com.dao.IStudentDAO; public class Test { public static void main(String[] args) throws IOException { //读取核心的配置文件 Reader reader = Resources.getResourceAsReader("Configuration.xml"); //创建会话工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); //数据库会话 SqlSession session = factory.openSession(); //查询一个对象 第一个参数字符串是由Student.xml 中的namespace+id组成 // Student stu = session.selectOne("com.dao.StudentMapper.getStudentById",5); //通过接口的方式 IStudentDAO stuDAO = session.getMapper(IStudentDAO.class); //测试多对一的级联查询 List<Student> list = stuDAO.manyToOne(); for (Student stu : list) { System.out.println(stu.getStuName()+" "+stu.getProfess().getProfessName()); } session.close(); } }
一对多测试代码:
package com.pojo; import java.io.IOException; import java.io.Reader; import java.util.Date; 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; import com.dao.IProfessDAO; import com.dao.IStudentDAO; public class Test { public static void main(String[] args) throws IOException { //读取核心的配置文件 Reader reader = Resources.getResourceAsReader("Configuration.xml"); //创建会话工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); //数据库会话 SqlSession session = factory.openSession(); //查询一个对象 第一个参数字符串是由Student.xml 中的namespace+id组成 // Student stu = session.selectOne("com.dao.StudentMapper.getStudentById",5); //通过接口的方式 IStudentDAO stuDAO = session.getMapper(IStudentDAO.class); IProfessDAO proDAO = session.getMapper(IProfessDAO.class); //测试一对多查询 List<Profess> list = proDAO.ontToMany(); for (Profess pro : list) { System.out.println(pro.getProfessName()); for (Student stu : pro.getStus()) { System.out.print(stu.getStuName()+" "); } System.out.println(); } session.close(); } }
Configuration中的注册
<!-- 对象的映射文件注册 --> <mappers> <mapper resource="com/pojo/Student.xml"></mapper> <mapper resource="com/pojo/Profess.xml"></mapper> </mappers>