1. 程式人生 > >MyBatis中的jdbcType、resultType、resultMap、parameterType

MyBatis中的jdbcType、resultType、resultMap、parameterType

1 jdbcType

MyBatis的jdbcType是一個列舉類,有以下型別:

public enum JdbcType {
    ARRAY(2003),
    BIT(-7),
    TINYINT(-6),
    SMALLINT(5),
    INTEGER(4),
    BIGINT(-5),
    FLOAT(6),
    REAL(7),
    DOUBLE(8),
    NUMERIC(2),
    DECIMAL(3),
    CHAR(1),
    VARCHAR(12),
    LONGVARCHAR(-1),
    DATE(91),
    TIME(92),
    TIMESTAMP(93),
    BINARY(-2),
    VARBINARY(-3),
    LONGVARBINARY(-4),
    NULL(0),
    OTHER(1111),
    BLOB(2004),
    CLOB(2005),
    BOOLEAN(16),
    CURSOR(-10),
    UNDEFINED(-2147482648),
    NVARCHAR(-9),
    NCHAR(-15),
    NCLOB(2011),
    STRUCT(2002),
    JAVA_OBJECT(2000),
    DISTINCT(2001),
    REF(2006),
    DATALINK(70),
    ROWID(-8),
    LONGNVARCHAR(-16),
    SQLXML(2009),
    DATETIMEOFFSET(-155);
}

注意:使用的時候請全部大寫

2 resultType與resultMap

使用resultType進行輸出對映,只有查詢出來的列名和pojo中的屬性名一致,該列才可以對映成功。

如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個對映關係。

具體的例子程式碼在最下面

3 parameterType

3.1. MyBatis的傳入引數parameterType型別分兩種

   1. 1. 基本資料型別:int,string,long,Date;

   1. 2. 複雜資料型別:類和Map

3.2. 如何獲取引數中的值:

   2.1  基本資料型別:#{引數} 獲取引數中的值

   2.2  複雜資料型別:#{屬性名}  ,map中則是#{key}

例項

Cat.java

import java.sql.Timestamp;

public class Cat {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private Timestamp birthday;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

    public Timestamp getBirthday() {
        return birthday;
    }

    public void setBirthday(Timestamp birthday) {
        this.birthday = birthday;
    }
}

ICatDAO.java

import com.hand.domain.entity.Cat;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface ICatDAO {
    String getCatName(Integer id);
    void addCat(Cat person);
    Integer getCatsCount();
    List<Cat> listCats();
    List<Cat> findAllCats();
    void deleteCat(Integer id);
}

ICatDAO.xml

<?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="xxx.xxx.xxx.mapper.ICatDAO">
    <resultMap id="BaseResultMap" type="xxx.xxx.xxx.Cat" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="sex" property="sex" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="birthday" property="birthday" jdbcType="TIMESTAMP" />
    </resultMap>

    <select id="getCatName" resultMap="BaseResultMap">
        SELECT name FROM catinfo where id=#{id}
    </select>

    <select id="listCats" resultMap="BaseResultMap">
        SELECT * FROM catinfo
    </select>

    <select id="getCatsCount" resultType="java.lang.Integer">
        SELECT count(*) FROM catinfo
    </select>

    <insert id="addCat" parameterType="xxx.xxx.xxx.Cat">
        INSERT INTO catinfo (id, name) VALUES (#{id}, #{name}, #{sex}, #{age}, #{birthday})
    </insert>

    <delete id="deleteCat" parameterType="com.hand.domain.entity.Cat">
        DELETE FROM catinfo WHERE id=#{id}
    </delete>
</mapper>