欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

mybatis-plus批处理IService的实现示例

时间:2021-04-04 10:13:25|栏目:JAVA代码|点击:

一、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>{

}

上一篇:Java实现SSH模式加密

栏    目:JAVA代码

下一篇:关于Spring AOP使用时的一些问题汇总

本文标题:mybatis-plus批处理IService的实现示例

本文地址:http://www.codeinn.net/misctech/94030.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有