Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式
resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置
1、項(xiàng)目目錄

2、數(shù)據(jù)庫中的表的信息

3、配置文件的信息
1、SqlMapConfig.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis主配置文件-->
<configuration>
<!--配置環(huán)境-->
<environments default="mysql">
<!-- 配置mysql環(huán)境-->
<environment id="mysql">
<!-- 配置事務(wù)類型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置數(shù)據(jù)源(連接池)-->
<dataSource type="POOLED">
<!-- 配置數(shù)據(jù)庫的基本信息-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="111111"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每一個(gè)dao獨(dú)立的配置文件-->
<mappers>
<mapper resource="com/mybatis/dao/IUserDao.xml"/>
</mappers>
</configuration>
2、IUserDao.xml
其中的mapper標(biāo)簽中的namespace屬性指的就是持久層中的接口,這里的sql語句都是對應(yīng)這個(gè)接口中的方法,也就是指定了命名空間。
在這里resultMap標(biāo)簽是查詢結(jié)果的列名和實(shí)體類的屬性名的對應(yīng)關(guān)系,也就是說我們類中的屬性名不一定和數(shù)據(jù)庫中的保持一致,其中property配置的就是類中的屬性名,column設(shè)置的就是數(shù)據(jù)庫中表的字段名。
在sql語句的標(biāo)簽中之前的,resultType變成了resultMap。sql標(biāo)簽中直接寫的是就是sql語句,這個(gè)可以有效的避免重復(fù)的寫sql相同代碼,如果要引用sql標(biāo)簽中內(nèi)容,在對應(yīng)的語句中需要引用Include標(biāo)簽,具體的可以看下面的代碼。
<?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.mybatis.dao.IUserDao">
<!-- 配置,查詢結(jié)果的列名和實(shí)體類的屬性名的對應(yīng)關(guān)系-->
<resultMap id="userMap" type="com.mybatis.domain.User">
<!-- 主鍵字段對應(yīng)-->
<id property="userId" column="id"></id>
<!-- 非主鍵字段對應(yīng)-->
<result property="userName" column="username"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</resultMap>
<sql id="defaultUser">
select * from users
</sql>
<!-- 查詢所有-->
<select id="findAll" resultMap="userMap">
<include refid="defaultUser"></include>
</select>
<select id="findById" parameterType="INT" resultMap="userMap">
select * from users where id = #{uid}
</select>
</mapper>
4、User類
package com.mybatis.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer userId;
private String userName;
private Date userBirthday;
private String userSex;
private String userAddress;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userBirthday=" + userBirthday +
", userSex='" + userSex + '\'' +
", userAddress='" + userAddress + '\'' +
'}';
}
}
5、IUserDao接口
package com.mybatis.dao;
import com.mybatis.domain.User;
import java.util.List;
public interface IUserDao {
/**
* 查詢所有用戶
* @return
*/
List<User> findAll();
/**
* 根據(jù)ID查詢用戶信息
* @param userId
* @return
*/
User findById(Integer userId);
}
6、MybatisTest
package com.mybatis.test;
import com.mybatis.dao.IUserDao;
import com.mybatis.domain.User;
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.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class MybatisTest {
private InputStream in;
private SqlSession session;
private IUserDao userDao;
@Before
public void init() throws Exception {
this.in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
System.out.println(in);
SqlSessionFactory factory = factoryBuilder.build(in);
// this.session = factory.openSession(true);
this.session = factory.openSession();
this.userDao = session.getMapper(IUserDao.class);
}
@After
public void destory() throws IOException {
session.commit();
this.in.close();
this.session.close();
}
@Test
public void testFindAll() throws Exception{
List<User> users = userDao.findAll();
for (User user:users){
System.out.println(user);
}
}
}
7、運(yùn)行結(jié)果

resultMap標(biāo)簽的使用規(guī)則
自定義結(jié)果映射規(guī)則
<!-- resultMap自定義某個(gè)javabean的封裝規(guī)則
? ? ? ?type:自定義規(guī)則的java類型
? ? ? ?id:唯一id方便引用
? ? ?-->
? ? <resultMap type="entity.Employee" id="getEmpByIdMap">
? ? ? ?<!-- id指定主鍵列的封裝規(guī)則
? ? ? ? ? ?column:指定哪一列
? ? ? ? ? ?property:指定對應(yīng)的javabean屬性
? ? ? ? -->
? ? ? ?<id column="id" property="id"/>
? ? ? ?<!-- result定義普通列封裝規(guī)則,若屬性名與數(shù)據(jù)庫對應(yīng)表的列名相同可不寫,
? ? ? ? ? ? mybatis會(huì)自動(dòng)封裝,但建議將所有的映射規(guī)則都寫上
? ? ? ?-->
? ? ? ?<result column="name" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? </resultMap>
? ? <!-- public Employee getEmpById(Integer id) -->
? ? <select id="getEmpById" resultMap="getEmpByIdMap">
? ? ? ?select * from employee where id=#{id}
? ? </select>association聯(lián)合查詢
association可以指定聯(lián)合的javabean對象
property="dept":指定哪個(gè)屬性是聯(lián)合對象javaType:指定這個(gè)屬性的類型
<resultMap type="entity.Employee" id="getEmpAndDeptMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="empName" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? ? ?<!-- association可以指定聯(lián)合的javabean對象
? ? ? ? ? ? property="dept":指定哪個(gè)屬性是聯(lián)合對象
? ? ? ? ? ? javaType:指定這個(gè)屬性的類型-->
? ? ? ?<association property="dept" javaType="entity.Department">
? ? ? ? ? ?<id column="did" property="id"/>
? ? ? ? ? ?<result column="deptName" property="departmentName"/>
? ? ? ?</association>
? ? </resultMap>
? ? <!-- public Employee getEmpAndDept(Integer id) -->
? ? <select id="getEmpAndDept" resultMap="getEmpAndDeptMap">
? ? ? ?select e.id id,e.name empName,e.email email,e.sex sex,e.d_id d_id,
? ? ? ? ? ?d.id did,d.name deptName from employee e,dept d
? ? ? ? ? ?where e.d_id=d.id and e.id=#{id}
? ? </select>使用association進(jìn)行分布查詢
1、先按照員工id查詢員工信息將會(huì)調(diào)用查詢員工的sql
2、根據(jù)查詢員工信息中的d_id值去部門表中查出部門信息
3、部門設(shè)置到員工中
<resultMap type="entity.Employee" id="getEmpAndDeptStepMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="name" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? ? ?<!-- association定義關(guān)聯(lián)對象的封裝規(guī)則
? ? ? ? ? ? select:表明當(dāng)前屬性是調(diào)用select指定的方法查出的結(jié)果
? ? ? ? ? ? column:指定將那一列的值作為參數(shù)傳給這個(gè)方法
? ? ? ? ? ? ?流程:使用select指定的方法(傳入column指定的這列參數(shù)的值)查出對象,
? ? ? ? ? ? ?并封裝給property指定的屬性
? ? ? ? ? ? -->
? ? ? ? ? ? <!-- discriminator鑒別器
? ? ? ? ? ? ? ? ?column:指定判定的列名
? ? ? ? ? ? ? ? ?javaType:列值對應(yīng)的java類型
? ? ? ? ? ? ?-->
? ? ? ?<discriminator javaType="string" column="sex">
? ? ? ? ? ?<!-- resultType不能缺少 -->
? ? ? ? ? ?<case value="男" resultType="entity.Employee">
? ? ? ? ? ? ? <association property="dept" select="dao.DepartmentMapper.getDeptById"
? ? ? ? ? ? ? ? ? column="d_id">
? ? ? ? ? ? ? </association>
? ? ? ? ? ?</case>
? ? ? ?</discriminator>
? ? </resultMap>
? ? <!-- public Employee getEmpByIdStep(Integer id) -->
? ? <select id="getEmpByIdStep" resultMap="getEmpAndDeptStepMap">
? ? ? ?select * from employee where id=#{id}
? ? </select>嵌套結(jié)果集的方式,使用collection標(biāo)簽定義關(guān)聯(lián)的集合類型的屬性封裝規(guī)則
<resultMap type="entity.Department" id="getDeptByIdPlusMap">
? ? ? ?<id column="did" property="id"/>
? ? ? ?<result column="deptName" property="departmentName"/>
? ? ? ?<!-- collection定義關(guān)聯(lián)集合類型的屬性的封裝規(guī)則
? ? ? ? ? ? ofType:指定集合里面元素的類型 ? ? ? ? ? ??
? ? ? ? -->
? ? ? ?<collection property="emps" ofType="entity.Employee">
? ? ? ? ? ?<!-- 定義這個(gè)集合中元素的封裝規(guī)則 -->
? ? ? ? ? ?<id column="eid" property="id"/>
? ? ? ? ? ?<result column="empName" property="name"/>
? ? ? ? ? ?<result column="sex" property="sex"/>
? ? ? ? ? ?<result column="email" property="email"/>
? ? ? ?</collection>
? ? </resultMap>
? ? <!-- public Department getDeptByIdPlus(Integer id) -->
? ? <select id="getDeptByIdPlus" resultMap="getDeptByIdPlusMap">
? ? ? ?select d.id did,d.name deptName,e.id eid,
? ? ? ? ? ?e.name empName,e.sex,e.email
? ? ? ? ? ?from dept d left join employee e
? ? ? ? ? ?on d.id=e.d_id
? ? ? ? ? ?where d.id=#{id}
? ? </select>collection分步查詢
<resultMap type="entity.Department" id="getDeptByIdStepMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="name" property="departmentName"/>
? ? ? ?<collection property="emps" select="dao.EmployeeMapperPlus.getEmpsByDeptId"
? ? ? ? ? ?column="{id}">
? ? ? <!-- 或則 column="{deptId=id}"-->
? ? ? ?</collection>
? ? </resultMap>
? ?<!-- public List<Employee> getEmpsByDeptId(Integer deptId -->
? ?<select id="getEmpsByDeptId" resultType="entity.Employee">
? ? ? ?select * from employee where d_id=#{deptId}
? ? </select>
? ? <!-- public Department getDeptByIdStep(Integer id) -->
? ? <select id="getDeptByIdStep" resultMap="getDeptByIdStepMap">
? ? ? ?select * from dept where id=#{id}
? ? </select>當(dāng)分布查詢需要傳遞多個(gè)多個(gè)值時(shí),將多個(gè)值封裝map傳遞
colum=“{key1=column1,key2=colum2...}”以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)服務(wù)接入nacos注冊中心流程詳解
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)服務(wù)接入nacos注冊中心流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
JAVA WEB中Servlet和Servlet容器的區(qū)別
這篇文章主要介紹了JAVA WEB中Servlet和Servlet容器的區(qū)別,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
Java 常見的限流算法詳細(xì)分析并實(shí)現(xiàn)
大數(shù)據(jù)量高并發(fā)訪問時(shí),經(jīng)常出現(xiàn)服務(wù)或接口面對暴漲的請求而不可用的情況,甚至引發(fā)連鎖反映導(dǎo)致整個(gè)系統(tǒng)崩潰。此時(shí)你需要使用的技術(shù)手段之一就是限流,當(dāng)請求達(dá)到一定的并發(fā)數(shù)或速率,就進(jìn)行等待、排隊(duì)、降級(jí)、拒絕服務(wù)等。限流時(shí),常見算法是計(jì)數(shù)器、漏斗、令牌桶算法2022-04-04
5分鐘快速創(chuàng)建spring boot項(xiàng)目的完整步驟
這篇文章主要給大家介紹了關(guān)于通過5分鐘快速創(chuàng)建spring boot項(xiàng)目的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Maven3種打包方式中maven-assembly-plugin的使用詳解
這篇文章主要介紹了Maven3種打包方式中maven-assembly-plugin的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
解決java.lang.NullPointerException問題(空指針異常)
本文詳細(xì)介紹了Java中的NullPointerException異常及其常見原因,包括對象引用為null、數(shù)組元素為null和方法返回null等情況,文章還提供了幾種解決空指針異常的方法,如使用if語句、Optional類、三元運(yùn)算符和異常處理等,通過這些方法,可以有效地避免空指針異常2025-02-02
非常全面的Java?SpringBoot點(diǎn)贊功能實(shí)現(xiàn)
但是這些功能再項(xiàng)目中是高頻出現(xiàn)的,如果直接操作數(shù)據(jù)庫的話,對數(shù)據(jù)庫壓力太大。那遇到這個(gè)問題怎么解決?這篇文章主要給大家介紹了關(guān)于Java?SpringBoot點(diǎn)贊功能實(shí)現(xiàn)?的相關(guān)資料,需要的朋友可以參考下2022-01-01
MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表
這篇文章主要介紹了MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

