目录
- @Table("user")
- public class User {
- @TableId(type = IdType.AUTO)
- private Long id;
- private String name;
- // ...
- // 省略getter和setter方法
- }
- public interface UserMapper extends BaseMapper<User> {
- // 自定义方法
- // ...
- }
在包结构下创建一个新的Java接口,用于定义业务逻辑的操作。通常,一个Service接口对应一个实体类,继承IService接口,并泛型
- public interface UserService extends IService<Users> {
- }
创建一个实现Service接口的类,并使用@Service注解进行标注。同样继承ServiceImpl实现类,并泛型
- @Service
- public class UserServiceImpl extends ServiceImpl
,Users> implements UserService { - }
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(CollectionentityList);
// 插入(批量)
boolean saveBatch(CollectionentityList, int batchSize);
参数说明
| 类型 | 参数名 | 描述 |
|---|---|---|
| T | entity | 实体对象 |
| Collection | entityList | 实体对象集合 |
| int | batchSize | 插入批次数量 |
// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, WrapperupdateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(CollectionentityList);
// 批量修改插入
boolean saveOrUpdateBatch(CollectionentityList, int batchSize);
参数说明
| 类型 | 参数名 | 描述 |
|---|---|---|
| T | entity | 实体对象 |
| Wrapper | updateWrapper | 实体对象封装操作类 UpdateWrapper |
| Collection | entityList | 实体对象集合 |
| int | batchSize | 插入批次数量 |
// 根据 queryWrapper 设置的条件,删除记录
boolean remove(WrapperqueryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(MapcolumnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection extends Serializable> idList);
参数说明
| 类型 | 参数名 | 描述 |
|---|---|---|
| Wrapper | queryWrapper | 实体包装类 QueryWrapper |
| Serializable | id | 主键 ID |
| Map | columnMap | 表字段 map 对象 |
| Collection extends Serializable> | idList | 主键 ID 列表 |
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(WrapperupdateWrapper);
// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, WrapperwhereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(CollectionentityList);
// 根据ID 批量更新
boolean updateBatchById(CollectionentityList, int batchSize);
参数说明
| 类型 | 参数名 | 描述 |
|---|---|---|
| Wrapper | updateWrapper | 实体对象封装操作类 UpdateWrapper |
| T | entity | 实体对象 |
| Collection | entityList | 实体对象集合 |
| int | batchSize | 更新批次数量 |
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(WrapperqueryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(WrapperqueryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
MapgetMap(Wrapper queryWrapper);
// 根据 Wrapper,查询一条记录
V getObj(Wrapper queryWrapper, Function super Object, V> mapper);
参数说明
| 类型 | 参数名 | 描述 |
|---|---|---|
| Serializable | id | 主键 ID |
| Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
| boolean | throwEx | 有多个 result 是否抛出异常 |
| T | entity | 实体对象 |
| Function super Object, V> | mapper | 转换函数 |
// 查询所有
Listlist();
// 查询列表
Listlist(Wrapper queryWrapper);
// 查询(根据ID 批量查询)
CollectionlistByIds(Collection extends Serializable> idList);
// 查询(根据 columnMap 条件)
CollectionlistByMap(Map columnMap);
// 查询所有列表
List
参数说明
| 类型 | 参数名 | 描述 |
|---|---|---|
| Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
| Collection extends Serializable> | idList | 主键 ID 列表 |
| Map | columnMap | 表字段 map 对象 |
| Function super Object, V> | mapper | 转换函数 |
// 无条件分页查询
IPagepage(IPage page);
// 条件分页查询
IPagepage(IPage page, Wrapper queryWrapper);
// 无条件分页查询
IPage> pageMaps(IPage page);
// 条件分页查询
IPage> pageMaps(IPage page, Wrapper queryWrapper);
参数说明
| 类型 | 参数名 | 描述 |
|---|---|---|
| IPage | page | 翻页对象 |
| Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(WrapperqueryWrapper);
参数说明
| 类型 | 参数名 | 描述 |
|---|---|---|
| Wrapper | queryWrapper | 实体对象封装操作类 QueryWrapper |
// 链式查询 普通
QueryChainWrapperquery();
// 链式查询 lambda 式。注意:不支持 Kotlin
LambdaQueryChainWrapperlambdaQuery();
- // 示例:
- query().eq("column", value).one();
- lambdaQuery().eq(Entity::getId, value).list();
// 链式更改 普通
UpdateChainWrapperupdate();
// 链式更改 lambda 式。注意:不支持 Kotlin
LambdaUpdateChainWrapperlambdaUpdate();
- // 示例:
- update().eq("column", value).remove();
- lambdaUpdate().eq(Entity::getId, value).update(entity);
- @Test//添加一万条数据
- public void saveBatch(){
- List<Users> list = new ArrayList<>(100);
- long b1 = System.currentTimeMillis();
- for (long i = 1; i <= 10000; i++) {
- if (i % 100 == 0){
- userService.saveBatch(list);
- list.clear();
- }
- list.add(users(i));
- }
- long b2 = System.currentTimeMillis();
- System.out.println("耗时:" + (b2 - b1));
- }
-
- private Users users(Long i){
- Users users = new Users();
- users.setId(i);
- users.setName("cxk" + i);
- return users;
- }
注意:jdbc参数要添加 rewriteBatchedStatements=true参数
lambdaQuery条件查询
- public List<Users> lamdbaQuery(String name,Integer min,Integer max){
- List<Users> list = userService.lambdaQuery()
- .like(name != null,Users::getName, name)//如果name不是null,则根据名称模糊查询
- .gt(min != null,Users::getAge, min)//如果min不是null,查大于多少岁
- .lt(max != null,Users::getAge, max)//如果max不是null,查小于多少岁
- .list();//结果为集合
- return list;
- }