溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MyBatis Plus工具快速入門使用教程

發(fā)布時間:2020-09-22 16:25:12 來源:腳本之家 閱讀:140 作者:帶妳訫飛 欄目:編程語言

MyBatis-plus有什么特色

   1.代碼生成 2.條件構(gòu)造器

    對我而言,主要的目的是使用它強(qiáng)大的條件構(gòu)建器.  

快速使用步驟:

  1.添加pom文件依賴

<dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity</artifactId>
  <version>1.7</version>
</dependency>
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>2.0.1</version> 
</dependency>

  注意:mybatis-plus會自動維護(hù)mybatis以及mybatis-spring的依賴,所以不需要引入后兩者,避免發(fā)生版本沖突.

  2.修改配置文件

  將mybatis的sqlSessionFactory替換成mybatis-plus的即可,mybatis-plus只做了一些功能的擴(kuò)展: 

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <!-- 自動掃描Mapping.xml文件 -->
  <property name="mapperLocations" value="classpath:mybatis/*/*.xml"/>
  <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
  <property name="typeAliasesPackage" value="com.baomidou.springmvc.model.*"/>
  <property name="plugins">
   <array>
    <!-- 分頁插件配置 -->
    <bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
     <property name="dialectType" value="mysql"/>
    </bean>
   </array>
  </property>
  <!-- 全局配置注入 -->
  <property name="globalConfig" ref="globalConfig" /> 
</bean>

  在上面的配置中,除了mybatis的常規(guī)配置,多了一個分頁插件的配置和全局配置,mybatis-plus提供了很方便的使用分頁的插件,還有一個全局配置如下:  

<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
  <!-- 
   AUTO->`0`("數(shù)據(jù)庫ID自增")
    INPUT->`1`(用戶輸入ID")
   ID_WORKER->`2`("全局唯一ID")
   UUID->`3`("全局唯一ID")
  -->
  <property name="idType" value="2" />
  <!--
   MYSQL->`mysql`
   ORACLE->`oracle`
   DB2->`db2`
   H2->`h3`
   HSQL->`hsql`
   SQLITE->`sqlite`
   POSTGRE->`postgresql`
   SQLSERVER2005->`sqlserver2005`
   SQLSERVER->`sqlserver`
  -->
  <!-- Oracle需要添加該項 -->
  <!-- <property name="dbType" value="oracle" /> -->
  <!-- 全局表為下劃線命名設(shè)置 true -->
  <property name="dbColumnUnderline" value="true" />
 </bean>

  至此,配置工作就算大功告成了,接下來通過一個簡單的例子來感受一下它的使用.

  1.新建一個User表:

@TableName("user")
public class User implements Serializable {
 /** 用戶ID */
 private Long id;
 /** 用戶名 */
 private String name;
 /** 用戶年齡 */
 private Integer age;
 @TableField(exist = false)
 private String state;
}

  這里有兩個注解需要注意,第一是@tableName("user"),它是指定與數(shù)據(jù)庫表的關(guān)聯(lián),這里的注解意味著你的數(shù)據(jù)庫里應(yīng)該有一個名為user的表與之對應(yīng),并且數(shù)據(jù)表的列名應(yīng)該就是User類的屬性,對于User類中有而user表中沒有的屬性需要加第二個注解@TableField(exist = false),表示排除User類中的屬性.

     2.新建Dao層接口UserMapper:

/**
 * User 表數(shù)據(jù)庫控制層接口
 */
public interface UserMapper extends BaseMapper<User> {
 @Select("selectUserList")
 List<User> selectUserList(Pagination page,String state);
}

  dao接口需要實現(xiàn)Basemapper,這樣就能夠使用封裝好的很多通用方法,另外也可以自己編寫方法,@select注解引用自第三步的UserMapper文件  

  3.新建UserMapper配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.baomidou.springmvc.mapper.system.UserMapper">
 <!-- 通用查詢結(jié)果列-->
 <sql id="Base_Column_List">
  id, name, age
 </sql>
 <select id="selectUserList" resultType="User">
  SELECT * FROM sys_user WHERE state=#{state}
 </select>
</mapper>

  4.新建service層類UserService:

 /**
 *
 * User 表數(shù)據(jù)服務(wù)層接口實現(xiàn)類
 *
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User>{
 public Page<User> selectUserPage(Page<User> page, String state) {
  page.setRecords(baseMapper.selectUserList(page,state));
  return page;
 }
}

  UserService繼承了ServiceImpl類,mybatis-plus通過這種方式為我們注入了UserMapper,這樣可以使用service層默認(rèn)為我們提供的很多方法,也可以調(diào)用我們自己在dao層編寫的操作數(shù)據(jù)庫的方法.Page類是mybatis-plus提供分頁功能的一個model,繼承了Pagination,這樣我們也不需要自己再編寫一個Page類,直接使用即可.

  5,新建controller層UserController

@Controller
public class UserController extends BaseController {
 @Autowired
 private IUserService userService;
 @ResponseBody
 @RequestMapping("/page")
 public Object selectPage(Model model){
  Page page=new Page(1,10);
  page = userService.selectUserPage(page, "NORMAL");
  return page;
 }

   以上就完成了一個基本的功能,下面來看一下它的條件構(gòu)建器.

mybatis-plus的條件構(gòu)建器

  首先看一個條件構(gòu)建器實例的簡單實用.

public void test(){
  EntityWrapper ew=new EntityWrapper();
  ew.setEntity(new User());
  String name="wang";
  Integer age=16;
  ew.where("name = {0}",name).andNew("age > {0}",age).orderBy("age");
  List<User> list = userService.selectList(ew);
  Page page2 = userService.selectPage(page, ew);
 }

  這里使用了一個條件包裝類EntityWrapper,來進(jìn)行對sql語句的拼裝,原理也很好理解,上面的代碼中,第一個list查詢的結(jié)果就是查詢數(shù)據(jù)庫中name=wang并且age>16歲的所有記錄并按照age排序.而第二個查詢就是再多加一個分頁的功能.

  基本上來說,使用EntityWrapper可以簡單地完成一些條件查詢,但如果查詢方法使用頻率很高的話還是建議自己寫在UserMapper里.

  那么自定義的mapper方法能不能使用EntityWrapper呢,當(dāng)然也是可以的.

  文檔中給了一個這樣的例子.

  1.在Mappper中定義:

  List<User> selectMyPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);

   2.在mapper文件中定義:

<select id="selectMyPage" resultType="User">
   SELECT * FROM user ${ew.sqlSegment}
</select>

   對于EntityMapper的條件拼接,基本可以實現(xiàn)sql中常用的where,and,or,groupby,orderby等語法,具體構(gòu)建方法可以靈活組合.

@Test
public void testTSQL11() {
 /*
  * 實體帶查詢使用方法 輸出看結(jié)果
  */
 ew.setEntity(new User(1));
 ew.where("name={0}", "'zhangsan'").and("id=1")
   .orNew("status={0}", "0").or("status=1")
   .notLike("nlike", "notvalue")
   .andNew("new=xx").like("hhh", "ddd")
   .andNew("pwd=11").isNotNull("n1,n2").isNull("n3")
   .groupBy("x1").groupBy("x2,x3")
   .having("x1=11").having("x3=433")
   .orderBy("dd").orderBy("d1,d2");
 System.out.println(ew.getSqlSegment());
}

 參考文檔

    mybaits-plus官方文檔

總結(jié)

以上所述是小編給大家介紹的MyBatis Plus工具快速入門使用教程,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI