mybaits八:select查询返回map集合
生活随笔
收集整理的這篇文章主要介紹了
mybaits八:select查询返回map集合
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
返回一條紀(jì)錄的map
package com.atChina.dao;import java.util.List; import java.util.Map;import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.annotations.Param;import com.atChina.bean.Employee;public interface EmployeeMapper {// 返回一條紀(jì)錄的map, key是列名, value是對(duì)應(yīng)的值public Map<String,Object> getEmpByDepno(Integer depno); }?sql配置文件
<selectid="getEmpByDepno" resultType="map" >select * from DEPTTEST where deptno = #{deptno}</select>測(cè)試方法:
@Testpublic void test22() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession openSession = sqlSessionFactory.openSession();try{// 命名空間.id,這樣別的配置文件里有同名的id,程序也不報(bào)錯(cuò)EmployeeMapper em = openSession.getMapper(EmployeeMapper.class);System.out.println(em.getClass()); // 動(dòng)態(tài)代理類Map<String, Object> map = em.getEmpByDepno(10);System.out.println(map+" "+map.size());}finally{// 關(guān)閉openSession.close();}}?
?返回多條紀(jì)錄的map
package com.atChina.dao;import java.util.List; import java.util.Map;import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.annotations.Param;import com.atChina.bean.Employee;public interface EmployeeMapper {// 返回多條紀(jì)錄的map, key是主鍵, value是封裝后的javaBean@MapKey("deptno") // @Mapkey("告訴mybatis使用javaBean的某個(gè)屬性的值作為map的key")public Map<String,Employee> getEmpByDepnos(Integer depno); }?
<selectid="getEmpByDepnos" resultType="com.atChina.bean.Employee" >select * from DEPTTEST where deptno >= #{deptno}</select> @Testpublic void test23() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession openSession = sqlSessionFactory.openSession();try{// 命名空間.id,這樣別的配置文件里有同名的id,程序也不報(bào)錯(cuò)EmployeeMapper em = openSession.getMapper(EmployeeMapper.class);System.out.println(em.getClass()); // 動(dòng)態(tài)代理類Map<String, Employee> map = em.getEmpByDepnos(10);System.out.println(map+" "+map.size());}finally{// 關(guān)閉openSession.close();}}?
總結(jié)
以上是生活随笔為你收集整理的mybaits八:select查询返回map集合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybaits七:select查询返回l
- 下一篇: mybaits九:自定义结果映射规则