Mybatis中多个对象包含同一个对象的处理操作
多个对象对应一个对象时,应该如何进行查询?
例如
关键字: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
<?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> <!--配置文件修改--> <properties resource="database.properties"/> <!--Mybatis设置--> <settings> <!--默认日志实现--> <!--<setting name="logImpl" value="STDOUT_LOGGING"/>--> <!--Log4j实现--> <setting name="logImpl" value="LOG4J"/> </settings> <!--配置别名--> <typeAliases> <package name="com.MLXH.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <!--class对应的是一个接口类--> <!--resource对应的是一个接口类的映射文件--> <mapper resource="com/MLXH/dao/StudentMapper.xml"/> </mappers> </configuration>
工具类:主要是为了使获得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 { //获得全部学生的信息以及对应的老师 List<Student> getStudents(); //获得全部学生的信息以及对应的老师 List<Student> getStudentsTwo(); }
针对于StudentDao接口的实现mapper文件:我们使用如下的方式来进行查询
1.模拟数据库思想:连表查询
StudentMapper.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"> <!--namespace不能写别名--> <mapper namespace="com.MLXH.dao.StudentDao"> <!--遇到问题:学生类中关联老师: 多个学生对应一个老师 --> <!--解决问题方式一:按查询结果嵌套处理,模拟数据库思想;--> <select id="getStudents" resultMap="StudentTeacher"> select * from mybatis.student </select> <resultMap id="StudentTeacher" type="Student"> <id column="id" property="id"/> <result column="name" property="name"/> <!--属性和字段对应 , 类和表对应 , 对象和记录 关联一个字段 需求:拿到老师这个类的属性 association : 关联,多对一 column : 数据库对应的列名 property : 对应属性名 javaType : 多对一字段对应的Java类型 select : 关联一个语句 --> <association column="tid" property="teacher" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from mybatis.teacher where id = #{id} </select> </mapper>
测试类
@Test public void getStudents(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); StudentDao mapper = sqlSession.getMapper(StudentDao.class); List<Student> students = mapper.getStudents(); for (Student student : students) { System.out.println("学生姓名:"+student.getName()+"\t老师姓名:"+student.getTeacher().getName()); } }
2.模拟面向对象的思想
<?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"> <!--namespace不能写别名!!!!!--> <mapper namespace="com.MLXH.dao.StudentDao"> <!-- 解决方式二:一个resultMap解决 , 模拟面向对象的思想--> <select id="getStudentsTwo" resultMap="StudentTeacher2"> select s.id,s.name,t.id as tid,t.name as tname from mybatis.student as s, mybatis.teacher as t where s.tid = t.id </select> <!--设置结果集映射ResultMap --> <resultMap id="StudentTeacher2" type="Student"> <id property="id" column="id"/> <result property="name" column="name"/> <!--直接关联一个老师--> <association property="teacher" javaType="Teacher"> <id property="id" column="tid"/> <result property="name" column="tname"/> </association> </resultMap> </mapper>
测试
@Test public void getStudentsTwo(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); StudentDao mapper = sqlSession.getMapper(StudentDao.class); List<Student> students = 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:
<select id="getStudent" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from student where 1 = 1 <!-- 使用参数一(是个自己的对象) --> <if test="student.id != null"> and id = #{student.id} </if> <!-- 使用参数二 String类型 --> <if test="age!= null"> and age = #{age} </if> </select>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)