MyBatisPlus基本增删改查(IService的使用方法)

MyBatis-Plus中有一个接口 IService和其实现类 ServiceImpl,封装了常见的业务层逻辑
说明:

  • 通用 Service CRUD 封装IService接口,进一步封装 CRUD,采用 get 查询单行、 remove 删 除、save插入、update更新、 list查询集合、 page 分页,前缀命名方式区分 Mapper 层避免混淆
  • 泛型 T 为任意实体对象
  • 建议如果存在自定义通用 Service方法的可能,请创建自己的 IBaseService 继承 Mybatis-Plus 提供的基类
  • 官网地址:https://baomidou.com/pages/49cc81/#service-crud-%E6%8E%A5%E5%8F%
    A3

1. 创建Service接口和实现类

/**
 * UserService继承IService模板提供的基础功能
 */
public interface UserService extends IService<User> {
}
/**
 * ServiceImpl实现了IService,提供了IService中基础功能的实现
 * 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

2. 创建测试类

@SpringBootTest
public class MyBatisPlusServiceTest {
    @Autowired
    private UserService userService;
}

3. 测试Iservice的方法

1.测试查询记录数
@Test
    public void testGetCount(){
    	// SELECT COUNT( * ) FROM user
        long count = userService.count();
        System.out.println("查询总记录数为"+count);
    }
2.测试批量插入
@Test
    public void testSaveBatch() {
        // SQL长度有限制,海量数据插入单条SQL无法实行,
        // 因此MP将批量插入放在了通用Service中实现,而不是通用Mapper
        ArrayList<User> users = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
//            User user = new User(null,"keke"+i,23+i,"123"+i+"qq.com");
            User user = new User();
            user.setName("keke"+i);
            user.setAge(20+i);
            user.setEmail("34464"+i+i+"@qq.com");
            users.add(user);
        }
        boolean res = userService.saveBatch(users);
        System.out.println(res);
    }
  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码: 首先,需要在pom.xml文件中添加mybatis-plus和mysql数据库驱动的依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.7.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 然后,在application.yml中配置数据源和mybatis-plus的相关配置: ``` spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver # mybatis-plus配置 mybatis-plus: mapper-locations: classpath*:mapper/*.xml ``` 接着,定义一个User实体类: ```java @Data @NoArgsConstructor @AllArgsConstructor @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; private String email; } ``` 定义一个UserService接口,继承IService<User>: ```java public interface UserService extends IService<User> { } ``` 实现UserService接口: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } ``` 最后,定义一个UserMapper接口,使用Mybatis-plus的Mapper接口: ```java @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 这样就可以使用mybatis-plus进行增删改查了。例如,查询所有用户: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Override public List<User> getAll() { return list(); } } ``` 更多mybatis-plus的使用可以参考官方文档:https://mybatis.plus/guide/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值