zoukankan      html  css  js  c++  java
  • mybatis之动态SQL操作之查询

    1)  查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL

    /**
     * 持久层
     * @author AdminTC
     */
    public class StudentDao {
        /**
         * 动态SQL--查询
         */
        public List<Student> dynaSQLwithSelect(String name,Double sal) throws Exception{
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            try{
                Map<String,Object> map = new LinkedHashMap<String, Object>();
                map.put("pname",name);
                map.put("psal",sal);
                return sqlSession.selectList("mynamespace.dynaSQLwithSelect",map);
            }catch(Exception e){
                e.printStackTrace();
                sqlSession.rollback();
                throw e;
            }finally{
                sqlSession.commit();
                MyBatisUtil.closeSqlSession();
            }
        }
        public static void main(String[] args) throws Exception{
            StudentDao dao = new StudentDao();
            
            List<Student> studentList1 = dao.dynaSQLwithSelect("哈哈",null);
            for(Student student : studentList1){
                System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
            }
            System.out.println("--------------");
            List<Student> studentList2 = dao.dynaSQLwithSelect(null,7000D);
            for(Student student : studentList2){
                System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
            }
            System.out.println("--------------");
            List<Student> studentList3 = dao.dynaSQLwithSelect("哈哈",7000D);
            for(Student student : studentList3){
                System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
            }
            System.out.println("--------------");
            List<Student> studentList4 = dao.dynaSQLwithSelect(null,null);
            for(Student student : studentList4){
                System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
            }
            System.out.println("--------------");
        }
    }

        StudentMapper.xml

    <?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="mynamespace">
        <select id="dynaSQLwithSelect" parameterType="map" resultType="loaderman.Student">
            select id,name,sal from students
            <where>
                <if test="pname!=null">
                    and name=#{pname}
                </if>
                <if test="psal!=null">
                    and sal=#{psal}
                </if>
            </where>     
        </select>
    </mapper>
  • 相关阅读:
    enmo_day_07
    enmo_day_04
    enmo_day_05
    数据仓库的模型设计
    Lucene 概念,定义应用场景
    enum 枚举的简单应用
    单例模式&synchronized
    Spark的 DAGschedule & task schedule 区别以及相互联系
    Spark的stage & job & task 到底是什么 ,以及划分原理
    Java基本数据类型&引用类型总结
  • 原文地址:https://www.cnblogs.com/loaderman/p/10064445.html
Copyright © 2011-2022 走看看