Mybatis大家都知道吧,对于数据库来说,它可以说是最流行的框架,那你们知道mybatis该如何返回map结果集吗?跟小编一起来了解了解吧。
例:数据库为MySQL 使用mybatis返回map
首先创建好我们的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(); }
测试类:
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>
结果:这时候的mapper,它的value就是java对象了。
以上就是今天的全部内容了,如果想要了解更多java架构师相关内容的话,就请持续关注我们的网站吧。
推荐阅读: