mybatis-普通sq增删改查学习笔记
admin
2023-04-30 19:03:43
0

import java.util.*;

import cn.mybatis.entity.Student;
import cn.mybatis.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

public class StudentDao {

    /**
     * 增加学生
     */
    public   void add(Student student) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            //事务开始(默认)
            //读取StudentMapper.xml映射文件中的SQL语句
            int i = sqlSession.insert(Student.class.getName()+".add",student);
            System.out.println("本次操作影响了"+i+"行");
            //事务提交
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            //事务回滚
            sqlSession.rollback();
            throw e;
        }finally{
            //MybatisUtil.closeSqlSession();
        }
    }

    /**
     * 根据ID查询学生
     */
    public Student findById(int id) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            Student student = sqlSession.selectOne(Student.class.getName()+".findById",id);
            sqlSession.commit();
            return student;
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }

    /**
     * 查询所有学生
     */
    public List findAll() throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.selectList(Student.class.getName()+".findAll");
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }finally{
            MybatisUtil.closeSqlSession();
        }
    }

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

    /**
     * 删除学生
     */
    public void delete(Student student) throws Exception{
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getSqlSession();
            sqlSession.delete(Student.class.getName()+".delete",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.add(new Student(3,"美丽",70030.3));
//        dao.add(new Student(4,"加油",70030.3));
//        dao.add(new Student(5,"关系",70030.3));
//        dao.add(new Student(6,"规律",70030.3));
//        dao.add(new Student(7,"古蔺",70030.3));

//     List studentslist = dao.findAll();
//        for (Student student : studentslist ) {
//            System.out.print(student.getId()+":"+student.getName()+":"+student.getSal());
//        }
//        Student student = dao.findById(4);
//        student.setName("liwen");
//        dao.update(student);
}
}





    
        
        
        
    

        
    
        insert into students(id,name,sal) values(#{id},#{name},#{sal})
    

    
    

    
    

    
    
        update students set name=#{name},sal=#{sal} where id=#{id}
    

    
    
        delete from students where id = #{id}
    

    
    

相关内容

热门资讯

男生偷拍女生被连夜开除,学历再... 据央视新闻报道,记者从南京审计大学获悉,经查,该校学生顾某某在校内偷拍他人隐私情况属实,且情节严重。...
“台独”顽固分子刘世芳外甥被台... 5月13日,国务院台办举行例行新闻发布会。有记者问:对于“台独”顽固分子刘世芳外甥颜文群被所在台企解...
1至4月全国铁路完成固定资产投... 【大河财立方消息】 5月13日,国铁集团发布的数据显示,今年1至4月,铁路建设优质高效推进,全国铁路...
了不起的河洛文化丨巩义的盛唐物... 巩义出土的唐三彩。 河南省文物考古研究院供图近日,郑州市文物考古研究院考古博物馆二楼报告厅举办了一场...
方太油烟机自动排烟故障 方太油烟机是一款高品质的厨房电器,它能够有效地吸收和排除厨房产生的油烟,保持室内空气的清新。然而,在...
方太油烟机的油烟怎么处理 方太油烟机是一种能够清除厨房油烟的设备,可以有效的净化厨房空气,减少油烟对人体的危害。但是,油烟机使...
方太油烟机尺寸为什么有大有小 在挑选抽油烟机时,抽油烟机的尺寸是重点考虑问题。如果购买到的抽油烟机尺寸过大,必然会占用太多空间,甚...
邻妹妹帮办|冷冻玉米长黑斑仍在... “玉米霉变非常明显,超市还在售卖!”近日,信阳市浉河区湖东街道辖区居民李女士向大河报·邻妹妹反映,其...
电视机安装高度 1、安装电视的观看距离应该至少为显示屏对角距离的3到5倍,安装高度建议以用户坐在椅子或沙发上眼睛平视...
安装构件的吊装高度包括 具体要看吊装设备的承载力,如果起重重量达100吨以上,那么这种构件的吊装高度起码要在75米左右。当然...