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}
详细请查看博客-->MyBatis的传入参数parameterType类型
实例
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>