Mybatis映射文件之常用標簽及特殊字符的處理方法
一、Mybatis映射文件 — resultMap標簽
(1)新建一個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)新建一個TeacherMapper持久層接口
并且新增一個查詢所有用戶方法
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測試類
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; //前置方法,不必重復代碼 @Before public void before() throws Exception { System.out.println("前置方法執(zhí)行·············"); // (1)讀取核心配置文件 is = Resources.getResourceAsStream("SqlMapConfig.xml"); // (2)創(chuàng)建SqlSessionFactoryBuilder對象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); // (3)SqlSessionFactoryBuilder對象獲取SqlSessionFactory對象 SqlSessionFactory factory = builder.build(is); // (4)SqlSessionFactory對象獲取SqlSession對象 session = factory.openSession(); // (5)SqlSession對象獲取代理對象 teacherMapper = session.getMapper(TeacherMapper.class); } //后置方法,釋放資源 @After public void after() throws Exception { System.out.println("后置方法執(zhí)行·············"); session.close(); is.close(); } // 測試查詢所有教師方法 @Test public void testFindAll(){ List<Teacher> teachers = teacherMapper.findAll(); teachers.forEach(System.out::println); } }
(5)運行結(jié)果
哎呀,這是為什么呢,查詢有三條記錄后,但是我們的集合對象卻是為空, 原來是因為
MyBatis可以將數(shù)據(jù)庫結(jié)果集封裝到對象中,是因為結(jié)果集的列名和對象屬性名相同
當POJO屬性名和數(shù)據(jù)庫列名不一致時,MyBatis無法自動完成映射關(guān)系。
那有什么好辦法可以解決呢?
此時有兩種解決方案:
① Sql語句的查詢字段起與POJO屬性相同的別名。如下代碼
<select id="findAll" resultType="com.mybatisstudy.pojo.Teacher"> select tid id, tname teacherName from teacher </select>
測試結(jié)果:
OK,這次發(fā)現(xiàn)沒有問題了;
② 自定義映射關(guān)系:
在映射文件中,使用 <resultMap> 自定義映射關(guān)系;在 <select> 標簽中,使用 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>
測試結(jié)果:
OK,本次測試結(jié)果也是沒有問題的。
二、Mybatis映射文件 — sql和include標簽
<sql> 用來定義可重用的Sql片段,通過 <include> 引入該片段。如:Sql語句的查詢字段起與POJO屬性相同的別名,該Sql片段就可以重用。
(1)持久層新增根據(jù)Id查詢方法
// 根據(jù)ID查詢用戶 Teacher findById(int id);
(2)映射文件新增相應標簽
<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)測試類新增方法
// 測試根據(jù)ID查詢方法 @Test public void testFindById(){ int id = 3; Teacher teacher = teacherMapper.findById(id); System.out.println(teacher); }
(4)測試結(jié)果
OK,本次測試也是非常成功的
三、Mybatis映射文件 — 特殊字符處理
符號 | 實體 |
< | < |
> | > |
& | & |
' | ' |
" | " |
(1)持久層新增查詢比輸入ID大的集合
// 查詢比輸入Id要大的記錄 List<Teacher> findById2(int id);
(2)映射文件新增標簽
<select id="findById2" resultType="com.mybatisstudy.pojo.Teacher"> <include refid="selectAllField"></include> from teacher where tid > #{id} </select>
(3)測試類新增測試方法
// 測試查詢比輸入Id大的記錄 @Test public void testFindById2(){ int id = 1; List<Teacher> teachers = teacherMapper.findById2(id); teachers.forEach(System.out::println); }
(4)運行結(jié)果
OK,確實查詢出了比id比1大的集合,本次實操也非常成功。
到此這篇關(guān)于Mybatis映射文件 — 常用標簽及特殊字符的處理的文章就介紹到這了,更多相關(guān)Mybatis映射文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea中提示Class 'xxx' is never us
這篇文章主要介紹了idea中提示Class 'xxx' is never used的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01如何在Spring中使用編碼方式動態(tài)配置Bean詳解
這篇文章主要給大家介紹了關(guān)于如何在Spring中使用編碼方式動態(tài)配置Bean的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-05-05SpringBoot多數(shù)據(jù)源配置并通過注解實現(xiàn)動態(tài)切換數(shù)據(jù)源
本文主要介紹了SpringBoot多數(shù)據(jù)源配置并通過注解實現(xiàn)動態(tài)切換數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08