Mybatis中怎么实现注解增删改查操作
导读:本文共1461.5字符,通常情况下阅读需要5分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 需要在config.xml中注册Java接口<mappers><mapperclass="com.mybatis.DAO.PeopleMapper"/></mappers>要用class=查publicinterfacePeopleMapper{@Select("select*frompeo... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。需要在config.xml中注册Java接口
<mappers><mapperclass="com.mybatis.DAO.PeopleMapper"/></mappers>
要用class=
查
publicinterfacePeopleMapper{@Select("select*frompeople")List<People>getPeopleList();}
增 可以先开启事务自动提交
publicstaticSqlSessiongetSqlSession(){returnsqlSessionFactory.openSession(true);}}Mapper.java```javapublicinterfacePeopleMapper{@Select("select*frompeople")List<People>getPeopleList();@Insert("insertintopeople(id,name,age,address)VALUES(#{id},#{name},#{age},#{address})")intaddPeople(Peoplepeople);}
test
publicclassPeopleDAOtest{[@Test](https://my.oschina.net/azibug)publicvoidprint(){SqlSessionsqlSession=MybatisUtils.getSqlSession();PeopleMapperpeopleMapper=sqlSession.getMapper(PeopleMapper.class);List<People>people=peopleMapper.getPeopleList();for(Peoplep:people){System.out.println(p);}sqlSession.close();}[@Test](https://my.oschina.net/azibug)publicvoidadd(){SqlSessionsqlSession=MybatisUtils.getSqlSession();PeopleMapperpeopleMapper=sqlSession.getMapper(PeopleMapper.class);peopleMapper.addPeople(newPeople(6,"圣迭戈",456,"啥地方"));print();}}
因为已经自动提交了,所以不需要sqlSession.commit();
删
注解@Param 只能用于基本数据类型 传入的参数只能和sql语句中参数一样 多个参数 样例
publicinterfacePeopleMapper{@Delete("deletepeoplefrompeoplewhereid=#{uid}")intdelPeople(@Param("uid")inti);}
test
publicclassPeopleDAOtest{@Testpublicvoiddel(){SqlSessionsqlSession=MybatisUtils.getSqlSession();PeopleMapperpeopleMapper=sqlSession.getMapper(PeopleMapper.class);peopleMapper.delPeople(6);print();}}
改
Mapper.java
publicinterfacePeopleMapper{@Update("updatemybatis.peoplesetname=#{name},age=#{age},address=#{address}whereid=#{id}")intupdateP(Peoplepeople);}
test
publicclassPeopleDAOtest{@Testpublicvoidupdate(){SqlSessionsqlSession=MybatisUtils.getSqlSession();PeopleMapperpeopleMapper=sqlSession.getMapper(PeopleMapper.class);peopleMapper.updateP(newPeople(5,"圣迭戈",456,"啥地方"));print();}}
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
Mybatis中怎么实现注解增删改查操作的详细内容,希望对您有所帮助,信息来源于网络。