• MyBatis(四)多参数处理问题


    这里总结了关于多参数传递时,MyBatis接收的三种方式。

    (1)接口中编写方法

    1   public Emp getEmpByParams(Integer id,String lastNmae);
    2     
    3     public Emp getEmpByParam(@Param("id")Integer id,@Param("lastName")String lastNmae);
    4     
    5     public Emp getEmpByParam(Map<String, Object> map);

    (2)编写Mapper文件

      <select id="getEmpByParams" resultType="com.eu.bean.Emp">
            select id,last_name lastName,gender geder,email from Emp where id = #{param1} and last_name= #{param2}
       </select>
      <select id="getEmpByParam" resultType="com.eu.bean.Emp">
            select id,last_name lastName,gender geder,email from Emp where id = #{id} and last_name= #{lastName}
        </select>

     (3)编写测试

     1   public SqlSessionFactory getSqlSessionFactory() throws IOException {
     2         String resource = "conf/mybatis-config.xml";
     3         InputStream inputStream = Resources.getResourceAsStream(resource);
     4         return new SqlSessionFactoryBuilder().build(inputStream);
     5     }
     6   @Test
     7     public void testMapperPar() throws IOException {
     8         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
     9         //1.获取到sqlsession 不会自动提交数据
    10         SqlSession openSession = sqlSessionFactory.openSession();
    11         EmpDao mapper = openSession.getMapper(EmpDao.class);
    12         
    13         Emp emp = mapper.getEmpByParam(1, "张三");
    14         System.out.println(emp);
    15         
    16         //手动提交数据
    17         openSession.commit();
    18         openSession.close();
    19     }
    20     
    21     @Test
    22     public void testMapperMap() throws IOException {
    23         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    24         //1.获取到sqlsession 不会自动提交数据
    25         SqlSession openSession = sqlSessionFactory.openSession();
    26         EmpDao mapper = openSession.getMapper(EmpDao.class);
    27         
    28         Map<String, Object> map=new HashMap<String, Object>();
    29         map.put("id", 1);
    30         map.put("lastName", "张三");
    31         Emp emp = mapper.getEmpByParam(map);
    32         System.out.println(emp);
    33         
    34         //手动提交数据
    35         openSession.commit();
    36         openSession.close();
    37     }
  • 相关阅读:
    CnSharp代码生成器。
    C#后台调用前台javascript的五种方法
    winfrom如何做一个语法着色控件
    Delphi AdvStringGrid表格保存和TClientDataSet发生关系的构想。
    Oracle 修改数据库字段的类型的语句
    structs 标签库(html)(转帖)
    Android Unable to resolve target 'android8'
    Android SDL_app: emulatorarm.exe 应用程序错误
    如何MyEclipse中显示WEBINF文件夹下的classes目录以及目录中的class文件
    Android conversion to dalvik format failed with error 1的解决办法
  • 原文地址:https://www.cnblogs.com/wanerhu/p/10713813.html
Copyright © 2020-2023  润新知