(三)MyBatis 使用动态SQL实现CRUD(增删改查)

前言:为什么要使用动态SQL呢? 因为动态SQL更加灵活,可以实现更加复杂的CRUD功能。

基于上篇博客的基础之上我们使用动态sql来实现增删改查功能。为了方便查看,我删除了上片博客的正删改查的代码
StudentMapper.xml:






    
    
        
        
        
        
    




     


    



    
    
        update students
        
            
                name = #{pname},
            
            
                 sal = #{psal},
            
        
        where  id = #{pid}
    







    
        delete from students where id in
        
        
            #{ids}
        
    




    
        delete from students where id in
        
            #{ids}
        
    











    



    
    
        
        
            
                 id,
            
            
                name,
            
            
                 sal,
            
        
    


    
    
        
        
            
                #{id},
            
            
                #{name},
            
            
                #{sal},
            
        
    





    
    
        insert into students() values()
    

 

StudentDao.java:

package com.flyz.app01;

import com.flyz.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class StudentDao {
 

    //=================================以下是基于动态SQL的实现功能===============================================================
    /**
     * 有条件的查询所有学生
     */
    public List findAll(Integer id,String name,Double sal) throws Exception {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();

            Map map = new LinkedHashMap();
            map.put("pid", id);
            map.put("pname", name);
            map.put("psal", sal);

            return sqlSession.selectList(Student.class.getName() +".findAllByDym", map);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            MybatisUtil.closeSqlSession();
        }
    }


    /**
     * 有条件更新学生
     */
    public void dynaUpdate(Integer id,String name,Double sal) throws Exception {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();

            Map map = new HashMap();
            map.put("pid", id);
            map.put("pname", name);
            map.put("psal", sal);
            sqlSession.update(Student.class.getName() +".dynaUpdate", map);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        } finally {
            MybatisUtil.closeSqlSession();
        }
    }







    /**
     * 根据ID批量删除学生(数组版本)
     */
    public void dynaDeleteArray(int... ids) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            sqlSession.delete(Student.class.getName()+".dynaDeleteArray",ids);
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }
    /**
     * 根据ID批量删除学生(集合版本)
     */
    public void dynaDeleteList(List ids) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            sqlSession.delete("com.flyz.app01.Student.dynaDeleteList",ids);
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }




    /**
     * 插入学生
     */
    public void dynaInsert(Student student) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            sqlSession.insert(Student.class.getName()+".dynaInsert",student);
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }


}

以上需要说明的是,多条件查询的时候,可以只写其中一个条件或者全部写上均可

你可能感兴趣的:((三)MyBatis 使用动态SQL实现CRUD(增删改查))