如何簡單使用mybatis注解
使用注解開發(fā)
本質(zhì):反射機(jī)制實(shí)現(xiàn)
底層:動(dòng)態(tài)代理
1、注解在接口上的實(shí)現(xiàn)
public interface UserMapper { @Select("select * from user") List<User> getUsers(); }
2、使用注解時(shí),需要在核心配置文件中綁定接口
<mappers> <mapper class="com.xiao.dao.UserMapper"/> </mappers>
3、測試使用
public class UserMapperTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); //底層主要靠反射實(shí)現(xiàn) UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.getUsers(); for (User user : users) { System.out.println(user); } sqlSession.close(); } }
使用注解完成CRUD
只需修改接口和測試類即可。
之前在使用XML的方式進(jìn)行CRUD操作時(shí),增(insert)、改(update)、刪(delete)時(shí),都需要手動(dòng)進(jìn)行事務(wù)提交操作sqlsession.commit()
。
查看openSession()的源碼
自動(dòng)提交
查(select)
接口
@Select("select * from user where id = #{id}") User getUserByID(@Param("id") int id);
測試類
public class UserMapperTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); //底層主要靠反射實(shí)現(xiàn) UserMapper mapper = sqlSession.getMapper(UserMapper.class); User userByID = mapper.getUserByID(1); System.out.println(userByID); sqlSession.close(); } }
增(insert)
接口
@Insert("insert into user(id,name,pwd) value (#{id},#{name},#{password})") int addUser(User user);
實(shí)現(xiàn)類
public class UserMapperTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); //底層主要靠反射實(shí)現(xiàn) UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.addUser(new User(5,"張飛","1456156")); sqlSession.close(); } }
改(update)
接口
@Update("update user set name = #{name},pwd=#{password} where id = #{id}") int updateUser(User user);
測試類
public class UserMapperTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); //底層主要靠反射實(shí)現(xiàn) UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(5,"趙云","565423")); sqlSession.close(); } }
刪(delete)
接口
@Delete("delete from user where id = #{id}") int deleteUser(int id);
測試類
public class UserMapperTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); //底層主要靠反射實(shí)現(xiàn) UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(5); sqlSession.close(); } }
到此這篇關(guān)于如何簡單使用mybatis注解的文章就介紹到這了,更多相關(guān)mybatis注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實(shí)現(xiàn)代碼
- MybatisPlus 不修改全局策略和字段注解如何將字段更新為null
- MyBatis使用注解開發(fā)和無主配置文件開發(fā)的情況
- Mybatis常見注解有哪些(總結(jié))
- Mybatis注解增刪改查的實(shí)例代碼
- 解決MyBatis @param注解參數(shù)類型錯(cuò)誤異常的問題
- 詳解mybatis @SelectProvider 注解
- mybatis3使用@Select等注解實(shí)現(xiàn)增刪改查操作
- MyBatis注解方式之@Update/@Delete使用詳解
- mybatis省略@Param注解操作
相關(guān)文章
淺試仿?mapstruct實(shí)現(xiàn)微服務(wù)編排框架詳解
這篇文章主要為大家介紹了淺試仿?mapstruct實(shí)現(xiàn)微服務(wù)編排框架詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08SpringMVC通過注解獲得參數(shù)的實(shí)例
下面小編就為大家?guī)硪黄猄pringMVC通過注解獲得參數(shù)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08關(guān)于eclipse中運(yùn)行tomcat提示端口被占用的4種解決
這篇文章主要介紹了關(guān)于eclipse中運(yùn)行tomcat提示端口被占用的4種解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01AsyncHttpClient ListenableFuture源碼流程解讀
這篇文章主要為大家介紹了AsyncHttpClient ListenableFuture源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Hibernate懶加載之<class>標(biāo)簽上的lazy
這篇文章主要介紹了Hibernate懶加載之<class>標(biāo)簽上的lazy,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02springboot對接minio的webhook完整步驟記錄
Minio是一款開源的對象存儲服務(wù),它致力于為開發(fā)者提供簡單、高性能、高可用的云存儲解決方案,下面這篇文章主要給大家介紹了關(guān)于springboot對接minio的webhook的相關(guān)資料,需要的朋友可以參考下2024-07-07