mybatis学习六 多参数查询(二)Map封装多参数
2017-03-29 00:00
316 查看
这种方式用得比较多,可以用于封装框架
由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
1. 编写接口多参数方法
/** * 多参数查询,注意要和Employeer.xml的方法名对应 */ public List<Employeer> findEmployeerByNameandDep1(Map<String,String> map);
2. 编写resultMap
<!-- 定义数据库字段与实体对象的映射关系 --> <resultMap type="Employeer" id="resultMap"> <id property="employeer_id" column="employeer_id"/> <result property="employeer_name" column="employeer_name"/> <result property="employeer_age" column="employeer_age"/> <result property="employeer_department" column="employeer_department"/> <result property="employeer_worktype" column="employeer_worktype"/> </resultMap>
3. 编写sql语句
<!--多参数 查找 返回list 的select 语句,注意 resultMap 的值是指向前面定义好的,注意key1和key2为传入的map的键值 --> <select id="findEmployeerByNameandDep1" parameterType="map" resultType="Employeer"> select* from `t_employeer` where employeer_name=#{key1} and employeer_department=#{key2} </select>
由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
4. 编写查询方法
/** * 多参数查询列表,使用map */ public static List<Employeer> getEmployeerList(Map<String, String> map){ SqlSession session = null; List<Employeer> employeers=null; try { session = sqlSessionFactory.openSession(); EmployeerMapper employeerMapper=session.getMapper(EmployeerMapper.class); employeers = employeerMapper.findEmployeerByNameandDep1(map); session.commit() ; } finally { session.close(); } return employeers; }
5. 编写测试类
System.out.println("=========================使用多单参数map方式查询==========================="); Map<String, String> map = new HashMap<String, String>(); map.put("key1", "明明"); map.put("key2", "财会部"); List<Employeer> employeers2=getEmployeerList(map); for(Employeer employeer2:employeers2){ System.out.println(employeer2); }
相关文章推荐
- MyBatis学习笔记-注解SQL多个参数查询异常处理
- MyBatis中的动态SQL,实现不确定条件的CRUD-----使用Map来传递查询的参数
- Ibatis/mybatis中,查询SQL参数和返回都是Map的情况
- mybatis级联查询传参(map保存多参数传参)
- Mybatis中查询的参数为Map条件中是in 数据组参数
- mybatis学习之查询结果返回List以及Map
- mybatis 查询参数为集合list、数组、map的情况
- MyBatis学习总结_13_Mybatis查询之resultMap和resultType区别
- mybatis 传递多个参数 --解决mybatis查询使用多个参数方法--javabean传统方法和map方法
- MyBatis学习总结_13_Mybatis查询之resultMap和resultType区别
- mybatis学习五 多参数查询(一)
- mybatis学习之多表关联查询和resultmap的常用标签和属性 和分页查询
- mybatis中传入一个List或Map集合作为查询条件的参数
- Mybatis 分页查询数据(借用map来封装参数)
- 【MyBatis学习07】输出类型resultType及输出参数映射resultMap
- MyBatis学习之SQL查询参数以及增删改操作
- Mybatis中查询的参数为Map条件中是in 数据组参数
- Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询
- 好947 Mybatis 配置resultMap 带参数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错
- 在mybatis查询时使用map以及list参数