MyBatisPlus有什么用-創(chuàng)新互聯(lián)

小編給大家分享一下MyBatis Plus有什么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

目前創(chuàng)新互聯(lián)公司已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、周寧網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

MyBatis Plus 是國(guó)內(nèi)人員開(kāi)發(fā)的 MyBatis 增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生。

MyBatis Plus 的核心功能有:支持通用的 CRUD與條件構(gòu)造器。

通用 CRUD:定義好 Mapper 接口后,只需要繼承 BaseMapper<T> 接口即可獲得通用的增刪改查功能,無(wú)需編寫(xiě)任何接口方法與配置文件

條件構(gòu)造器:通過(guò) EntityWrapper<T> (實(shí)體包裝類),可以用于拼接 SQL 語(yǔ)句,并且支持排序、分組查詢等復(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"/>

<!-- 插件注冊(cè) -->

<property name="plugins">

<list>

<!--     注冊(cè)分頁(yè)插件 -->

<bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor" />

</list>

</property>

</bean>

4.Dao層

public interface IUserDao extends BaseMapper<User> {

}

5.實(shí)體類

@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.常見(jiàn)注解

@TableField(exist = false):表示該屬性不為數(shù)據(jù)庫(kù)表字段,但又是必須使用的。

@TableField(exist = true):表示該屬性為數(shù)據(jù)庫(kù)表字段。

@TableName:數(shù)據(jù)庫(kù)表相關(guān)

@TableId:表主鍵標(biāo)識(shí)

@TableField:表字段標(biāo)識(shí)

7.測(cè)試方法

@Test

public void testMybatisPlus(){

System.out.println("selectById:"+userDao.selectById(4)); // 根據(jù)Id查詢

System.out.println("selectList:"+userDao.selectList(null)); // 查詢?nèi)?/p>

com.baomidou.mybatisplus.plugins.Page<User> page = new com.baomidou.mybatisplus.plugins.Page<>();

List<User> list = userDao.selectPage(page, null); // 分頁(yè)查詢

page.setRecords(list); // 把結(jié)果封裝到分頁(yè)對(duì)象中

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);

}

以上是“MyBatis Plus有什么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

名稱欄目:MyBatisPlus有什么用-創(chuàng)新互聯(lián)
地址分享:http://muchs.cn/article40/dddpho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、域名注冊(cè)企業(yè)網(wǎng)站制作、云服務(wù)器網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司