作者:手机用户2502892647 | 来源:互联网 | 2023-08-31 11:07
扩展:MyBatis系列学习汇总
文章目录 一、实体类实现增删改查 1.1、增 > insert 1.2、删 > delete 1.3、改 > update 1.4、查 > select 二、Map实现增删改查 一、实体类实现增删改查 前置条件跟上一篇的一样,这里不在重复,这里只讲实现方式! 1.1、增 > insert ①抽象接口
Integer addUserInfo ( User user) ;
②xml
这里要注意关键字的书写方式 like要用飘号
来包起来,防止跟关键字引起冲突! < insert id = " addUserInfo" parameterType = " com.dbright.pojo.User" > insert into user(name,age,`like`)values(#{name},#{age},#{like} ) insert>
③测试类
session.commit();
这个是提交事务!必须得写,不然语句不会生效! @Test public void addUserInfo ( ) { SqlSession session = MybatisUtils. getSession ( ) ; UserMapper mapper = session. getMapper ( UserMapper. class ) ; User user = new User ( ) ; user. setName ( "面条" ) ; user. setAge ( 22 ) ; user. setLike ( "王者荣耀" ) ; Integer statusNum = mapper. addUserInfo ( user) ; System. out. println ( statusNum) ; session. commit ( ) ; session. close ( ) ; }
④执行结果
1.2、删 > delete ①抽象接口
Integer deleteUserInfo ( int id) ;
②xml
< delete id = " deleteUserInfo" parameterType = " int" > delete from user where id = #{id} delete>
③测试类
@Test public void deleteUserInfo ( ) { SqlSession session = MybatisUtils. getSession ( ) ; UserMapper mapper = session. getMapper ( UserMapper. class ) ; Integer statusNum = mapper. deleteUserInfo ( 26 ) ; System. out. println ( statusNum) ; session. commit ( ) ; session. close ( ) ; }
④执行结果
1.3、改 > update ①抽象接口
Integer updateUserInfo ( User user) ;
②xml
< update id = " updateUserInfo" parameterType = " com.dbright.pojo.User" > update user set name = #{name},age = #{age},`like` = #{like} where id = #{id} update>
③测试类
@Test public void updateUserInfo ( ) { SqlSession session = MybatisUtils. getSession ( ) ; UserMapper mapper = session. getMapper ( UserMapper. class ) ; User user = new User ( ) ; user. setId ( 30 ) ; user. setName ( "丁大大" ) ; user. setAge ( 23 ) ; user. setLike ( "钓鱼" ) ; Integer statusNum = mapper. updateUserInfo ( user) ; System. out. println ( statusNum) ; session. commit ( ) ; session. close ( ) ; }
④执行结果
1.4、查 > select ①抽象接口
List< User> getUserInfo ( ) ; User getUserInfoById ( int id) ; User getUserInfoByIF ( @Param ( "name" ) String name , @Param ( "age" ) Integer age) ;
②xml
< select id = " getUserInfo" resultType = " com.dbright.pojo.User" > select * from user select> < select id = " getUserInfoById" resultType = " com.dbright.pojo.User" > select * from user where id = #{id} select> < select id = " getUserInfoByIF" resultType = " com.dbright.pojo.User" > select * from `user` where `name` = #{name} and age = #{age} select>
③测试类
@Test public void selectUser ( ) { SqlSession session = MybatisUtils. getSession ( ) ; UserMapper mapper = session. getMapper ( UserMapper. class ) ; List< User> users = mapper. getUserInfo ( ) ; for ( User map: users) { System. out. println ( map) ; } session. close ( ) ; } @Test public void getUserInfoById ( ) { SqlSession session = MybatisUtils. getSession ( ) ; UserMapper mapper = session. getMapper ( UserMapper. class ) ; User userInfo = mapper. getUserInfoById ( 30 ) ; System. out. println ( userInfo) ; session. close ( ) ; } @Test public void getUserInfoByIF ( ) { SqlSession session = MybatisUtils. getSession ( ) ; UserMapper mapper = session. getMapper ( UserMapper. class ) ; User userInfo = mapper. getUserInfoByIF ( "丁大大" , 23 ) ; System. out. println ( userInfo) ; session. close ( ) ; }
④执行结果
二、Map实现增删改查 这里不举太多例子了,因为mapper会自动将map中的值根据key对应起来
十分方便,在实际工作中,使用Map查询居多
前提是前后端联调使用的是PostMan
或者Yapi
等联调工具
如果使用swaager
联调,必须使用实体类来进行开发
例子: ①接口类
Integer addUserInfo ( Map< String, Object> map) ;
②xml
< insert id = " addUserInfo" parameterType = " map" > insert into user(name,age,`like`)values(#{name},#{age},#{like} ) insert>
③测试类
@Test public void addUserInfo ( ) { SqlSession session = MybatisUtils. getSession ( ) ; UserMapper mapper = session. getMapper ( UserMapper. class ) ; Map< String, Object> map = new HashMap < > ( ) ; map. put ( "name" , "面条" ) ; map. put ( "age" , "50" ) ; map. put ( "like" , "王者荣耀" ) ; Integer statusNum = mapper. addUserInfo ( map) ; System. out. println ( statusNum) ; session. commit ( ) ; session. close ( ) ; }
④执行结果
路漫漫其修远兮,吾必将上下求索!
如果你认为i博主写的不错!写作不易,请点赞、关注、评论加收藏!三连一下!给博主一个鼓励吧~
转载请注明出处哦~