目录
IService接口
1、写实体类
@Table("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
// ...
// 省略getter和setter方法
}
2、写mapper接口
public interface UserMapper extends BaseMapper<User> {
// 自定义方法
// ...
}
3、写service接口
在包结构下创建一个新的Java接口,用于定义业务逻辑的操作。通常,一个Service接口对应一个实体类,继承IService接口,并泛型
public interface UserService extends IService<Users> {
}
4、写service接口的实现类
创建一个实现Service接口的类,并使用@Service注解进行标注。同样继承ServiceImpl实现类,并泛型
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,Users> implements UserService{
}
IService自带方法
1、save
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection<T> | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
2、SaveOrUpdate
// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Wrapper<T> | updat |