• Mybaits多个参数的传递


    今天介绍是多个参数传递到映射xml,进行CURD操作

    一.使用参数映射的方法进行传递

     1在接口写对应的方法

    public interface EmployeeMapper {

    public Employee getEmpByIdAndLastName(@Param("id")Integer id,@Param("lastName")String lastName);


    }

    2在xml里配置对应的映射

    <!-- public Employee getEmpByIdAndLastName(Integer id,String lastName);-->
    <select id="getEmpByIdAndLastName" resultType="com.atguigu.mybatis.bean.Employee">
    select * from tbl_employee where id = #{id} and last_name=#{lastName}
    </select>

    3在juit进行测试

    @Test
    public void test04() throws IOException{

    SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    //1、获取到的SqlSession不会自动提交数据
    SqlSession openSession = sqlSessionFactory.openSession();

    try{
    EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
    Employee employee = mapper.getEmpByIdAndLastName(1, "tom");
    System.out.println(employee);
    }finally{
    openSession.close();
    }
    }

    二 使用map进行多个参数的传递

     1在接口写对应的方法

    public Employee getEmpByMap(Map<String, Object> map);

    2在xml里配置对应的映射

    <!-- public Employee getEmpByMap(Map<String, Object> map); -->
    <select id="getEmpByMap" resultType="com.atguigu.mybatis.bean.Employee">
    select * from ${tableName} where id=${id} and last_name=#{lastName}
    </select>

    3在juit进行测试

    @Test
    public void test04() throws IOException{

    SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    //1、获取到的SqlSession不会自动提交数据
    SqlSession openSession = sqlSessionFactory.openSession();

    try{
    EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
    Map<String, Object> map = new HashMap<>();
    map.put("id", 2);
    map.put("lastName", "Tom");
    map.put("tableName", "tbl_employee");
    Employee employee = mapper.getEmpByMap(map);
    System.out.println(employee);
    }finally{
    openSession.close();
    }
    }

  • 相关阅读:
    ASP.NET MVC 学习笔记 1
    安装xp遇到的问题与如何连接共享的打印机
    win8.1 安装
    AspxGridView控件的使用
    JS获取fileupload文件全路径
    正则表达式的学习
    回归起点
    Vmware ESX 5.0 安装与部署
    UITextField检测输入内容不能有空格的处理
    关于cell自动布局约束实现高度自适应问题
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/8544760.html
Copyright © 2020-2023  润新知