• 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     }
  • 相关阅读:
    git push :推送本地更改到远程仓库的三种模式
    GIT版本库回滚【图文版】
    微服务架构的分布式事务解决方案
    断路器-Hystrix的深入了解
    Elasticsearch顶尖高手系列课程推荐
    Dockerfile分离构建LNMP环境部署wordpress
    ELK 聚合查询
    tomcat日志采集
    ELK采集之nginx 之高德地图出城市IP分布图
    Elasticsearch 安装与集群配置
  • 原文地址:https://www.cnblogs.com/wanerhu/p/10713813.html
Copyright © 2020-2023  润新知