Mybatis映射文件之常用標(biāo)簽及特殊字符的處理方法
一、Mybatis映射文件 — resultMap標(biāo)簽
(1)新建一個(gè)Teacher類,如下
package com.mybatisstudy.pojo;
public class Teacher {
private int id;
private String teacherName;
public Teacher(){
}
public Teacher(int id,String name){
this.id = id;
this.teacherName = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
@Override
public String toString() {
return "Teacher[ " +
"id=" + id +
", teacherName='" + teacherName + '\'' +
" ]";
}
}數(shù)據(jù)庫里有如下記錄:

(2)新建一個(gè)TeacherMapper持久層接口
并且新增一個(gè)查詢所有用戶方法
package com.mybatisstudy.mapper;
import com.mybatisstudy.pojo.Teacher;
import java.util.List;
public interface TeacherMapper {
// 查詢所有記錄
List<Teacher> findAll();
}(3)新增TeacherMapper.xml Mybatis映射文件
<?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.mybatisstudy.mapper.TeacherMapper">
<select id="findAll" resultType="com.mybatisstudy.pojo.Teacher">
select * from teacher
</select>
</mapper>(4)新增testTeacherMapper測(cè)試類
import com.mybatisstudy.mapper.TeacherMapper;
import com.mybatisstudy.pojo.Teacher;
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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class TestTeacherMapper {
InputStream is = null;
SqlSession session = null;
TeacherMapper teacherMapper = null;
//前置方法,不必重復(fù)代碼
@Before
public void before() throws Exception {
System.out.println("前置方法執(zhí)行·············");
// (1)讀取核心配置文件
is = Resources.getResourceAsStream("SqlMapConfig.xml");
// (2)創(chuàng)建SqlSessionFactoryBuilder對(duì)象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// (3)SqlSessionFactoryBuilder對(duì)象獲取SqlSessionFactory對(duì)象
SqlSessionFactory factory = builder.build(is);
// (4)SqlSessionFactory對(duì)象獲取SqlSession對(duì)象
session = factory.openSession();
// (5)SqlSession對(duì)象獲取代理對(duì)象
teacherMapper = session.getMapper(TeacherMapper.class);
}
//后置方法,釋放資源
@After
public void after() throws Exception {
System.out.println("后置方法執(zhí)行·············");
session.close();
is.close();
}
// 測(cè)試查詢所有教師方法
@Test
public void testFindAll(){
List<Teacher> teachers = teacherMapper.findAll();
teachers.forEach(System.out::println);
}
}(5)運(yùn)行結(jié)果

哎呀,這是為什么呢,查詢有三條記錄后,但是我們的集合對(duì)象卻是為空, 原來是因?yàn)?/p>
MyBatis可以將數(shù)據(jù)庫結(jié)果集封裝到對(duì)象中,是因?yàn)榻Y(jié)果集的列名和對(duì)象屬性名相同
當(dāng)POJO屬性名和數(shù)據(jù)庫列名不一致時(shí),MyBatis無法自動(dòng)完成映射關(guān)系。
那有什么好辦法可以解決呢?
此時(shí)有兩種解決方案:
① Sql語句的查詢字段起與POJO屬性相同的別名。如下代碼
<select id="findAll" resultType="com.mybatisstudy.pojo.Teacher">
select tid id, tname teacherName from teacher
</select>測(cè)試結(jié)果:

OK,這次發(fā)現(xiàn)沒有問題了;
② 自定義映射關(guān)系:
在映射文件中,使用 <resultMap> 自定義映射關(guān)系;在 <select> 標(biāo)簽中,使用 resultMap 屬性代替 resultType 屬性,使用自定義映射關(guān)系。 如下:
<select id="findAll" resultMap="teacherMapper">
select * from teacher
</select>
<resultMap id="teacherMapper" type="com.mybatisstudy.pojo.Teacher">
<!-- id自定義主鍵列 properties:POJO屬性名 column:數(shù)據(jù)庫列名-->
<id property="id" column="tid"></id>
<!-- result定義普通列 property:POJO屬性名 column:數(shù)據(jù)庫列名-->
<result property="teacherName" column="tname"></result>
</resultMap>測(cè)試結(jié)果:

OK,本次測(cè)試結(jié)果也是沒有問題的。
二、Mybatis映射文件 — sql和include標(biāo)簽
<sql> 用來定義可重用的Sql片段,通過 <include> 引入該片段。如:Sql語句的查詢字段起與POJO屬性相同的別名,該Sql片段就可以重用。
(1)持久層新增根據(jù)Id查詢方法
// 根據(jù)ID查詢用戶
Teacher findById(int id);(2)映射文件新增相應(yīng)標(biāo)簽
<sql id="selectAllField">
select tid id,tname teacherName
</sql>
<select id="findById" resultType="com.mybatisstudy.pojo.Teacher">
<include refid="selectAllField"></include>
from teacher where tid=#{id}
</select>(3)測(cè)試類新增方法
// 測(cè)試根據(jù)ID查詢方法
@Test
public void testFindById(){
int id = 3;
Teacher teacher = teacherMapper.findById(id);
System.out.println(teacher);
}(4)測(cè)試結(jié)果

OK,本次測(cè)試也是非常成功的
三、Mybatis映射文件 — 特殊字符處理
| 符號(hào) | 實(shí)體 |
| < | < |
| > | > |
| & | & |
| ' | ' |
| " | " |
(1)持久層新增查詢比輸入ID大的集合
// 查詢比輸入Id要大的記錄
List<Teacher> findById2(int id);(2)映射文件新增標(biāo)簽
<select id="findById2" resultType="com.mybatisstudy.pojo.Teacher">
<include refid="selectAllField"></include>
from teacher where tid > #{id}
</select>(3)測(cè)試類新增測(cè)試方法
// 測(cè)試查詢比輸入Id大的記錄
@Test
public void testFindById2(){
int id = 1;
List<Teacher> teachers = teacherMapper.findById2(id);
teachers.forEach(System.out::println);
}(4)運(yùn)行結(jié)果

OK,確實(shí)查詢出了比id比1大的集合,本次實(shí)操也非常成功。
到此這篇關(guān)于Mybatis映射文件 — 常用標(biāo)簽及特殊字符的處理的文章就介紹到這了,更多相關(guān)Mybatis映射文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea中提示Class 'xxx' is never us
這篇文章主要介紹了idea中提示Class 'xxx' is never used的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
如何在Spring中使用編碼方式動(dòng)態(tài)配置Bean詳解
這篇文章主要給大家介紹了關(guān)于如何在Spring中使用編碼方式動(dòng)態(tài)配置Bean的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Spring?Boot如何監(jiān)控SQL運(yùn)行情況?
Druid是Java語言中最好的數(shù)據(jù)庫連接池,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot如何監(jiān)控SQL運(yùn)行情況的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
SpringBoot多數(shù)據(jù)源配置并通過注解實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源
本文主要介紹了SpringBoot多數(shù)據(jù)源配置并通過注解實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java Socket編程實(shí)現(xiàn)簡單的問候服務(wù)
這篇文章主要為大家介紹了Java Socket編程實(shí)現(xiàn)簡單的問候服務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01

