mybatis动态sql更新语法

1.StudentDo

package cn.itcast.app05;

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

import org.apache.ibatis.session.SqlSession;

import cn.itcast.util.MybatisUtil;

public class StudentDao {
	
	
	public void dynaUpdate(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);
			sqlSession.update(Student.class.getName()+".dynaUpdate", map);
			sqlSession.commit();
		}catch(Exception e){
			e.printStackTrace();
			sqlSession.rollback();
			throw e;
		}finally{
			MybatisUtil.closeSqlSession();
		}
	}
	
	
	
	//测试
	public static void main(String[] args) throws Exception{
		StudentDao dao=new StudentDao();
		dao.dynaUpdate(1,null,9000D);
		
	}
}

2.映射文件






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



你可能感兴趣的:(mybatis)