mybatis-普通sq增删改查学习笔记
发表于:2025-11-06 作者:千家信息网编辑
千家信息网最后更新 2025年11月06日,import java.util.*;import cn.mybatis.entity.Student;import cn.mybatis.util.MybatisUtil;import org.ap
千家信息网最后更新 2025年11月06日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安全错误
数据库的锁怎样保障安全
工信部认证的软件开发资质
国内云服务器加盟项目平台
华为下一代数据库
数据库视图在哪里打开
广西省台式电脑服务器自动生产线
网络安全绘画作品怎么画
描述数据库中全局
庭凤网络技术
数据库的表的字段默认值怎么修改
学校网络安全条例
网络安全系统图解
五华服务器建设
手机软件开发概念股票
帝国神话私人服务器伤害调节
掌上通三级网络技术
明朝万达网络安全产业链
软件开发流程管理论文
万方数据库截词检索
数据库accept
网络技术检测方法
电子商务网络技术的心得体会
教育软件开发技术
华为网络技术工程师出差
用服务器接业务端口
下列关于数据库的叙述
手机软件开发概念股票
帝国神话私人服务器伤害调节
服务器共享如何管理
数据库dbs概念
汇丰银行西安软件开发中心