建表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`)
)
表结构:
表名称为:constant
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 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 constantMap = constantDao.loadConstant();
System.out.println(constantMap);
}
}
当Mapper代码为:
select constant.id,constant.key,constant.value,constant.type from constant
执行结果是:
Paste_Image.png
这时查询出来的map的value是map
当Mapper为:
select * from constant
执行结果为:
Paste_Image.png
这时查询出来的map的value是java对象