通用Mapper
官方:https://gitee.com/free/Mapper?_from=gitee_search
文档:https://gitee.com/free/Mapper.wiki.git
- 只能对单表进行操作。
1.integration 文档中有三种集成方式,其中:springboot 集成方式示例:
1.1 引入依赖:
<!-- SpringBoot通用Mapper依赖 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
版本号:http://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter
注意:引入该 starter 时,和 MyBatis 官方的 starter 没有冲突,但是官方的自动配置不会生效!
1.2 启用方式:
基于 starter 的自动配置
如果你需要对通用 Mapper 进行配置,你可以在 Spring Boot 的配置文件中配置 mapper. 前缀的配置。
例如在 yml 格式中配置:
mapper:
mappers:
- tk.mybatis.mapper.common.Mapper
- tk.mybatis.mapper.common.Mapper2
notEmpty: true
在 properties 配置中:
mapper.mappers=tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.Mapper2
mapper.notEmpty=true
基于 @MapperScan 注解的手工配置
@tk.mybatis.spring.annotation.MapperScan(basePackages = "扫描包")
@SpringBootApplication
public class SampleMapperApplication implements CommandLineRunner {
DAO:
@Repository
public interface UserBaseShouMapper extends Mapper<UserBaseShou>, InsertListMapper<UserBaseShou>, ExampleMapper<UserBaseShou> {
}
Controller:
@GetMapping("/getList")
public List<UserBaseShou> getList() {
//通用mapper的获取列表方法
return userBaseShouMapper.selectAll();
}
@GetMapping("/getList/{userId}")
public List<UserBaseShou> query(@PathVariable String userId){
Example con = new Example(UserBaseShou.class);
Example.Criteria criteria = con.createCriteria();
criteria.andEqualTo("userid", userId);
//复杂条件查询方法
return userBaseShouMapper.selectByExample(con);
}
Entity: 如果表名字段名不同,可以使用别名
@Data
@Table(name = "UserBaseShou")
public class UserBaseShou {
@Id
private String userid;