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

时间:2021-05-19

一、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")@RestControllerpublic 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实体类)

@Servicepublic 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中的值对应着表名@Datapublic 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接口层

@Mapperpublic interface UserInfoDao extends BaseMapper<UserInfoEntity>{}

到此这篇关于mybatis-plus批处理IService的实现示例的文章就介绍到这了,更多相关mybatis-plus批处理IService内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章