Mybatis中 用法(主要用於一對多去重)


 

一、創建部門表和員工表:

創建部門信息表`t_department`,其中包括`id`, `name`

    CREATE TABLE t_department (
        id INT AUTO_INCREMENT,
        name VARCHAR(20) UNIQUE NOT NULL,
        PRIMARY KEY(id)
    ) DEFAULT CHARSET=UTF8;

往部門表中插入數據:

INSERT INTO t_department (name) VALUES 
        ('UI'), ('RD'), ('TEST');

 

創建員工信息表t_user

CREATE TABLE t_user(

  id INT PRIMARY KEY AUTO_INCREMENT,

  username VARCHAR(20) ,

  password VARCHAR(20) ,

  age int ,

  phone VARCHAR(20) ,

  email VARCHAR(20) ,

  is_Delete int

)DEFAULT CHARSET=UTF8;

往員工表中插入數據:

INSERT INTO t_user VALUES(null,'張三','123456',23,'110','11111@qq.com',1),(null,'歷史','123456',23,'110','11111@qq.com',1),(null,'孫悟空','123456',23,'110','11111@qq.com',2),(null,'李白','123456',23,'110','11111@qq.com',2),(null,'八戒','123456',23,'110','11111@qq.com',2);

 插入數據錯誤:

https://www.cnblogs.com/package-java/p/10494380.html

在查詢時,`<select>`節點必須指定結果的類型,可以通過`resultType`屬性來指定,也可以通過`resultMap`屬性來指定。
 
當有直接對應的查詢結果時,可以使用`resultType`,取值一般是實體類的類型,或VO類的類型。
 
某些查詢可能需要將查詢結果進行特殊的封裝,例如查詢時存在1對多、多對多、多對1等關系,則需要使用`resultMap`來配置封裝的方式。


二、創建實體類:

1、Vo類的實體類,因為部門只有三個,三個部門里面可能有很多員工,所以把員工封裝到List集合中,由於將多條信息明細存儲到了list中,因此查詢后將不再出現重復數據,達到了去重的效果

package cn.tedu.mybatis.vo;

import java.io.Serializable;
import java.util.List;

import cn.tedu.mybatis.entity.User;

public class DepartmentVO implements Serializable {

    private static final long serialVersionUID = -6442405812964981459L;

    private Integer did;
    private String name;
    private List<User> users;

    public Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @Override
    public String toString() {
        return "DepartmentVO [did=" + did + ", name=" + name + ", users=" + users + "]";
    }

}

2、員工類:

package cn.tedu.mybatis.entity;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 7323921614984096421L;

    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private String phone;
    private String email;
    private Integer isDelete;
    private Integer did;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(Integer isDelete) {
        this.isDelete = isDelete;
    }

    public Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + ", phone="
                + phone + ", email=" + email + ", isDelete=" + isDelete + ", did=" + did + "]";
    }
}

 

三、創建接口和抽象方法:

package cn.tedu.mybatis.mapper;

import cn.tedu.mybatis.vo.DepartmentVO;

public interface DepartmentMapper {
    
    DepartmentVO findById(Integer id);

}

四、SQL語句的映射:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
    "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="cn.tedu.mybatis.mapper.DepartmentMapper">
<!-- namespace:抽象類絕對路徑 -->
    <resultMap id="Department_VO_Map" 
        type="cn.tedu.mybatis.vo.DepartmentVO">
     <!-- id節點:配置主鍵 --> <!-- type:實體類絕對路徑 --> <!-- column:查詢結果中的列名 --> <!-- property:以上type屬性對應的數據類型中的屬性名(類里面的屬性) --> <id column="did" property="did"/> <!-- result節點:配置普通字段 --> <result column="name" property="name"/> <!-- collection節點:配置List集合類型的屬性,用於1對多的查詢 --> <!-- ofType:在List里放的是什么類型 --> <collection property="users" ofType="cn.tedu.mybatis.entity.User"> <id column="uid" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> <result column="age" property="age"/> <result column="phone" property="phone"/> <result column="email" property="email"/> <result column="isDelete" property="isDelete"/> </collection> </resultMap> <select id="findById" resultMap="Department_VO_Map"> SELECT t_department.id AS did, name, t_user.id AS uid, username, password, age, phone, email, is_delete FROM t_department INNER JOIN t_user ON t_user.did=t_department.id WHERE t_department.id=#{id} </select> </mapper>

> 以上代碼中,自定義別名是因為需要區分查詢結果中的列的名稱,並不是因為需要與數據類型中的屬性對應,關於查詢結果的列名與數據類型的屬性名的對應,可以通過`<resultMap>`中的配置來完成!

 

五、測試:

package cn.tedu.mybatis.mapper;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.tedu.mybatis.vo.DepartmentVO;

public class DepartmentMapperTestCase {

    AbstractApplicationContext ac;
    DepartmentMapper mapper;
    
    @Before
    public void doBefore() {
        ac = new ClassPathXmlApplicationContext("spring-dao.xml");
        mapper = ac.getBean("departmentMapper", DepartmentMapper.class);
    }
    
    @After
    public void doAfter() {
        ac.close();
    }
    
    @Test
    public void findById() {
        Integer id = 2;
        DepartmentVO data
            = mapper.findById(id);
        System.out.println(data);
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM