通用Mapper使用

通用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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值