建设社区网站有什么借鉴之处,app拉新佣金排行榜,南昌整站优化,网址提交2023.10.29 本章学习MyBatis的基本crud操作。
insert
java程序如下#xff1a;
①使用map集合传参
Testpublic void testInsertCar(){SqlSession sqlSession SqlSessionUtil.openSession();//先将数据放到Map集合中#xff0c;在sql语句中使用 #{map集合的key} 来完成传…2023.10.29 本章学习MyBatis的基本crud操作。
insert
java程序如下
①使用map集合传参
Testpublic void testInsertCar(){SqlSession sqlSession SqlSessionUtil.openSession();//先将数据放到Map集合中在sql语句中使用 #{map集合的key} 来完成传值#{} 等同于JDBC中的 ? #{}就是占位符MapString,Object map new HashMap();map.put(carNum, 1029);map.put(brand, 马自达);map.put(guidePrice, 50.3);map.put(produceTime, 2023-10-29);map.put(carType, 燃油车);// 执行SQL语句使用map集合给sql语句传递数据int count sqlSession.insert(insertCar, map);System.out.println(插入了几条记录 count);sqlSession.commit();sqlSession.close();}
②使用pojo传参
Testpublic void testInsertCarByPOJO(){SqlSession sqlSession SqlSessionUtil.openSession();Car car new Car(null,1029,马自达,50.3,2023-10-29,燃油车);int count sqlSession.insert(insertCar,car);System.out.println(count);sqlSession.commit();sqlSession.close();}
SQL语句如下
insert idinsertCarinsert into t_car(id,car_num,brand,guide_price,produce_time,car_type) values (null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType});
/insert 运行结果 ps如果采用map集合传参#{} 里写的是map集合的key如果key不存在不会报错数据库表中会插入NULL。
ps如果采用POJO传参#{} 里写的是POJO类中get方法的方法名去掉get之后将剩下的单词首字母变小写例如getAge对应的是#{age}getUserName对应的是#{userName}如果这样的get方法不存在会报错。
delete
需求根据car_num进行删除。
java程序如下
Testpublic void testDeleteById(){SqlSession sqlSession SqlSessionUtil.openSession();int count sqlSession.delete(deleteById,2);sqlSession.commit();sqlSession.close();}
sql语句如下 delete iddeleteByIddelete from t_car where id #{id}/delete
运行结果 ps当占位符只有一个的时候${} 里面的内容可以随便写。
update
java代码如下 Testpublic void testUpdateById(){SqlSession sqlSession SqlSessionUtil.openSession();Car car new Car(21L,1029,宝马7系,66.6,1999-11-23,燃油车);int count sqlSession.update(updateById,car);System.out.println(count);sqlSession.commit();sqlSession.close();}
sql语句如下 update idupdateByIdupdate t_car setcar_num #{carNum}, brand #{brand},guide_price #{guidePrice}, produce_time #{produceTime},car_type #{carType}where id #{id}/update
运行结果 select
查询一条数据
需求查询id为21的Car信息
java代码如下 Testpublic void testSelectCarById(){// 获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession();// 执行SQL语句Object car sqlSession.selectOne(selectCarById, 21);System.out.println(car);}
sql语句如下 select idselectCarById resultTypepojo.Carselectid, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carTypefromt_carwhereid #{id}/select
运行结果 ps当查询结果的字段名和java类的属性名对应不上的话可以采用as关键字起别名 。
查询多条数据
java代码如下 Testpublic void testSelectCarAll(){// 获取SqlSession对象SqlSession sqlSession SqlSessionUtil.openSession();// 执行SQL语句ListObject cars sqlSession.selectList(selectCarAll);// 输出结果cars.forEach(car - System.out.println(car));}
sql语句如下 !--虽然结果是List集合但是resultType属性需要指定的是List集合中元素的类型。--select idselectCarAll resultTypepojo.Car!--记得使用as起别名让查询结果的字段名和java类的属性名对应上。--selectid, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carTypefromt_car/select
由于我数据库只有一条记录先手动添加几条记录 运行查询语句结果为 ps在SQL Mapper配置文件中mapper标签的namespace属性可以翻译为命名空间这个命名空间主要是为了防止sqlId冲突的。