建表sql语句:
CREATE TABLE `constant` (
`id` bigint(20) NOT NULL AUTO_INCREMENT ,
`key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`value` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`type` int(10) NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
表结构:
POJO:
public class Constant {
private Long id;
private String key;
private String value;
private Integer type;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key == null ? null : key.trim();
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value == null ? null : value.trim();
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
DAO层:
import java.util.Map;
import org.apache.ibatis.annotations.MapKey;
import org.springframework.stereotype.Repository;
import com.jm.model.Constant;
@Repository
public interface ConstantDao {
/**
* 注释@MapKey表示表中那个字段作为Map的key
* @return
*/
@MapKey("id")
Map<Long,Constant> loadConstant();
}
junitTest:
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.jm.dao.ConstantDao;
import com.jm.model.Constant;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" })
public class MubatisMapTest {
@Autowired
private ConstantDao constantDao;
@Test
public void mapTest() {
Map<Long,Constant> constantMap = constantDao.loadConstant();
System.out.println(constantMap);
}
}
当Mapper代码为:
<?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.jm.dao.ConstantDao">
<resultMap id="BaseResultMap" type="com.jm.model.Constant">
<result column="id" property="id" jdbcType="BIGINT" />
<result column="key" property="key" jdbcType="VARCHAR" />
<result column="value" property="value" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="INTEGER" />
</resultMap>
<select id="loadConstant" resultType="map">
select constant.id,constant.key,constant.value,constant.type from constant
</select>
</mapper>
执行结果是:
这时查询出来的map的value是map
当Mapper为:
<?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.jm.dao.ConstantDao">
<resultMap id="BaseResultMap" type="com.jm.model.Constant">
<result column="id" property="id" jdbcType="BIGINT" />
<result column="key" property="key" jdbcType="VARCHAR" />
<result column="value" property="value" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="INTEGER" />
</resultMap>
<select id="loadConstant" resultMap="BaseResultMap">
select * from constant
</select>
</mapper>
执行结果为:
这时查询出来的map的value是java对象