zoukankan      html  css  js  c++  java
  • 使用Map和模糊查询

    Map和模糊查询

    在某些时候我们只需要给MyBatis传递几个参数而不是一个完整的对象,如仅仅update表中的两三个属性。此时parameterType设置为一个pojo显然不合适。可以考虑使用Map

    mapper.xml

        <update id="updateName" parameterType="map">
            # 使用map传递参数在sql中直接取出key即可
            update mybatis.employee
            set last_name=#{last_name},
                email=#{email}
            where empid = #{empid}
        </update>
    
    
    //接口
    int updateName(Map map);
    

    测试类

    @Test
    	public void test2(){
    		SqlSession sqlSession = MyBatisUtil.getSqlSession();
    		EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
    
    		Map<String ,String> map= new HashMap();
    
    		map.put("empid","1002");
    		map.put("last_name","李商隐");
    		map.put("email","LiSy@163.com");
    
    		empMapper.updateName(map);
    
    		sqlSession.commit();
    		sqlSession.close();
    
    	}
    

    模糊查询例子

    1. mapper.xml

          <select id="getEmpListByName" resultType="com.maple.pojo.Employee">
              select *
              from mybatis.employee
              where last_name like #{value}
          </select>
      
    2. j接口

      	List<Employee> getEmpListByName(String value);
      
    3. 测试类

      	@Test
      	public void test3(){
      		SqlSession sqlSession = MyBatisUtil.getSqlSession();
      		EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
      
      		List<Employee> list = empMapper.getEmpListByName("%李%");
      
      		for(Employee employee : list){
      			System.out.println(employee);
      		}
      
      		sqlSession.commit();
      		sqlSession.close();
      
      	}
      
      
  • 相关阅读:
    用C语言编写生成小学四则运算程序
    每周学习报告
    读现代软件工程有感和自我介绍
    第七天
    第五天
    第六天
    作业九:课程总结
    作业四:结对编程项目--四则运算
    psp记录个人项目花费时间
    作业三:代码规范,代码复查
  • 原文地址:https://www.cnblogs.com/junlinsky/p/12810625.html
Copyright © 2011-2022 走看看