重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

mybatis动态SQL操作之插入学习笔记

1


import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.itcast.javaee.mybatis.util.MybatisUtil;

/**
 * 持久层 
 * @author AdminTC
 */
public class StudentDao {
    /**
     * 插入学生
     */
    public void dynaInsert(Student student) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            sqlSession.insert("studentNamespace.dynaInsert",student);
            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.dynaInsert(new Student(1,"哈哈",7000D));//insert into 表名(*,*,*) values(?,?,?)
        //dao.dynaInsert(new Student(2,"哈哈",null));//insert into 表名(*,*) values(?,?)
        //dao.dynaInsert(new Student(3,null,7000D));//insert into 表名(*,*) values(?,?)
        dao.dynaInsert(new Student(4,null,null));//insert into 表名(*) values(?)
    }
}

2




   

    
        
        
        
    

    
    
        
        
            
                students_id,
            
            
                students_name,
            
            
                students_sal,
            
        
    

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

    
    
        insert into students() values()
    



网站栏目:mybatis动态SQL操作之插入学习笔记
链接地址:http://cqcxhl.cn/article/jsphes.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP