03——mybatis结果反射&使用

一、结果映射是什么?

ResultMap

1.使用场景一

当数据库的列名和实体类的属性名不一致时,无法直接通过resultType接收查询结果.

那么我们可以是用resultMap属性应用一个结果映射,在结果映射中指定每一个列使用哪一个属性接收.

2. 使用场景二

可以通过resultMap配置对一关系和对多关系

二、如何使用结果映射?

在关联外部属性时,外部属性是一个对象就使用Association

Association:用于配置对一的关系

Property:指定本类中的属性名

Column:将自己的某一个列作为参数传入查询中

Select:调用外部查询获得数据

JavaType:本属性属于哪一种java类型

resultMap:引用其他的映射

Collection:用于配置对多的关系

Property:指定本类中的属性名

Column:将自己的某一个列作为参数传入查询中

Select:调用外部查询获得数据

ofType:本集合属性中的元素属于哪一种java类型

resultMap:引用其他的映射

在关联外部属性时,外部属性是一个集合就使用Collection



三、结果映射的使用

Sql

/*

Navicat MySQL Data Transfer


Source Server        : Linux_mysql

Source Server Version : 50622

Source Host          : 192.168.188.130:3306

Source Database      : db_mybatis


Target Server Type    : MYSQL

Target Server Version : 50622

File Encoding        : 65001


Date: 2019-01-06 18:21:21

*/


SET FOREIGN_KEY_CHECKS=0;


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

-- Table structure for dTable

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

DROP TABLE IF EXISTS `dTable`;

CREATE TABLE `dTable` (

  `dId` int(11) NOT NULL,

  `dDate` datetime DEFAULT NULL,

  `tId` int(11) DEFAULT NULL,

  PRIMARY KEY (`dId`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


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

-- Table structure for tTable

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

DROP TABLE IF EXISTS `tTable`;

CREATE TABLE `tTable` (

  `tId` int(11) NOT NULL,

  `tName` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`tId`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


项目结构



03——mybatis结果反射&使用_第1张图片


dTableMappers.java

package mappers;


import java.util.List;


import model.dTable;


public interface dTableMappers {

List selectAll();

}



tTableMappers.java

package mappers;


import model.tTable;


public interface tTableMappers {

public tTable selectById(Integer tId);

}



dTableMappers.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">

<mapper namespace="mappers.dTableMappers">


<resultMap id="dtableRM" type="dTable">

<id column="dId" property="dId"/>

<result column="dDate" property="dDate"/>

<result column="tId" property="tId"/>

resultMap>


<resultMap id="dtableRM_s" type="dTable" extends="dtableRM">

<association property="tTable" resultMap="mappers.tTableMappers.tTableRM" >association>

resultMap>


<select id="selectAll" resultMap="dtableRM_s">

select * from dTable t1,tTable t2 where t1.tId=t2.tId

select>


mapper>



tTableMappers.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">

<mapper namespace="mappers.tTableMappers">

<resultMap id="tTableRM" type="tTable">

<id column="tId" property="tId"/>

<result column="tName" property="tName"/>

resultMap>

<resultMap type="tTable" id="tTableRM_ds" extends="tTableRM">

<collection property="dTableList" resultMap="mappers.dTableMappers.dtableRM">collection>

resultMap>


<select id="selectById" parameterType="Integer" resultMap="tTableRM_ds">

select * from tTable t1 left join dTable t2 on t1.tId=t2.tId where t1.tId=#{tId}

select>

mapper>



dTable.java

package model;


public class dTable {

private Integer dId;

private String dDate;

private Integer tId;

private tTable tTable;


public dTable(Integer dId, String dDate, Integer tId) {

super();

this.dId = dId;

this.dDate = dDate;

this.tId = tId;

}


public dTable(String dDate, Integer tId) {

super();

this.dDate = dDate;

this.tId = tId;

}


public dTable() {

super();

}


public Integer getdId() {

return dId;

}


public void setdId(Integer dId) {

this.dId = dId;

}


public String getdDate() {

return dDate;

}


public void setdDate(String dDate) {

this.dDate = dDate;

}


public Integer gettId() {

return tId;

}


public void settId(Integer tId) {

this.tId = tId;

}


public tTable gettTable() {

return tTable;

}


public void settTable(tTable tTable) {

this.tTable = tTable;

}


@Override

public String toString() {

return "dTable [" +

"dId=" + dId + ", dDate=" + dDate +

", tId=" + tId +

", tname="+ tTable.gettName() +

" ]";

}

}



tTable.java

package model;


import java.util.List;


public class tTable {

private Integer tId;

private String tName;

private List dTableList;


public tTable() {

super();

}

public tTable(String tname) {

super();

this.tName = tname;

}


public tTable(Integer tId, String tname) {

super();

this.tId = tId;

this.tName = tname;

}

public String gettName() {

return tName;

}

public void settName(String tName) {

this.tName = tName;

}


public Integer gettId() {

return tId;

}


public void settId(Integer tId) {

this.tId = tId;

}


public List getdTableList() {

return dTableList;

}


public void setdTableList(List dTableList) {

this.dTableList = dTableList;

}


@Override

public String toString() {

return "tTable ["

+ "tId=" + tId +

", tname=" + tName +

"]";

}

}



dTableTest.java

package service;


import java.util.ArrayList;

import java.util.HashMap;

import org.apache.ibatis.session.SqlSession;


import mappers.dTableMappers;

import mappers.tTableMappers;

import model.dTable;

import model.tTable;

import util.SqlSessionFactoryUtil;


public class dTableTest {


public static void main(String[] args) {

SqlSession sqlSession = SqlSessionFactoryUtil.openSession();


/** 1-n*/

// tTableMappers tTableMp = sqlSession.getMapper(tTableMappers.class);

// tTable tTable = tTableMp.selectById(1);

// System.out.println("id = "+tTable.gettId());

// System.out.println("name = "+tTable.gettName());

//

// if(tTable.getdTableList() != null)

// for (dTable d : tTable.getdTableList() ) {

// System.out.println("did = "+d.getdId());

// System.out.println("dDate = "+d.getdDate());

// }


/** 1-1*/

// dTableMappers studentMapper = sqlSession.getMapper(dTableMappers.class);

//

// for (dTable s : studentMapper.selectAll()) {

// System.out.println(s);

// }

sqlSession.commit();

}


}



SqlSessionFactoryUtil.java

package util;


import java.io.InputStream;


import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class SqlSessionFactoryUtil {


private static SqlSessionFactory sqlSessionFactory;

public static SqlSessionFactory getSqlSessionFactory(){

if(sqlSessionFactory==null){

InputStream inputStream=null;

try{

inputStream=Resources.getResourceAsStream("mybatis-config.xml");

sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);

}catch(Exception e){

e.printStackTrace();

}

}

return sqlSessionFactory;

}

public static SqlSession openSession(){

return getSqlSessionFactory().openSession();

}

}

你可能感兴趣的:(03——mybatis结果反射&使用)