一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

mybatis-plus批处理IService代码实现示例

时间:2020-08-24 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下mybatis-plus批处理IService代码实现示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

一、pom文件引入

    
      org.springframework.boot
      spring-boot-starter
    
    
       com.baomidou
        mybatis-plus-boot-starter
        3.3.1.tmp
    
    
        com.baomidou
        mybatis-plus
        3.3.1.tmp
    
     
      mysql
      mysql-connector-java
      runtime
    
    
        org.springframework.boot
       spring-boot-starter-web
    

二、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{
  
  public void addUser();

}

四、ServiceImpl(UserInfoDao和UserInfoEntitty分别为业务对应的UserEntityDao接口和UserInfoEntitty实体类)

@Service
public class UserInfoServiceImpl extends ServiceImpl implements UserInfoService{
  @Override
  public void addUser() {
    Random r=new Random(100);
    String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
     Random random=new Random();
    Set entityList=new HashSet();
    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{

}

热门栏目