<!-- mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- mybatis pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!-- mybatis 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
yml
文件中的配置### Pagehelper ###
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
### Mybatis Config ###
mybatis:
typeAliasesPackage: com.example.janche.**.domain # 实体类的包路径
mapperLocations: classpath:mapper/**/*.xml # mapper 的文件路径
### 通用 Mapper ###
mapper:
IDENTITY: mysql
notEmpty: false
mappers: # 此处可以配置多个通用mapper,因为很多时候,一个是无法满足需求的,后文会介绍到
- com.example.janche.common.core.Mapper
- com.example.janche.common.core.TkMapper
mapper
新建两个通用 Mapper
的接口 ,注意此Mapper的路径需要同UserMapper等业务Mapper的位置区分开。
package com.example.janche.common.core;
import tk.mybatis.mapper.common.*;
public interface Mapper<T> extends
BaseMapper<T>,
MySqlMapper<T>,
IdsMapper<T>,
ConditionMapper<T>,
ExampleMapper<T>{
}
package com.example.janche.common.core;
import tk.mybatis.mapper.common.*;
public interface TkMapper<T> extends
BaseMapper<T>,
MySqlMapper<T>,
ConditionMapper<T>,
ExampleMapper<T>{
}
上面两个mapper中,第一个多继承了IdsMapper,其余都是一样的,那为什么需要TkMapper呢,因为继承了IdsMapper的业务Mapper,其实体类中必须且只能有一个主键ID,联合主键或者没有主键都不行,否则启动会报错。
package com.example.janche.user.dao;
import com.example.janche.common.core.Mapper;
import com.example.janche.user.domain.User;
public interface UserMapper extends Mapper<User> {
}
package com.example.janche.user.dao;
import com.example.janche.common.core.TkMapper;
import com.example.janche.user.domain.UserAndRole;
public interface UserAndRoleMapper extends TkMapper<UserAndRole> {
}
// UserController 层接口:
@GetMapping("/list")
public RestResult list(@ApiParam(value = "分页信息") PageParam pageParam,
@ApiParam(value = "筛选条件") String query) {
List<User> list = userService.list(pageParam, query);
PageInfo pageInfo = new PageInfo(list);
return ResultGenerator.genSuccessResult(pageInfo);
}
----------------------------------------------------------------------------
// UserServiceImpl 实现类:
@Resource
private UserMapper userMapper;
@Override
public List<User> list(PageParam pageParam, String query) {
Example example = new Example(User.class);
example.or().andLike("username", "%"+query+"%");
example.or().andLike("actualName", "%"+query+"%");
example.or().andLike("sex", "%"+query+"%");
// PageHelper 只须在查询语句真正执行前调用即可完成分页
PageHelper.startPage(pageParam.getPage(), pageParam.getSize(), pageParam.getOrderBy());
return userMapper.selectByExample(example);
}
到此,整个接口的调用,不用写一句SQL代码,只要是单表操作,均可通过pagehelper+通用mapper的形式实现。
// UserServiceImpl 实现类:
@Resource
private UserMapper userMapper; // 继承自Mapper
@Resource
private UserAndRoleMapper userAndRoleMapper; // 继承自TkMapper
/**
* 批量删除用户
* @param ids
*/
@Override
public void deleteUser(String ids) {
List<String> Ids = Arrays.stream(ids.split(",")).collect(Collectors.toList());
// 删除用户和角色的关联
Example example = new Example(UserAndRole.class);
example.and().andIn("userId", Ids);
userAndRoleMapper.deleteByExample(example);
// 删除用户
userMapper.deleteByIds(ids);
}
------------------------------------------------------------------------------------
// UserAndRole 实体类
@Table(name = "user_role")
public class UserAndRole implements Serializable {
@Column(name = "role_id")
private Long roleId;
@Column(name = "user_id")
private Long userId;
}
上面的例子使用到了TkMapper,因为UserAndRole实体类中没有唯一主键ID,它是联合主键,IdsMapper中会根据主键Ids批量删除,因此UserAndRoleMapple不能继承 继承过IdsMapper的 Mapper。
一套整合完善的springboot2脚手架工程,源码地址如下:GitHub项目地址