<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      mybatis plus基礎

      mybatis plus

      1引入依賴

      <!--MybatisPlus-->
      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>		
          <version>3.5.3.1</version>
      </dependency>
      

      2創建mapper,繼承自BaseMapper<>中放實體泛型

      public interface UserMapper extends BaseMapper<User> {
          
      }
      

      3直接調用UserMapper的增刪改查方法即可。單表的基本增刪改查可以實現。

      類名駝峰轉下劃線為表明,字段名駝峰轉下劃線為字段名,

      常用注解:

      MybatisPlus中比較常用的幾個注解如下
      @TableName:用來指定表名

      @Tableld:用來指定表中的主鍵字段信息

      @TableField:用來指定表中的普通字段信息

      @TableName("tb_user")
      public class User {
          @TableId(value= "id",type= IdType.AUTO )
          private Long id;
          @TableField("username")
          private String  name;
          @TableField("is_married")  //is開頭的
          private Boolean isMarried;
          @TableField("'order'")
          private Integer order;      //與數據庫關鍵字報錯
          @TableField(exist = false)  //表中不存在的
          private String address;
      

      ldType枚舉 :
      AUTO : 數據庫自增長
      INPUT : 通過set方法自行輸入
      ASSIGN ID : 分配 ID,接口ldentifierGenerator的方法nextld來生成id默認實現類為DefaultldentifierGenerator雪花算法

      mybatis-plus:
      	type-aliases-package: com.mp.domain.po # 別名掃描包
      	mapper-locations: "classpath*:/mapper/**/*.xml" # Mapper.xml文件地址,默認值
      	configuration:
      		map-underscore-to-camel-case: true # 是否開啟下劃線和駝峰的映射
      	    cache-enabled: false # 是否開啟二級緩存
      	global-config:
      	    db-config:
      		     id-type: assign_id # id為雪花算法生成
      		     update-strategy: not_null # 更新笑略: 只更新非空字段
      

      條件構造器的用法
      QueryWrapper和LambdaQueryWrapper通常用來構建select、
      delete、update的where條件部分
      UpdateWrapper和LambdaUpdateWrapper通常只有在set語句比較特殊才使用
      盡量使用LambdaQueryWrapper和LambdaUpdateWrapper
      避免硬編碼

      自定義sql

      MyBatisPlus的Wrapper來構建復雜的Where條件,然后自己定義SQL語句中剩下的部分。

      1,基于Wrapper構建where條件

      List<Long> ids = List.of(1L,2L,4L);
      int amount = 200;
      // 1.構建條件
      LambdaQuerywrapper<User> wrapper = new LambdaQueryWrapper<User>().in(User::getId,ids);
      //2自定義SQL方法調用
      userMapper.updateBalanceByIds(wrapper,amount);
      

      2,在mapper方法參數中用Param注解聲明wrapper變量名稱,必須是ew

      void updateBalanceByIds(@Param("ew") LambdaQuerywrapper<User> wrapper, @Param("amount") int amount);
      

      3,自定義sql,并使用Wrapper條件

      <update id="updateBalanceByIds">
      UPDATE tb_user SET balance = balance - #{amount} ${ew.customSqlSegment}
      /update>
      

      IService接口

      插入:

      save(T): boolean

      saveBatch(Collection): boolean

      saveBatch(Collection, int): boolean

      saveOrUpdateBatch(Collection): boolean

      saveOrUpdateBatch(Collection, int): boolean

      saveOrUpdate(T): boolean

      saveOrUpdate(T, Wrapper): boolean

      修改:

      saveOrUpdateBatch(Collection): boolean

      saveOrUpdateBatch(Collection, int): boolean

      updateByld(T): boolean

      update(Wrapper): boolean

      update(T, Wrapper): boolean

      updateBatchByld(Collection): boolean

      updateBatchByld(Collection, int): boolean

      saveOrUpdate(T): boolean

      刪除:

      removeByld(Serializable): boolean

      removeByld(Serializable, boolean): boolean

      removeByld(T): boolean

      removeByMap(Map<String, Object>): boolean

      remove(Wrapper<T>): booleanremoveBylds(Collection<?>): boolean

      removeBylds(Collection<?>, boolean): boolean

      removeBatchBylds(Collection<?>): boolean

      removeBatchBylds(Collection<?>, boolean): boolean

      removeBatchBylds(Collection<?>, int): boolean

      removeBatchBylds(Collection<?>, int, boolean): boolean

      列表查詢:

      listBylds(Collection<? extends Serializable>): List<T>

      listByMap(Map<String, Object>): List<T>

      list(Wrapper<T>): List<T>

      list(): List<T>

      查詢單個:

      getByld(Serializable): T

      getOne(Wrapper<T>):T

      getOne(Wrapper<T>, boolean):T

      查詢數量:

      count(): long

      count(Wrapper<T>): long

      分頁查詢:

      page(E, Wrapper<T>): E

      page(E): E

      調價構造器:

      lambdaQuery(): LambdaQueryChainWrapper<T>

      lambdaQuery(T): LambdaQueryChainWrapper<T>

      lambdaUpdate(): LambdaUpdateChainWrapper<T>

      復制Dto到info

      BeanUtil.copyProperties(Souces,TargetClass): T

      User user=BeanUtil.copyProperties(userDto,User.class);
      

      BeanUtil.copyToList(SourceList,TargetClass): List<T>

      List<User> list=BeanUtil.copyToList(userDtoList,User.class);
      

      1.Mapper繼承BaseMapper

      public interface UserMapper extends BaseMapper<User> {
          
      }
      

      2.接口繼承IService

      public interface IUserService extends IService<User>{
      
      }
      

      3.實現類繼承ServiceImpl

      public UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService{}
      

      想要查詢單個字段

      使用select()

      lambdaQuery().select(User::getUserName).eq(User::getUserId,1)

      想要修改單個字段:set(User::getUserName,value)方法

      IService批量新增

      需求:批量插入10萬條用戶數據,并作出對比:
      1 普通for循環插入,普通for循環逐條插入速度極差,不推薦
      2 IService的批量插入,MP的批量新增,基于預編譯的批處理,性能不錯配置jdbc參數
      3 開啟rewriteBatchedStatements=true參數,開rewriteBatchedStatements,性能最好

      第三種在yml中連接數據庫的位置在最后拼上這個配置

      MP擴展功能

      ?

      ?

      代碼生成


      1. 下載插件mybatis plus
      2. 在IDEA上面菜單欄選擇other-》config database
      3. 輸入相關信息,點擊ok
      4. 選擇other -》Code generator

      tablePrefix是生成時去掉的表名前綴:如 t_message_

      ?

      ?

      ?

      靜態工具


      Db靜態工具類的方法和Iservice提供的方法幾乎一樣,只不過使用時需要傳入字節碼文件(.class),一般為了避免循環依賴時可以使用其進行操作。

      Db.lambdaQuery(User.class).eq(User::getId,id).one;
      

      ?

      ?

      邏輯刪除


      邏輯刪除就是基于代碼邏輯模擬刪除效果,但并不會真正刪除數據。

      思路如下:
      在表中添加一個字段標記數據是否被刪除
      當刪除數據時把標記置為1
      查詢時只查詢標記為0的數據

      ?

      ?

      MvbatisPlus提供了邏輯刪除功能,無需改變方法調用的方式,而是在底層幫我們自動修改CRUD的語句。我們要做的就是在application.yml文件中配置邏輯刪除的字段名稱和值即可:

      mybatis-plus:
      	global-config:
      		db-config:
      		Logic-delete-field: del_flag # 全局邏輯刪除的實體字名,字段類型可以是boolean、integer
      		Logic-delete-value: 1 # 邏輯已刪值(默認為 1)
      		Logic-not-delete-value: 0 # 邏輯未刪除值(默認為 0)
      

      ?

      ?

      ?

      枚舉處理器


      如何實現PO類中的枚舉類型變量與數據庫字段的轉換?
      給枚舉中的與數據庫對應value值添加@EnumValue注解,即可實現,@JsonValue注解用戶實現給前端傳遞字段的指定

      @EnumValue
      private final int value;
      @JsonValue
      private final String desc;

      ?

      ?

      分頁

      簡單用法:

      1 定義配置類,聲明插件

      @Configuration
      public class MyBatisConfig {
      @Bean
      public MybatisPlusInterceptor mybatisplusInterceptor(){
          MybatisplusInterceptor interceptor = new MybatisplusInterceptor();
          // 1.創建分頁插件
          PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSOL);
          paginationInnerInterceptor.setMaxLimit(1000L);
          // 2.添加分頁插件
          interceptor.addInnerInterceptor(paginationInnerInterceptor);
          return interceptor;
        }
      }
      

      2使用

      @Test
      void testPageQuery() {
      int pageNo = 1, pageSize = 2;
      // 1.準備分頁條件
      // 1.1.分頁條件
      Page<User> page = Page.of(pageNo,pageSize);
      // 1.2.排序條件,字段,true升序,false降序
      page.addorder(new OrderItem(  "balance"r , true)) ;
      page.addOrder(new OrderItem(  "id",  true));
      // 2.分頁查詢,
      Page<User> p = userService.page(page);
      // 3.解析
       long total = p.getTotal();
       System.out.println("total=" + total);
       long pages = p.getPages();
       System.out.println("pages="+ pages);
       List<User> users = p.getRecords();
       users.forEach(System.out::println);
      
      }
      

      舉例創建:

      分頁查詢實體:

      @Data
      @ApiModel(description = "分頁查詢實體")
      public class PageQuery {
          @ApiModelProperty("頁碼")
          private Integer pageNo ;
          
          @ApiModelProperty("每頁大小")
          private Integer pageSize;
          
          @ApiModelProperty("排序字段")
          private String sortBy;
          
          @ApiModelProperty("是否升序")
          private Boolean isAsc;
          
          
          //user轉userVo
      /* 調用  
          return PageDTO.of(p,user ->
          // 1.拷貝基礎屬性
          UserVO vo = BeanUtil.copyProperties(user,UserV0.class);
          // 2.處理特殊邏輯
          return vo;
          );
      */
          
          
          public static <PO,V0> PageDTO<V0>  of(Page<P0> p, Function<PO,V0> convertor){
              PageDTO<V0> dto = new PageDTO<>();
      		// 1.總條數
      		dto,setTotal(p.getTotal()) ;
      		// 2.總頁數
      		dto.setPages(p.getPages()) ;
      		// 3,當前頁數據
      		List<PO> records = p.getRecords();
              // 4.拷貝user的VO
              dto,setList(records,stream() .map(convertor).collect(Collectors,toList()));
              // 5.返回
      		return dto;  
          }
          
          public static PageDTO<UserVO>of(Page<User> p){
              PageDTO<UserV0> dto = new PageDTO<>();
              // 1.總條數
      		dto.setTotal(p.getTotal());
              // 2.總頁數
              dto,setPages(p.getPages());
              // 3.當前頁數據
      		List<User> records = p.getRecords();
              if (CollUtil.isEmpty(records)) {
                  dto,setList(Collections.emptyList());
                  return dto;
              }
      		// 4.拷貝user的vO
              dto.setList(BeanUtil.copyToList(records, Uservo.class));
              // 5.返回
              return dto;
          }
      }
      

      用于傳參的Dto:

      @EqualsAndHashCode(callSuper = true)
      @Data
      @ApiModel(description ="用戶查詢條件實體")
      public class UserQuery extends PageQuery {
          @ApiModelProperty("用戶名關鍵字")
          private String name;
          @ApiModelProperty("用戶狀態: 1-正常,2-凍結")
          private Integer status;
          @ApiModelProperty("余額最小值")
          private Integer minBalance;
      	@ApiModelProperty("余額最大值")
      	private Integer maxBalance;
      }
      

      返回結果定義:

      @Data
      @ApiModel(description = "分頁結果")
      public class PageVO<T> {
          @ApiModelProperty("總條數")
          private Integer total;
          @ApiModelProperty("總頁數")
          private Integer pages ;
          @ApiModelProperty("集合")
          private List<T> list;
      }
      
      public PageDTO<UserVo> queryUsersPage(UserQuery query){
          String name = query.getName();
      	Integer status = query.getStatus() ;
      	// 1.構建分頁條件
      	// 1.1.分頁條件
      	Page<User> page = Page.of(query.getPageNo(),query.getPageSize());
      	// 1.2.排序條件
      	page.addOrder(new OrderItem(query.getSortBy(),query.getIsAsc()));
      
          // 2.分頁查詢
      	Page<User> p = lambdaQuery()
      	.like( condition: name != null, User::getUsername, name).
              eq( condition: status != null, User::getStatus, status).page(page);
           // 3.封裝VO結果
          PageVo<UserV0> dto = new PageVo<>();
          // 3.1.總條數
          dto.setTotal(p.getTotal()) ;
          // 3.2.總頁數
      	dto.setPages(p.getPages());
          // 3.3.當前頁數據
      	List<User> records = p.getRecords();
      	if (CollUtil.isEmpty(records)) {
              dto.setList(Collections.emptyList());
           	return dto ;
      	// 3.4.拷貝user的VO
          List<UserVo> vos = BeanUtil.copyToList(records, UserV0.class);
      	dto.setList(vos);
      	// 4.返回
      	return dto; 
      }
      
      
      posted @ 2024-02-16 14:27  赤葉秋楓  閱讀(126)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 国产suv精品一区二区五| 久久久无码精品亚洲日韩按摩| 一区二区三区国产偷拍| 欧美人成精品网站播放| AV秘 无码一区二| 国产一区二区三区日韩精品| 久久精品国产一区二区三| 在线观看国产一区亚洲bd| 国产成人精品亚洲午夜| 蜜臀精品国产高清在线观看| 一区二区三区四区自拍偷拍| 动漫av网站免费观看| 欧美一本大道香蕉综合视频| 精品国产一区二区色老头| 国产免费人成网站在线播放| 国产亚洲精品第一综合| 97香蕉碰碰人妻国产欧美| 国产一区二区不卡在线看| 欧美交a欧美精品喷水| 99在线精品国自产拍中文字幕| 国产极品精品自在线不卡| 国产 亚洲 制服 无码 中文 | 国产精品一码二码三码四码| 国产精品有码在线观看| 熟妇人妻无码中文字幕老熟妇| 自偷自拍亚洲综合精品| 无码av不卡免费播放| 国产免费久久精品44| 中国少妇无码专区| 人妻中文字幕不卡精品| 婷婷六月色| 亚洲国产99精品国自产拍| 久久久久无码精品国产h动漫| 久久99国产乱子伦精品免费| 亚洲国产精品国自拍av| 亚洲V天堂V手机在线| 亚洲AV天天做在线观看| 另类 专区 欧美 制服| 男人扒女人添高潮视频| 天堂网亚洲综合在线| 澎湖县|