• mybatis动态SQL--传入参数为集合,数组类型


    当接口方法的传入类型为List 或数组Array 时,我们该如何操作


    /**
    	 * 
    	 * 1.单个的参数Mybatis不会做特殊处理
    	 *     #{这里随便写什么都可以}    它都能把这里面的值取到
    	 * 2.传入对象POJO(普通的java类)..
    	 * 		#{对象的属性名称}
    	 * 3.多个参数。Mybatis会做特殊处理。会把传入的参数自动封装成Map类型
    	 * 		Map 的key值就是从param1...paramN ..
    	 * 		map.put("param1",name)
    	 * 		map.put("param2,id")
    	 *      @param("name") 可以使用这个注解 来自定义Map封装数据的key值。
    	 * 4.直接传入Map
    	 * 
    	 * 5.Collection(集合)类型(List,Set) ,数组。
    	 * 		Mybatis也会做特殊处理。。
    	 *		如果是List或者Set  封装到map中 
    	 *		如果是数组
    	 *		map.put("array",你传入的数组)
    	 */	
    

    先学习当传入参数是List

    /Mybatis02/src/com/chen/dao/GoodsDao2.java

    //批量操作  (返回影响了几条数据的一个int 数字)
    	public Integer deleteByList(List<GoodsInfo> list);
    

    然后把表映射ML文件 写好具体实现
    /Mybatis02/config/mappers/GoodsInfoMapper.xml

        <delete id="deleteByList">
        	delete from goods where id in
        	<foreach collection="list" open="(" separator="," close=")" item="haha">
        		#{haha}
        	</foreach>
        	
        </delete>
    

    现在我去数据库表goods里新插入3条数据

    
    public class Start2 {
    
    	public static void main(String[] args) throws IOException {
    		
    		String resource = "mybatis-conf.xml";
    		InputStream  inputStream = Resources.getResourceAsStream(resource);
    		//创建SqlSessionFactory
    		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    		//true表示自动提交。否则需要使用commit方法才会提交。默认是false
    		SqlSession session = sqlSessionFactory.openSession();
    		
    		//拿到接口的代理对象
    		GoodsDao2 dao=session.getMapper(GoodsDao2.class);
    		//拿到了dao这个对象接下来就可以创建sql语句了;(直接调用接口方法)
    		
    	     List list =new ArrayList();
    	     list.add("73"); //准备删除id为73的数据
    	     list.add("75"); //准备删除id为75的数据
    	     int x= dao.deleteByList(list); //这个方法有个int返回值,会告诉你已影响了多少条数据
    	     System.out.println(x);
    	   
    		//如果上面不设置自动提交表单,那么就需要commit方法
    		session.commit();
    	}
    
    }
    
    

    点击运行

    表示,影响了2条数据(既 删除了2条数据)
    现在查看下数据库表 发现

    表里 id=73 和id=75 的数据 已经被删除了

    成功

    现在学习当传入参数是Array

    /Mybatis02/src/com/chen/dao/GoodsDao2.java

    //(一般都是根据  id来删除数据 ,我的数据是varchar类型,对应的是string类型的数组
    	public Integer deleteByArray(String[] str );
    

    然后老套路第二步,在表映射XML文件里写好具体实现

    /Mybatis02/config/mappers/GoodsInfoMapper.xml

    <delete id="deleteByArray">
        delete from goods where id in
        <foreach collection="array" open="(" separator="," close=")" item="haha">
        		#{haha}
        </foreach>
        	
        </delete>
    

    老套路第三步,主入口类设置具体 传入值
    /Mybatis02/src/test/Start2.java

    
    public class Start2 {
    
    	public static void main(String[] args) throws IOException {
    		
    		String resource = "mybatis-conf.xml";
    		InputStream  inputStream = Resources.getResourceAsStream(resource);
    		//创建SqlSessionFactory
    		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    		//true表示自动提交。否则需要使用commit方法才会提交。默认是false
    		SqlSession session = sqlSessionFactory.openSession();
    		
    		//拿到接口的代理对象
    		GoodsDao2 dao=session.getMapper(GoodsDao2.class);
    		//拿到了dao这个对象接下来就可以创建sql语句了;(直接调用接口方法)
    	
    	
    		String[] strs={"101","102","103"};
    	         int x= dao.deleteByArray(strs); //这个方法有个int返回值,会告诉你已影响了多少条数据
    	         System.out.println(x);
    	   
    		//如果上面不设置自动提交表单,那么就需要commit方法
    		session.commit();
    	}
    
    }
    

    我现在在数据库表goods里新插入3条数据,它们都id我设为101,102,103看看等下能不能把他们删除

    点击运行

    输出结果3,表面已经影响了3条数据,那么是不是数据库相关信息已经被删了呢,现在看下数据库

    成功

  • 相关阅读:
    DGA域名可以是色情网站域名
    使用cloudflare加速你的网站隐藏你的网站IP
    167. Two Sum II
    leetcode 563. Binary Tree Tilt
    python 多线程
    leetcode 404. Sum of Left Leaves
    leetcode 100. Same Tree
    leetcode 383. Ransom Note
    leetcode 122. Best Time to Buy and Sell Stock II
    天津Uber优步司机奖励政策(12月28日到12月29日)
  • 原文地址:https://www.cnblogs.com/czy16/p/7631246.html
Copyright © 2020-2023  润新知