mybatis-普通sq增删改查学习笔记
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,import java.util.*;import cn.mybatis.entity.Student;import cn.mybatis.util.MybatisUtil;import org.ap
千家信息网最后更新 2026年10月31日mybatis-普通sq增删改查学习笔记
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}
学生
事务
查询
只需
美丽
文件
理论
类型
规律
语句
路径
路径名
古蔺
影响
更新
普通
笔记
学习
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
保险科技 互联网保险
互联网科技财经
鸠鸠互联网科技
互联网最新的科技有限公司
港股互联网科技股
唐信互联网科技 大连
互联网科技宣传片
中正互联网科技招聘
佩信高科技互联网
北京三清互联网科技
成都互联网科技公司100强
互联网金融科技公司干啥的
互联网大会黑科技排名
互联网5g黑科技有哪些
工业互联网等科技实现智能化赋能
南博望互联网科技有限公司
沐思想互联网科技有限公司
城市宝互联网科技有限公司
米泉互联网科技专业怎么样
互联网科技创新创业项目
咖啡网
互联网金融 科技金融
亿策互联网科技
九亚互联网科技
腾讯科技 互联网女皇
赫美联合互联网科技
互联网科技有哪些公司
移动互联网科技公司
宜昌元轩互联网科技有限公司
华夏恒生互联网科技013171