zoukankan      html  css  js  c++  java
  • mybatis动态SQL--if--choose

    学习动态SQL

    先学习动态SQL中的if

    开始干活,先在接口里面写一个方法

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

    //动态SQL 。。。 if..
    	public List<GoodsInfo>  queryByIf(GoodsInfo a);
    

    /Mybatis02/config/mappers/GoodsInfoMapper.xml

    <!-- 下面是动态SQL -->
        <select id="queryByIf" resultType="com.chen.GoodsInfo">
        	select * from goods where 
        	<if test="name !=null">
        		name like '${name}%'
        	</if>
        </select>
    

    上面这个if标签的意思是,如果你传递过来的name不为空,那么就把下面 name like '${name}%' 拼接到 where的后面去。即变成,select * from goods where name like '${name}%'

    在主入口类 设置好对象的属性,以传值进去
    /Mybatis02/src/test/Start2.java

    package test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import com.chen.GoodsInfo;
    import com.chen.dao.GoodsDao;
    import com.chen.dao.GoodsDao2;
    
    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语句了;(直接调用接口方法)
    		//因为接口方法里需要传入一个对象,所以
    		GoodsInfo goods =new GoodsInfo();
    		//然后给对象插属性
    		goods.setId(123);
    		goods.setName("牛");
    		
    	     List list =dao.queryByIf(goods);
    		
    		System.out.println(list.size());
    		
    		//如果上面不设置自动提交表单,那么就需要commit方法
    		session.commit();
    	}
    
    }
    
    

    三个地方连起来就是表达了 select * from goods where name like '牛'% 这句话

    点击运行,查询到结果


    **因为name以牛开头的 ,数据库表里有三条,即有三个对象


    现在学习动态SQL的---Choose

    /Mybatis02/config/mappers/GoodsInfoMapper.xml

    <select id="queryByChoose" resultType="com.chen.GoodsInfo">
         <!-- 为了避免传入空值而where后没拼接语句造成的mysql语法错误,一般都是写select * from goods where 1=1 -->
        	select * from goods where 
        	<choose>
        		<when test="name !=null">
        			name like '${name}%'
        		</when>
        		
        		<when test="id !=null">
        		   id =${id}
        		</when>
        		
        		<otherwise>
        			order by name
        		</otherwise>
        	</choose>
        </select>
    

    在接口中添加一个新方法
    /Mybatis02/src/com/chen/dao/GoodsDao2.java

    //动态SQL 。。Choose
    	public List<GoodsInfo> queryByChoose(GoodsInfo a);
    

    在主入口类 设置好对象的属性,以传值进去
    /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语句了;(直接调用接口方法)
    		//因为接口方法里需要传入一个对象,所以
    		GoodsInfo goods =new GoodsInfo();
    		//然后给对象插属性
    
    		goods.setId(10);
    		goods.setName("牛");
    	     List list =dao.queryByChoose(goods);
    		System.out.println(list.size());
    		
    		//如果上面不设置自动提交表单,那么就需要commit方法
    		session.commit();
    	}
    
    }
    
    

    点击运行

    这表示List 里面 存放着 3个对象 , 就意味这 ,查询到了 三条数据。不信,我去mysql客户端 查下


    因为现在我们用的是choose标签 ,那么when 标签里写着
    <when test="id !=null"> id =${id} </when>
    假如我们只设置:goods.setId(10);,而把goods.setName("牛");注释掉,那么就只传入了id这个参数。然后即是相当于执行了
    select * from goods where id =${id}这条语句

    结果显示 只得到了一个对象,就意味着数据库查到的只有一条数据
    那么现在将这条语句放mysql客户端查一下 并验证

    验证成功,也是数据库查到的只有一条数据


  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/czy16/p/7629194.html
Copyright © 2011-2022 走看看