mybatis 参数传递总结
来源:互联网 发布:卸载加密软件 编辑:程序博客网 时间:2024/06/05 15:21
前一篇文章分析了下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;@Servicepublic 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封装参数时自动生成的)
- 当方法只有一个参数(比如String 类型的)并且方法不加@param注解时,xml文件可以通过#{任意字符}取值,当加了注解时比如@param(value=”e”)可以通过#{e}取值,或者#{param1}取值。
- 当方法只有一个参数(比如对象类型的Emp)并且方法不加@param注解时,xml文件可以通过#{对象属性}取值,当加了注解时比如@param(value=”e”)时,xml可以通过#{e.对象属性}取值,或者#{param1.对象属性}取值。
- 当方法只有一个参数(比如Map 类型的)并且方法不加@param注解时,xml文件可以通过#{map的key}取值,当加了注解时比如@param(value=”e”)可以通过#{e.map的key}取值,或者#{param1.map的key}取值。
- 当方法有一个参数(比如List 类型的)并且方法不加@param注解时,可以通过foreach循环,此时要写collection=”list”或者collection=”collection”,当方法加了注解比如@param(value=”e”)此时要写collection=”e”或者要写collection=”param1”。
- 当方法有一个参数(比如String[] 类型的)并且方法不加@param注解时,可以通过foreach循环,此时要写collection=”array”,当方法加了注解比如@param(value=”e”)此时要写collection=”e”或者要写collection=”param1”。
- 当方法有多个参数(比如String 类型的)并且方法不加@param注解时,可以通过#{param1},#{param2}…取值,或者#{0},#{1}取值,否则会报错,当加了注解时比如@param(value=”e”)可以通过#{e}取值,或者#{param1},#{param2}…取值
暂时就整理了这么多,可能还有遗漏,如有不对之处还请大家指正。
好了 睡觉!!!
阅读全文
0 0
- Mybatis参数传递总结
- mybatis传递参数总结
- mybatis 参数传递总结
- MyBatis-----MyBatis传递数组参数
- mybatis传递多种参数
- mybatis 之参数传递
- mybatis传递参数
- mybatis传递参数
- mybatis多参数传递
- Mybatis参数传递记录
- MyBatis多参数传递
- Mybatis参数传递
- MyBatis参数传递
- Mybatis参数传递
- Mybatis参数传递
- Mybatis参数传递
- Mybatis 传递参数示例
- mybatis参数传递
- solr6.3与MySQL结合使用
- 【前端知识点】promise简书-30分钟带你搞懂promise面试必备
- FFmpeg源码剖析-框架:process_input()
- jquery使用append动态增加元素,该元素绑定事件失效的解决办法
- 个人笔记1
- mybatis 参数传递总结
- 3Sum--LeetCode
- HDU
- 32. Longest Valid Parentheses
- JavaScript连续赋值的思考
- redis事物的开启和终止
- nginx第三方模块---nginx-sticky-module的使用(基于cookie的会话保持)
- SpringMvc三大组件详解
- HTTP学习(1)