欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式

 更新時間:2022年01月10日 10:51:20   作者:借汝之光,得以光明  
這篇文章主要介紹了Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置

1、項目目錄

在這里插入圖片描述

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>
<!--    指定映射配置文件的位置,映射配置文件指的是每一個dao獨立的配置文件-->
    <mappers>
        <mapper resource="com/mybatis/dao/IUserDao.xml"/>
    </mappers>
</configuration>

2、IUserDao.xml

其中的mapper標(biāo)簽中的namespace屬性指的就是持久層中的接口,這里的sql語句都是對應(yīng)這個接口中的方法,也就是指定了命名空間。

在這里resultMap標(biāo)簽是查詢結(jié)果的列名和實體類的屬性名的對應(yīng)關(guān)系,也就是說我們類中的屬性名不一定和數(shù)據(jù)庫中的保持一致,其中property配置的就是類中的屬性名,column設(shè)置的就是數(shù)據(jù)庫中表的字段名。

在sql語句的標(biāo)簽中之前的,resultType變成了resultMap。sql標(biāo)簽中直接寫的是就是sql語句,這個可以有效的避免重復(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é)果的列名和實體類的屬性名的對應(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、運行結(jié)果

在這里插入圖片描述

resultMap標(biāo)簽的使用規(guī)則

自定義結(jié)果映射規(guī)則

<!-- resultMap自定義某個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會自動封裝,但建議將所有的映射規(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":指定哪個屬性是聯(lián)合對象
  • javaType:指定這個屬性的類型
<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":指定哪個屬性是聯(lián)合對象
? ? ? ? ? ? javaType:指定這個屬性的類型-->
? ? ? ?<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查詢員工信息將會調(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ù)傳給這個方法
? ? ? ? ? ? ?流程:使用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">
? ? ? ? ? ?<!-- 定義這個集合中元素的封裝規(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)分布查詢需要傳遞多個多個值時,將多個值封裝map傳遞

colum=“{key1=column1,key2=colum2...}”

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中將一個List等分成n個list的工具方法(推薦)

    java中將一個List等分成n個list的工具方法(推薦)

    下面小編就為大家?guī)硪黄猨ava中將一個List等分成n個list的工具方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • java實現(xiàn)簡單的爬蟲之今日頭條

    java實現(xiàn)簡單的爬蟲之今日頭條

    最近在學(xué)習(xí)搜索方面的東西,需要了解網(wǎng)絡(luò)爬蟲方面的知識,雖然有很多開源的強(qiáng)大的爬蟲,但本著學(xué)習(xí)的態(tài)度,想到之前在做資訊站的時候需要用到爬蟲來獲取一些文章,今天剛好有空就研究了一下.在網(wǎng)上看到了一個demo,使用的是Jsoup,我拿過來修改了一下,有需要的朋友可以參考
    2016-11-11
  • java編程下字符串的16位,32位md5加密實現(xiàn)方法

    java編程下字符串的16位,32位md5加密實現(xiàn)方法

    下面小編就為大家?guī)硪黄猨ava編程下字符串的16位,32位md5加密實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • 詳解Spring Boot使用Maven自定義打包方式

    詳解Spring Boot使用Maven自定義打包方式

    這篇文章主要介紹了Spring Boot使用Maven自定義打包方式,本文通過多種方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Java下載文件的四種方式詳細(xì)代碼

    Java下載文件的四種方式詳細(xì)代碼

    這篇文章介紹了Java下載文件的四種方式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • logback的isDebugEnabled日志配置級別源碼解析

    logback的isDebugEnabled日志配置級別源碼解析

    這篇文章主要為大家介紹了logback的isDebugEnabled日志配置級別源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • JDBC PreparedStatement Like參數(shù)報錯解決方案

    JDBC PreparedStatement Like參數(shù)報錯解決方案

    這篇文章主要介紹了JDBC PreparedStatement Like參數(shù)報錯解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • java格式化時間示例

    java格式化時間示例

    這篇文章主要介紹了java格式化時間示例,需要的朋友可以參考下
    2014-04-04
  • java中this與super關(guān)鍵字的使用方法

    java中this與super關(guān)鍵字的使用方法

    這篇文章主要介紹了java中this與super關(guān)鍵字的使用方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家徹底理解應(yīng)用java中this與super,需要的朋友可以參考下
    2017-09-09
  • java異步編程詳解

    java異步編程詳解

    這篇文章主要介紹了java異步編程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論