1. 程式人生 > 程式設計 >mybatis-plus批處理IService的實現示例

mybatis-plus批處理IService的實現示例

一、pom檔案引入

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
       <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1.tmp</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus</artifactId>
        <version>3.3.1.tmp</version>
    </dependency>
     <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

二、Controller層

@RequestMapping("/user")
@RestController
public class UserController {
  @Autowired
   UserInfoService userInfoService;
  
  @RequestMapping("/add")
  public void addUser() {
    userInfoService.addUser();
  }
}

三、IService層(此處請確保繼承的是 mybatisplus下的 IService,上述的UserInfoEntity為實體類)

import com.baomidou.mybatisplus.extension.service.IService;
import com.entity.UserInfoEntity;

public interface UserInfoService extends IService<UserInfoEntity>{
  
  public void addUser();

}

四、ServiceImpl(UserInfoDao和UserInfoEntitty分別為業務對應的UserEntityDao介面和UserInfoEntitty實體類)

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao,UserInfoEntity> implements UserInfoService{
  @Override
  public void addUser() {
    Random r=new Random(100);
    String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
     Random random=new Random();
    Set<UserInfoEntity> entityList=new HashSet<UserInfoEntity>();
    for(int i=0;i<1000000;i++) {
      UserInfoEntity entity=new UserInfoEntity();
      entity.setAge(r.nextInt());
      int number=random.nextInt(62);
      entity.setName(""+str.charAt(number));
      entity.setEvaluate("good");
      entity.setFraction(r.nextLong());
      entityList.add(entity);
    }
    this.saveBatch(entityList);
  }

五、entity層

@TableName("user_info")//@TableName中的值對應著表名
@Data
public class UserInfoEntity {

  /**
   * 主鍵
   * @TableId中可以決定主鍵的型別,不寫會採取預設值,預設值可以在yml中配置
   * AUTO: 資料庫ID自增
   * INPUT: 使用者輸入ID
   * ID_WORKER: 全域性唯一ID,Long型別的主鍵
   * ID_WORKER_STR: 字串全域性唯一ID
   * UUID: 全域性唯一ID,UUID型別的主鍵
   * NONE: 該型別為未設定主鍵型別
   */
  @TableId(type = IdType.AUTO)
  private Long id;
  /**
   * 姓名
   */
  private String name;
  /**
   * 年齡
   */
  private Integer age;
  /**
   * 技能
   */
  private String skill;
  /**
   * 評價
   */
  private String evaluate;
  /**
   * 分數
   */
  private Long fraction;

六、Mapper介面層

@Mapper


public interface UserInfoDao extends BaseMapper<UserInfoEntity>{

}

到此這篇關於mybatis-plus批處理IService的實現示例的文章就介紹到這了,更多相關mybatis-plus批處理IService內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!