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>
  • 相关阅读:
    【C语言】23typedef
    C#蓝牙开发之查找设备以及配对
    GridView获取隐藏列的值
    PDA(Windows Mobile)调用远程WebService
    VS2008使用宏记录来实现自动增加注释信息
    CS 系统框架二[部分内容更新]
    GridView里面嵌套RadioButton
    .Net 以报表的形式加载SAP里面的数据
    取GridView的PagerTemplate里面的控件ID
    《深入Ajax架构和最佳实践》读书笔记
  • 原文地址:https://www.cnblogs.com/loaderman/p/10064445.html
Copyright © 2011-2022 走看看