好947 Mybatis 配置resultMap 带参数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错
2014-07-09 00:37
966 查看
//TMD 写几个demo 还有大站采集<a target=_blank href="http://hao947.com/" target="_blank">好947</a>
映射配置文件
<!-- type:映射实体类的数据类型 id:resultMap的唯一标识 -->
<RESULTMAP id=BaseResultMap type="person"><pre name="code" class="java"><a target=_blank href="http://hao947.com/" target="_blank">好947</a> <!-- column:库表的字段名 property:实体类里的属性名 --> <ID property="personId" column="person_id" /> <RESULT property="name" column="name" /> <RESULT property="gender" column="gender" /> <RESULT property="personAddr" column="person_addr" /> <RESULT property="birthday"
column="birthday" /> </RESULTMAP>
映射配置查询语句
<!-- 当查询值得时候resultMap指定key与value。
map.put("name","项目经理")
map.put("gender" 1)<pre name="code" class="java"><a target=_blank href="http://hao947.com/" target="_blank">好947</a> --> <SELECT id=selectPersonByParams resultmap="BaseResultMap" parametertype="java.util.Map"> select * from person p where p.name = #{name} and p.gender =#{gender} </SELECT>
映射管理库表文件
<!-- 集中管理库表的映射文件 --><pre name="code" class="java"><a target=_blank href="http://hao947.com/" target="_blank">好947</a> <MAPPERS> <MAPPER resource="com/hao947/sql/mapper/PersonMapper.xml" /> </MAPPERS>
测试文件
}
映射配置文件
<!-- type:映射实体类的数据类型 id:resultMap的唯一标识 -->
<RESULTMAP id=BaseResultMap type="person"><pre name="code" class="java"><a target=_blank href="http://hao947.com/" target="_blank">好947</a> <!-- column:库表的字段名 property:实体类里的属性名 --> <ID property="personId" column="person_id" /> <RESULT property="name" column="name" /> <RESULT property="gender" column="gender" /> <RESULT property="personAddr" column="person_addr" /> <RESULT property="birthday"
column="birthday" /> </RESULTMAP>
映射配置查询语句
<!-- 当查询值得时候resultMap指定key与value。
map.put("name","项目经理")
map.put("gender" 1)<pre name="code" class="java"><a target=_blank href="http://hao947.com/" target="_blank">好947</a> --> <SELECT id=selectPersonByParams resultmap="BaseResultMap" parametertype="java.util.Map"> select * from person p where p.name = #{name} and p.gender =#{gender} </SELECT>
映射管理库表文件
<!-- 集中管理库表的映射文件 --><pre name="code" class="java"><a target=_blank href="http://hao947.com/" target="_blank">好947</a> <MAPPERS> <MAPPER resource="com/hao947/sql/mapper/PersonMapper.xml" /> </MAPPERS>
测试文件
@Test public void selectPersonByParams() { // 创建一个sqlSession <a target=_blank href="http://hao947.com" target="_blank">好947</a> SqlSession session = sqlSessionFactory.openSession(); try { Map<STRING, object=""> map = new HashMap<STRING, object="">(); map.put("name", "项目经理"); map.put("gender", 0); // 第一个参数查找sql,规则namespace+"."+sql的id,第二个参数是sql要接收的参数 List<PERSON> list = session .selectList("com.hao947.sql.mapper.PersonMapper.selectPersonByParams",map); for (Person p : list) { System.out.println(p); } } finally { session.close(); } }
}
相关文章推荐
- mybatis执行任意sql,不配置resultMap,返回结果集
- hao947 : Mybatis resultMap配置插入和主键自增返回 : 好947
- hao947 : Mybatis resultMap配置插入和主键自增返回 : 好947
- mybatis 查询 resultMap="" 只返回一条数据
- hibernate通过参数查询,格式化为map返回结果
- MyBatis的返回参数类型和查询结果遍历
- Mybatis连3表查询数据resultMap结果映射
- 创建一个包,其中定义一个过程,可以查询得到编号为7900的员工的姓名ename、薪水sal、佣金comm、上司编号mgr (通过OUT类型的参数将结果返回并打印) (注意包有包头部分的声明和包体部分的
- 【iBatis】使用resultMap配置返回结果时报错“列名无效”
- 在使用mybatis时遇到查询结果返回为空的情况,但是查数据库能查到
- Mybatis连3表查询数据resultMap结果映射
- Mybatis 配置resultMap 查询全部sql
- 数据库查询结果、Ibatis的ResultMap与JavaBean的对应关系
- MyBatis查询返回Map时设置值为null的字段也在结果集中
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- Mybatis对有可能返回empty set的数据库查询结果进行操作
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- Ibatis/mybatis中,查询SQL参数和返回都是Map的情况
- mybatis返回list很智能很简答的,只需要配置resultmap进行类型转换,你dao方法直接写返回值list<对应的object>就行了啊
- java:数据库查询结果类:ResultMap 屏蔽数据库设计细节,减弱强联系。