使用MyBatis完成CRUD
配合视频教程观看,更易学习理解,课程讲解从Mybatis的一些核心要点与实战中的运用,一直过渡到MyBaits源码,由表及里的代入架构思维。一步一案例,一码一实操。从简单到深入,从实战到源码,MyBatis重点、难点、考点一网打尽。
相关的学习资料给大家备好了
delete(Delete)
需求:根据car_num进行删除。
SQL语句这样写:
<delete id="deleteByCarNum"> delete from t_car where car_num = #{SuiBianXie} </delete>
Java程序这样写:
@Test public void testDeleteByCarNum(){ // 获取SqlSession对象 SqlSession sqlSession = SqlSessionUtil.openSession(); // 执行SQL语句 int count = sqlSession.delete("deleteByCarNum", "102"); System.out.println("删除了几条记录:" + count); }
运行结果:
注意:当占位符只有一个的时候,${} 里面的内容可以随便写。
3.3 update(Update)
需求:修改id=34的Car信息,car_num为102,brand为比亚迪汉,guide_price为30.23,produce_time为2018-09-10,car_type为电车
修改前:
SQL语句如下:
<update id="updateCarByPOJO"> update t_car set car_num = #{carNum}, brand = #{brand}, guide_price = #{guidePrice}, produce_time = #{produceTime}, car_type = #{carType} where id &#