zoukankan      html  css  js  c++  java
  • 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();
    }
    }

  • 相关阅读:
    使用反射和HttpServlet类制作一个简单的web层框架
    [剑指offer]跳台阶问题&动态规划求解
    [剑指offer]旋转数组的最小值
    java实现大锤的自动校对程序(字节校招,字符串问题)
    栈结构的java实现&括号匹配问题
    单链表结构及链表反转操作java代码实现
    排序算法的java实现
    Ajax+JSON
    Jquery
    Filter+Listener
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/8544760.html
Copyright © 2011-2022 走看看