一:接口
public interface OrderMapper { //三表联查 public List<Student> findAllStudent(); }
二、student表和scores表和course表的联系
student与score表的关系是一对多
score与course表的关系是一对一
三、映射文件:
<?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"> <!--接口代理映射,所以OrderMapper接口--> <mapper namespace="com.jiazhong.Dao.moretableDao.OrderMapper"> <resultMap id="studentok" type="com.jiazhong.pojo.moreTable.Student"> <!--手动去指定字段与实体属性的映射关系--> <!--column:字段名称 property:实体属性的名称--> <!--student表--> <id column="sno" property="sno"></id> <result column="sname" property="sname"></result> <result column="ssex" property="ssex"></result> <result column="sbirthday" property="sbirthday"></result> <result column="classZ" property="classZ"></result> <!--scores表集合--> <!--对应的JavaBean地址--> <!--一对多是collection--> <collection property="scores" ofType="com.jiazhong.pojo.moreTable.Scores"> <id column="sno" property="sno"></id> <id column="cno" property="cno"></id> <result column="degree" property="degree"></result> <!--一对一是association--> <association property="courses" javaType="com.jiazhong.pojo.moreTable.Courses"> <!-- courses表--> <id column="cno" property="cno"></id> <result column="cname" property="cname"></result> <result column="tno" property="tno"></result> </association> </collection> </resultMap> <!--sql语句--> <!--id指的接口--> <!--resultMap保持一致--> <select id="findAllStudent" resultMap="studentok"> select students.*,scores.*,courses.* from students,scores,courses where students.sno=scores.sno and courses.cno=scores.cno </select> </mapper>
四、将文件核心配置文件中的<mappers>配置改成以下内容
<mapper resource="mapper/MoreTableStudentMapper.xml"/>
五、测试文件:
public void select3() throws IOException { //配置日志 Properties properties = new Properties(); FileInputStream fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties"); properties.load(fileInputStream); PropertyConfigurator.configure(properties); //连接核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis_config1.xml"); //获取一个工厂对象 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); //通过工厂获取一个对象 SqlSession sqlSession = build.openSession(); //获取接口 OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class); //调用接口中的方法 List<Student> userAndRoleAll = orderMapper.findAllStudent(); //与接口方法名称一致 for (Student user:userAndRoleAll){ System.out.println("信息:" + user); } }
六、javabean:
Student:
public class Student { private String sno; private String sname; private String ssex; private String sbirthday; private String classZ; private List<Scores> scores; public Student() { } public Student(String sno, String sname, String ssex, String sbirthday, String classZ, List<Scores> scores) { this.sno = sno; this.sname = sname; this.ssex = ssex; this.sbirthday = sbirthday; this.classZ = classZ; this.scores = scores; } /** * 获取 * @return sno */ public String getSno() { return sno; } /** * 设置 * @param sno */ public void setSno(String sno) { this.sno = sno; } /** * 获取 * @return sname */ public String getSname() { return sname; } /** * 设置 * @param sname */ public void setSname(String sname) { this.sname = sname; } /** * 获取 * @return ssex */ public String getSsex() { return ssex; } /** * 设置 * @param ssex */ public void setSsex(String ssex) { this.ssex = ssex; } /** * 获取 * @return sbirthday */ public String getSbirthday() { return sbirthday; } /** * 设置 * @param sbirthday */ public void setSbirthday(String sbirthday) { this.sbirthday = sbirthday; } /** * 获取 * @return classZ */ public String getClassZ() { return classZ; } /** * 设置 * @param classZ */ public void setClassZ(String classZ) { this.classZ = classZ; } /** * 获取 * @return scores */ public List<Scores> getScores() { return scores; } /** * 设置 * @param scores */ public void setScores(List<Scores> scores) { this.scores = scores; } public String toString() { return "Student{sno = " + sno + ", sname = " + sname + ", ssex = " + ssex + ", sbirthday = " + sbirthday + ", classZ = " + classZ + ", scores = " + scores + "}"; } }
Scores:
public class Scores { private String sno; private String cno; private String degree; private Courses courses; public Scores() { } public Scores(String sno, String cno, String degree, Courses courses) { this.sno = sno; this.cno = cno; this.degree = degree; this.courses = courses; } /** * 获取 * @return sno */ public String getSno() { return sno; } /** * 设置 * @param sno */ public void setSno(String sno) { this.sno = sno; } /** * 获取 * @return cno */ public String getCno() { return cno; } /** * 设置 * @param cno */ public void setCno(String cno) { this.cno = cno; } /** * 获取 * @return degree */ public String getDegree() { return degree; } /** * 设置 * @param degree */ public void setDegree(String degree) { this.degree = degree; } /** * 获取 * @return courses */ public Courses getCourses() { return courses; } /** * 设置 * @param courses */ public void setCourses(Courses courses) { this.courses = courses; } public String toString() { return "Scores{sno = " + sno + ", cno = " + cno + ", degree = " + degree + ", courses = " + courses + "}"; } }
Courses:
public class Courses { private String cno; private String cname; private String tno; public Courses() { } public Courses(String cno, String cname, String tno) { this.cno = cno; this.cname = cname; this.tno = tno; } /** * 获取 * @return cno */ public String getCno() { return cno; } /** * 设置 * @param cno */ public void setCno(String cno) { this.cno = cno; } /** * 获取 * @return cname */ public String getCname() { return cname; } /** * 设置 * @param cname */ public void setCname(String cname) { this.cname = cname; } /** * 获取 * @return tno */ public String getTno() { return tno; } /** * 设置 * @param tno */ public void setTno(String tno) { this.tno = tno; } public String toString() { return "Courses{cno = " + cno + ", cname = " + cname + ", tno = " + tno + "}"; } }
七:日志配置文件内容:
日志有助于帮你排错和观察每一步的进程和结果
log4j.rootLogger=debug, stdout,R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. Log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log Log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=5 log4j.appender.R.layout=org.apache.log4j.PatternLayout Log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
七、建表语句:
-- ----------------------------
-- Table structure for courses
-- ----------------------------
DROP TABLE IF EXISTS `courses`;
CREATE TABLE `courses` (
`cno` varchar(5) NOT NULL,
`cname` varchar(10) NOT NULL,
`tno` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of courses
-- ----------------------------
INSERT INTO `courses` VALUES ('3-105', '计算机导论', '825');
INSERT INTO `courses` VALUES ('3-245', '操作系统', '804');
INSERT INTO `courses` VALUES ('6-166', '数据电路', '856');
INSERT INTO `courses` VALUES ('9-888', '高等数学', '100');
-- ----------------------------
-- Table structure for scores
-- ----------------------------
DROP TABLE IF EXISTS `scores`;
CREATE TABLE `scores` (
`sno` varchar(3) NOT NULL,
`cno` varchar(5) NOT NULL,
`degree` decimal(10,1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of scores
-- ----------------------------
INSERT INTO `scores` VALUES ('103', '3-245', '86.0');
INSERT INTO `scores` VALUES ('105', '3-245', '75.0');
INSERT INTO `scores` VALUES ('109', '3-245', '68.0');
INSERT INTO `scores` VALUES ('103', '3-105', '92.0');
INSERT INTO `scores` VALUES ('105', '3-105', '88.0');
INSERT INTO `scores` VALUES ('109', '3-105', '76.0');
INSERT INTO `scores` VALUES ('101', '3-105', '64.0');
INSERT INTO `scores` VALUES ('107', '3-105', '91.0');
INSERT INTO `scores` VALUES ('108', '3-105', '78.0');
INSERT INTO `scores` VALUES ('101', '6-166', '85.0');
INSERT INTO `scores` VALUES ('107', '6-106', '79.0');
INSERT INTO `scores` VALUES ('108', '6-166', '81.0');
-- ----------------------------
-- Table structure for students
-- ----------------------------
DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
`sno` varchar(3) NOT NULL,
`sname` varchar(4) NOT NULL,
`ssex` varchar(2) NOT NULL,
`sbirthday` date NOT NULL,
`classZ` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of students
-- ----------------------------
INSERT INTO `students` VALUES ('108', '曾华', '男', '1977-09-01', '95033');
INSERT INTO `students` VALUES ('105', '匡明', '男', '1975-10-02', '95031');
INSERT INTO `students` VALUES ('107', '王丽', '女', '1976-01-23', '95033');
INSERT INTO `students` VALUES ('101', '李军', '男', '1976-02-20', '95033');
INSERT INTO `students` VALUES ('109', '王芳', '女', '1975-02-10', '95031');
INSERT INTO `students` VALUES ('103', '陆君', '男', '1974-06-03', '95031');