mybatis 参数传递总结

前一篇文章分析了下mybatis参数传递时的封装过程,这篇文章对参数传递各种情况做一个汇总
直接撸代码,太累了!!!
1、mapper Dao层

package com.dowson.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;

import com.dowson.bean.Emp;

public interface EmpMapper {

    //当不写注解时
    //public Emp getEmp(String empNo);
    //当写注解时
    public Emp getEmp(@Param(value="emp_no")String empNo);
    //当不写注解时
    //public Emp getEmp2(String empNo,String eName);
    //当写注解时
    public Emp getEmp2(@Param(value="empNo") String empNo,@Param(value="eName")String eName);
    //当不写注解时
    //public Emp getEmp3(Emp emp);
    //当写注解时
    public Emp getEmp3(@Param(value="e")Emp emp);
    //当不写注解时
    //public Emp getEmp4(Map<String,String> map);
    //当写注解时
    public Emp getEmp4(@Param(value="e")Map<String,String> map);
    //当不写注解时
    //public List<Emp> getEmp5(List<String> _list);
    //当写注解时
    public List<Emp> getEmp5(@Param(value="e") List<String> _list);
    //当不写注解时
    //public List<Emp> getEmp6(String[] arr);
    //当写注解时
    public List<Emp> getEmp6(@Param(value="arr_")String[] arr);
    //当不写注解时
    //public List<Emp> getEmp7(String empNo,Map<String,String> map,List<String> list);
    //当写注解时
    public List<Emp> getEmp7(@Param(value="empNo")String empNo,@Param(value="map")Map<String,String> map,@Param(value="e") List<String> list);  
}

service 层

package com.dowson.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dowson.bean.Emp;
import com.dowson.mapper.EmpMapper;
import com.dowson.service.util.UIPage;
import com.github.pagehelper.PageInfo;

@Service
public class EmpService {

    @Autowired
    private EmpMapper empMapper;

    public Emp getEmp(String empNo) {
        return empMapper.getEmp(empNo);
    }
    public Emp getEmp2(String empNo,String eName) {
        return empMapper.getEmp2(empNo,eName);
    }
    public Emp getEmp3(Emp emp) {
        return empMapper.getEmp3(emp);
    }
    public Emp getEmp4(Map<String,String> emp) {
        return empMapper.getEmp4(emp);
    }
    public List<Emp> getEmp5(List<String> list) {
        return empMapper.getEmp5(list);
    }
    public List<Emp> getEmp6(String[] arr) {
        return empMapper.getEmp6(arr);
    }

    public List<Emp> getEmp7(String empNo, Map<String, String> map, List<String> list) {
        return empMapper.getEmp7(empNo, map, list);
    }

}

Controller层

package com.dowson.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dowson.bean.Emp;
import com.dowson.service.EmpService;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping(value = "emp")
public class EmpController {

    @Resource
    private EmpService empService;

    @RequestMapping(value = "/getEmp", method = RequestMethod.GET)
    public Emp getEmpByEmpNo(@RequestParam(value="empNo")String empNo) {
        return empService.getEmp(empNo);
    }

    @RequestMapping(value = "/getEmp2", method = RequestMethod.GET)
    public Emp getEmpByEmpNo2(String empNo,String eName) {
        return empService.getEmp2(empNo,eName);
    }
    @RequestMapping(value = "/getEmp3", method = RequestMethod.GET)
    public Emp getEmpByEmpNo3() {
        Emp emp = new Emp();
        emp.setEmpNo("7369");
        emp.seteName("SMITH");
        return empService.getEmp3(emp);
    }
    @RequestMapping(value = "/getEmp4", method = RequestMethod.GET)
    public Emp getEmpByEmpNo4() {
        Map<String,String> map = new HashMap<String, String>();
        map.put("emp_No", "7369");
        map.put("eName", "SMITH");
        return empService.getEmp4(map);
    }
    @RequestMapping(value = "/getEmp5", method = RequestMethod.GET)
    public List<Emp> getEmpByEmpNo5() {
        List<String> list = new ArrayList<String>();
        list.add("7369");
        list.add("7788");
        return empService.getEmp5(list);
    }
    @RequestMapping(value = "/getEmp6", method = RequestMethod.GET)
    public List<Emp> getEmpByEmpNo6() {
        String[] arr = new String[]{"7369","7788"}; 
        return empService.getEmp6(arr);
    }
    @RequestMapping(value = "/getEmp7", method = RequestMethod.GET)
    public List<Emp> getEmpByEmpNo7() {
        String empNo = "7788";
        Map<String,String> map = new HashMap<String, String>();
        map.put("eName", "SCOTT");
        List<String> list = new ArrayList<String>();
        list.add("7788");
        return empService.getEmp7(empNo,map,list);
    }
}

EmpMapper.xml 文件
下文的sql用到了union查询,感觉和sb,在这里只是演示一下可以这样传参,实际情况不会这样写的。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dowson.mapper.EmpMapper">
    <!-- 当不写注解时 -->
    <!-- <select id="getEmp" parameterType="java.lang.String" resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{xxoo}
    </select> -->
    <select id="getEmp" parameterType="java.lang.String" resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{param1}
        union
        select *from emp where empNo = #{emp_no}
    </select>
    <!--两个字符串参数时  当不写注解时 -->
    <!-- <select id="getEmp2" parameterType="java.lang.String"
        resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{0} and eName = #{1}
        union
        select *from emp where empNo = #{param1} and eName = #{param2}
    </select> -->
    <select id="getEmp2" parameterType="java.lang.String"
        resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{param1} and eName =
        #{param2}
        union
        select *from emp where empNo = #{empNo} and eName =
        #{eName}
    </select>
    <!-- 当参数为一个对象时,当不写注解时 -->
    <!-- <select id="getEmp3" parameterType="com.dowson.bean.Emp"
        resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{param1.empNo} and eName = #{param1.eName}
    </select> -->

    <select id="getEmp3" parameterType="com.dowson.bean.Emp"
        resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{param1.empNo} and eName =
        #{param1.eName}
        union
        select *from emp where empNo = #{e.empNo} and
        eName =
        #{e.eName}
    </select>

    <!--当参数为一个Map集合时, 当不写注解时 -->
    <!-- <select id="getEmp4" parameterType="hashMap" resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{param1.emp_No} and eName = #{param1.eName}
    </select> -->

    <select id="getEmp4" parameterType="hashMap" resultType="com.dowson.bean.Emp">
        select*from emp where empNo = #{param1.emp_No} and eName =
        #{param1.eName}
        union
        select*from emp where empNo = #{e.emp_No} and
        eName = #{e.eName}
    </select>
    <!-- 当参数为一个list集合时,当不写注解时,此时要写 collection="list"或者collection="collection" -->
    <!-- <select id="getEmp5" parameterType="java.util.List" resultType="com.dowson.bean.Emp">
        select *from emp where empNo in
        <foreach collection="list" index="index" item="i" open="("
            separator="," close=")"> #{i} </foreach>
    </select> -->
    <select id="getEmp5" parameterType="java.util.List" resultType="com.dowson.bean.Emp">
        select *from emp where empNo in
        <!-- 或者 collection="e" -->
        <foreach collection="param1" index="index" item="i" open="("
            separator="," close=")">
            #{i}
        </foreach>
    </select>

   <!-- 当参数为一个String[]时 当不写注解时 -->
   <!-- <select id="getEmp6" parameterType="java.util.List" resultType="com.dowson.bean.Emp">
        select *from emp where empNo in
        <foreach collection="array" index="index" item="i" open="("
            separator="," close=")">
            #{i}
        </foreach>
    </select> -->

    <select id="getEmp6" parameterType="java.util.List" resultType="com.dowson.bean.Emp">
        select *from emp where empNo in
        <foreach collection="arr_" index="index" item="i" open="("
            separator="," close=")">
            #{i}
        </foreach>
    </select> 

<!--当参数为多个时, 当不写了注解时 -->
    <!-- <select id="getEmp7" parameterType="hashMap" resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{param1} and eName = #{param2.eName}
        and
        empNo in
        <foreach collection="param3" index="index" item="i" open="("
            separator="," close=")"> #{i} </foreach>
    </select> -->
    <select id="getEmp7" parameterType="hashMap" resultType="com.dowson.bean.Emp">
        select *from emp where empNo = #{empNo} and eName = #{map.eName} and
        empNo in
        <foreach collection="e" index="index" item="i" open="("
            separator="," close=")">
            #{i}
        </foreach>
    </select>
</mapper>    

总结:

mybatis (注:下文所提到param1,或者param2…,都是mybatis封装参数时自动生成的)

  1. 当方法只有一个参数(比如String 类型的)并且方法不加@param注解时,xml文件可以通过#{任意字符}取值,当加了注解时比如@param(value=”e”)可以通过#{e}取值,或者#{param1}取值。
  2. 当方法只有一个参数(比如对象类型的Emp)并且方法不加@param注解时,xml文件可以通过#{对象属性}取值,当加了注解时比如@param(value=”e”)时,xml可以通过#{e.对象属性}取值,或者#{param1.对象属性}取值。
  3. 当方法只有一个参数(比如Map 类型的)并且方法不加@param注解时,xml文件可以通过#{map的key}取值,当加了注解时比如@param(value=”e”)可以通过#{e.map的key}取值,或者#{param1.map的key}取值。
  4. 当方法有一个参数(比如List 类型的)并且方法不加@param注解时,可以通过foreach循环,此时要写collection=”list”或者collection=”collection”,当方法加了注解比如@param(value=”e”)此时要写collection=”e”或者要写collection=”param1”。
  5. 当方法有一个参数(比如String[] 类型的)并且方法不加@param注解时,可以通过foreach循环,此时要写collection=”array”,当方法加了注解比如@param(value=”e”)此时要写collection=”e”或者要写collection=”param1”。
  6. 当方法有多个参数(比如String 类型的)并且方法不加@param注解时,可以通过#{param1},#{param2}…取值,或者#{0},#{1}取值,否则会报错,当加了注解时比如@param(value=”e”)可以通过#{e}取值,或者#{param1},#{param2}…取值
    暂时就整理了这么多,可能还有遗漏,如有不对之处还请大家指正。

好了 睡觉!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值