无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

MyBatis其他方式來實現(xiàn)多表查詢的操作

2020-03-31 15:44 作者:匯智知了堂  | 我要投稿

MyBatis其他方式來實現(xiàn)多表查詢的操作


利用Java中的集合框架(List,Map)

其中List存儲多個查詢返回的記錄

Map查詢返回字段,同時記錄表中一條數(shù)據(jù)


<?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.zlt.mapper.StudentMapper_2">

?<select id="getStudent" resultType="java.util.HashMap">

? ? ? ? SELECT sid,sname,a.t_id,t_name FROM student a, teacher b WHERE a.t_id=b.t_id

?</select> </mapper>

---------------------------------------------------------------------------------------------------------------------------

package com.zlt.test;

import java.io.IOException;

import java.io.Reader;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

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 org.junit.Test;

import com.zlt.pojo.Student;

import com.zlt.pojo.Teacher;

import com.zlt.pojo.Team;

public class Test03 {

?private static SqlSessionFactorysqlSessionFactory;

?private static Reader reader;

?static {

?try {

? ? ? ? ? ? reader = Resources.getResourceAsReader("config.xml");

? ? ? ? ? ? sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

? ? ? ? } catch (IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

? ? @Test

?public void m01() {

? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();

? ? ? ? String sql = "com.zlt.mapper.StudentMapper.getStudent";

? ? ? ? List<Student> list = sqlSession.selectList(sql);

?for (Student student : list) {

? ? ? ? ? ? System.out.println(student.getSname() + "====" + student.getTeacher().getT_name());

? ? ? ? }

? ? ? ? sqlSession.close();

? ? }

? ? @Test

?public void m02() {

? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();

? ? ? ? String sql = "com.zlt.mapper.TeacherMapper.getTeacher";

? ? ? ? Teacher teacher = sqlSession.selectOne(sql);

? ? ? ? List<Student> student = teacher.getStudent();

?for (Student s : student) {

? ? ? ? ? ? System.out.println(teacher.getT_name() + "===" + s.getSname());

? ? ? ? }

? ? ? ? sqlSession.close();

? ? }

? ? @Test

?public void m03() {

? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();

? ? ? ? String sql = "com.zlt.mapper.StudentMapper_2.getStudent";

? ? ? ? List<Map<String, Object>> list = sqlSession.selectList(sql);

? ? ? ? System.out.println(list);

? ? ? ? sqlSession.close();

? ? }

}


MyBatis其他方式來實現(xiàn)多表查詢的操作的評論 (共 條)

分享到微博請遵守國家法律
中宁县| 万年县| 油尖旺区| 星子县| 苗栗县| 阿克| 琼中| 富宁县| 和顺县| 江孜县| 霍城县| 徐州市| 石狮市| 台北市| 云安县| 祁东县| 尉氏县| 乡宁县| 隆子县| 满城县| 甘泉县| 宁武县| 额济纳旗| 吉木萨尔县| 乐亭县| 西乌珠穆沁旗| 建瓯市| 砚山县| 蓬溪县| 黄陵县| 德兴市| 山丹县| 鄱阳县| 东阿县| 朔州市| 云霄县| 湛江市| 黎平县| 洛川县| 白山市| 霍邱县|