多个对象对应一个对象时,应该如何进行查询?
例如
关键字:association : 联系 ,关联 多个人可以关联一个人。
首先做一些准备,如:实体类,工具类和Mybatis核心文件
实体类:
//老师实体类 package com.MLXH.pojo; public class Teacher { private int id; private String name; public Teacher() { } public Teacher(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Teacher{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
//学生实体类 package com.MLXH.pojo; public class Student { private int id; private String name; private Teacher teacher; public Student() { } public Student(int id, String name, Teacher teacher) { this.id = id; this.name = name; this.teacher = teacher; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", teacher=" + teacher + '}'; } }
database.properties配置文件数据库需要的数据
driver = com.mysql.jdbc.Driver url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8 username = root password = 123456
Mybatis工具类:mybatis-config.xml
工具类:主要是为了使获得sqlsession更为简单方便
package com.MLXH.utils; 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 java.io.IOException; import java.io.InputStream; //mybatis的工具类,重复的代码的提纯 public class MyBatisUtils { //类变量不需要设置默认值; private static SqlSessionFactory sqlSessionFactory; static { //在maven中,所有的资源文件一般都放在resources目录下,我们可以直接拿到。 try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //设置SqlSessionFactory公共的方法 public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } //获得一个带事务自动提交功能的SqlSession公共的方法 public static SqlSession getSqlSession(){ //自动提交事务 return sqlSessionFactory.openSession(true); } }
StudentDao接口
package com.MLXH.dao; import com.MLXH.pojo.Student; import java.util.List; public interface StudentDao { //获得全部学生的信息以及对应的老师 ListgetStudents(); //获得全部学生的信息以及对应的老师 List getStudentsTwo(); }
针对于StudentDao接口的实现mapper文件:我们使用如下的方式来进行查询
1.模拟数据库思想:连表查询
StudentMapper.xml
测试类
@Test public void getStudents(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); StudentDao mapper = sqlSession.getMapper(StudentDao.class); Liststudents = mapper.getStudents(); for (Student student : students) { System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName()); } }
2.模拟面向对象的思想
测试
@Test public void getStudentsTwo(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); StudentDao mapper = sqlSession.getMapper(StudentDao.class); Liststudents = mapper.getStudentsTwo(); for (Student student : students) { System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName()); } }
总结:
mybatis中遇到多对一的情况,要使用关联映射处理:使用association
两种处理思路:
- 数据库思想 : 联表查询
- OOP思想 :关联对象
Mybatis同时传入多个对象及普通参数
当传入多个文件时,mapper接口文件的方法参数要使用@param(“xx”)注释。
例子:
mapper:
//Student是对象,age是String类型。 int getPojo(@param("student") Student student, @param("age") String age );
xml:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。