使用MyBatis联表查询
创建实体类Student、Teacher、Subject;
实体类中有属性、getter and setter、无参有参构造方法、toString()
注意:如果在对应mapper中启用了二级缓存,则对应的实体类要实现序列化Serializable接口;
11)创建mapper文件StudentMapper.xml、TeacherMapper.xml、SubjectMapper.xml,
在各个mapper文件中编写通用结果集映射,编写查询所有表数据的sql。
12)在plsql developer工具中编写多表查询的语句,正确查询到学生和教师信息;
13)在实体类Student中添加教师类型属性,类型是Teacher,重新生成getter and setter、无参有参构造、toString();
14)在StudentMapper中编写,写入多表查询语句;使用resultMap指定结果集映射;
编写新的结果集映射resultMap,返回类型是Student类路径。其中包含学生与教师的字段-属性映射关系,通过使用
包含教师的字段-属性映射关系;
15)创建dao层接口,编写抽象方法,方法名与mapper中id一致;
16)创建service层接口和实现类,在实现类中创建dao层对象并调用多表查询方法;
17)编写测试类,创建service层对象并调用多表查询方法,输出返回结果,查看学生和教师信息。
代码 StudentMapper
<?xml version="1.0" encoding="UTF-8"?><!--通过结果集映射 表中字段名称与实体类属性名称对应-->
<resultMap id="BaseResultMap" type="com.gxy.entity.Student">
<!--配置主键列 column列名 property-->
<id column="stu_id" property="id"></id>
<!--配置非空主键-->
<result column="stu_name" property="name"></result>
<result column="stu_age" property="age"></result>
<result column="stu_gender" property="gender"></result>
<result column="tea_id" property="teaid"></result>
<result column="stu_create_date" property="date"></result>
</resultMap>
<!--查询list-->
<select id="selectStudent" resultMap="BaseResultMap">
select * from student
</select>
select s.*,t.* from student s,teacher t where s.tea_id=t.t_id
应该注意 ![namespace里是接口的类路径 ](https://img-blog.csdnimg.cn/20210409155614184.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjUwMDYzMQ==,size_16,color_FFFFFF,t_70)
代码 teacherMapper
<?xml version="1.0" encoding="UTF-8"?><!--通过结果集映射 表中字段名称与实体类属性名称对应-->
<resultMap id="BaseResultMap" type="com.gxy.entity.Teacher">
<!--配置主键列 column列名 property-->
<id column="t_id" property="id"></id>
<!--配置非空主键-->
<result column="t_name" property="name"></result>
<result column="t_profession" property="profession"></result>
</resultMap>
<!--查询list-->
<select id="selectTeacher" resultMap="BaseResultMap">
SELECT * from teacher
</select>
![dao接口是方法 也是sql语句里的id](https://img-blog.csdnimg.cn/20210409160003799.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjUwMDYzMQ==,size_16,color_FFFFFF,t_70) ![service接口与dao方法一样](https://img-blog.csdnimg.cn/20210409160106187.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjUwMDYzMQ==,size_16,color_FFFFFF,t_70)
代码 impl
package com.gxy.service.impl;
import com.gxy.dao.IStudentDao;
import com.gxy.entity.Student;
import com.gxy.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
/**
- Created by lenovo on 2021/4/9.
*/
public class IStudentserviceImpl implements IStudentService {
@Override
public List selectOne2one() {
SqlSession session = MyBatisUtil.getSqlSession();
//创建dao层对象 代理模式(反射)
IStudentDao dao = session.getMapper(IStudentDao.class);
//调用dao层提供的方法
List list = dao.selectOne2one();
//关闭资源
MyBatisUtil.close();
return list;
}
}
代码 test测试类
package com.gxy.test;
;import com.gxy.entity.Student;
import com.gxy.service.impl.IStudentService;
import com.gxy.service.impl.IStudentserviceImpl;
import java.io.IOException;
import java.util.List;
/**
- Created by lenovo on 2021/4/9.
*/
public class Test {
public static void main(String[] args) throws IOException {
IStudentService i = new IStudentserviceImpl();
//调用方法
List a=i.selectOne2one();
System.out.print(a);
}
}_
特别注意
**不要删iml文件 如果删了 欢迎看下一篇博客
**需要一些架包