Java中MyBatis Plus知識點總結(jié)
好程序員Java教程分享MyBatis Plus介紹:
1.MyBatis Plus 介紹
MyBatis Plus 是國內(nèi)人員開發(fā)的 MyBatis 增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生。
MyBatis Plus 的核心功能有:支持通用的 CRUD、代碼生成器與條件構(gòu)造器。
通用 CRUD:定義好 Mapper 接口后,只需要繼承 BaseMapper<T> 接口即可獲得通用的增刪改查功能,無需編寫任何接口方法與配置文件
條件構(gòu)造器:通過 EntityWrapper<T> (實體包裝類),可以用于拼接 SQL 語句,并且支持排序、分組查詢等復(fù)雜的 SQL
2.添加依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.3</version> </dependency>
3.配置
<!-- MP 提供的 MybatisSqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean"
class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 別名處理 -->
<property name="typeAliasesPackage" value="com.qf.entity"/>
<!-- 插件注冊 -->
<property name="plugins">
<list>
<!-- 注冊分頁插件 -->
<bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor" />
</list>
</property>
</bean>
4.Dao層
public interface IUserDao extends BaseMapper<User> {
}
5.實體類
@Data
@TableName(value="t_user")
public class User{
@TableId(value="id",type=IdType.AUTO)
private Integer id;
@TableField(value="username")
private String name;
private Integer age;
private String password;
@TableField(exist=false)
private Integer xxx;
}
6.常見注解
@TableField(exist = false):表示該屬性不為數(shù)據(jù)庫表字段,但又是必須使用的。
@TableField(exist = true):表示該屬性為數(shù)據(jù)庫表字段。
@TableName:數(shù)據(jù)庫表相關(guān)
@TableId:表主鍵標(biāo)識
@TableField:表字段標(biāo)識
7.測試方法
@Test
public void testMybatisPlus(){
System.out.println("selectById:"+userDao.selectById(4)); // 根據(jù)Id查詢
System.out.println("selectList:"+userDao.selectList(null)); // 查詢?nèi)?
com.baomidou.mybatisplus.plugins.Page<User> page = new com.baomidou.mybatisplus.plugins.Page<>();
List<User> list = userDao.selectPage(page, null); // 分頁查詢
page.setRecords(list); // 把結(jié)果封裝到分頁對象中
System.out.println(page.getCurrent());
System.out.println(page.getPages());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.getRecords());
EntityWrapper<User> entityWrapper = new EntityWrapper<>();
entityWrapper.eq("id", 4);
entityWrapper.or().like("username", "3");
List<User> selectList = userDao.selectList(entityWrapper); // 條件查詢
System.out.println("wrapper:"+selectList);
}
以上就是本次介紹的全部相關(guān)知識點,感謝大家的學(xué)習(xí)和對腳本之家的支持。
相關(guān)文章
SpringBoot如何動態(tài)改變?nèi)罩炯墑e
這篇文章主要介紹了SpringBoot如何動態(tài)改變?nèi)罩炯墑e,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12
SpringBoot使用FFmpeg實現(xiàn)M3U8切片轉(zhuǎn)碼播放
FFmpeg是一個開源跨平臺的多媒體處理工具套件,它支持音頻、視頻文件的編碼、解碼、流媒體傳輸以及轉(zhuǎn)換等多種操作,本文小編給大家介紹了SpringBoot使用FFmpeg實現(xiàn)M3U8切片轉(zhuǎn)碼播放的操作,需要的朋友可以參考下2024-08-08
Executor攔截器高級教程QueryInterceptor的規(guī)范
今天小編就為大家分享一篇關(guān)于Executor攔截器高級教程QueryInterceptor的規(guī)范,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
SpringBoot?spring.factories加載時機分析
這篇文章主要為大家介紹了SpringBoot?spring.factories加載時機分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
利用Java將2019拆分成三個素數(shù)平方和的方法實例
這篇文章主要給大家介紹了關(guān)于利用Java將2019拆分成三個素數(shù)平方和的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

