如何簡(jiǎn)單使用mybatis注解
使用注解開(kāi)發(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、測(cè)試使用
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
只需修改接口和測(cè)試類即可。
之前在使用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);
測(cè)試類
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);
測(cè)試類
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);
測(cè)試類
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)于如何簡(jiǎn)單使用mybatis注解的文章就介紹到這了,更多相關(guān)mybatis注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實(shí)現(xiàn)代碼
- MybatisPlus 不修改全局策略和字段注解如何將字段更新為null
- MyBatis使用注解開(kāi)發(fā)和無(wú)主配置文件開(kāi)發(fā)的情況
- Mybatis常見(jiàn)注解有哪些(總結(jié))
- Mybatis注解增刪改查的實(shí)例代碼
- 解決MyBatis @param注解參數(shù)類型錯(cuò)誤異常的問(wèn)題
- 詳解mybatis @SelectProvider 注解
- mybatis3使用@Select等注解實(shí)現(xiàn)增刪改查操作
- MyBatis注解方式之@Update/@Delete使用詳解
- mybatis省略@Param注解操作
相關(guān)文章
關(guān)于swagger配置及踩坑@Api參數(shù)postion無(wú)效解決接口排序問(wèn)題
這篇文章主要介紹了關(guān)于swagger配置及踩坑@Api參數(shù)postion無(wú)效解決接口排序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
RestTemplate發(fā)送HTTP?POST請(qǐng)求使用方法詳解
這篇文章主要為大家介紹了RestTemplate發(fā)送HTTP?POST請(qǐng)求的使用方法詳解,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Lombok中@EqualsAndHashCode注解的使用及說(shuō)明
這篇文章主要介紹了Lombok中@EqualsAndHashCode注解的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot3集成ElasticSearch的方法詳解
Elasticsearch是一個(gè)分布式、RESTful風(fēng)格的搜索和數(shù)據(jù)分析引擎,適用于各種數(shù)據(jù)類型,數(shù)字、文本、地理位置、結(jié)構(gòu)化數(shù)據(jù)、非結(jié)構(gòu)化數(shù)據(jù),本文給大家詳解介紹了SpringBoot3集成ElasticSearch的方法,需要的朋友可以參考下2023-08-08
SpringBoot集成ShardingSphere實(shí)現(xiàn)數(shù)據(jù)庫(kù)分表
ShardingSphere?是一個(gè)開(kāi)源的分布式數(shù)據(jù)庫(kù)中間件,旨在為應(yīng)用提供數(shù)據(jù)庫(kù)分片、讀寫(xiě)分離、分布式事務(wù)等功能,下面我們來(lái)看看SpringBoot如何集成ShardingSphere實(shí)現(xiàn)數(shù)據(jù)庫(kù)分表吧2024-12-12

