(三)mybatis-plus学习--测试Service

1 创建sevice层

(1)设置接口和抽象方法

public interface UserService extends IService<User> {
}

其中IService为mybatis-plus中的,包含CRUD操作,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

其中ServiceImpl<M,T>,M为Mapper,T为对应实体类。

进行测试:

测试代码为:

 @Test
    public void testGetCount(){
        long count = userService.count();
        System.out.println(count);
    }

输出结果为:

 ==>  Preparing: SELECT COUNT( * ) FROM user
==> Parameters: 
<==    Columns: COUNT( * )
<==        Row: 2
<==      Total: 1

(2)批量添加测试

 @Test
    public void testInsetMore(){
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setName("ybc"+i);
            user.setAge(20+i);
            list.add(user);
        }
        boolean b = userService.saveBatch(list);
        System.out.println(b);
    }

输出结果为:

 ==>  Preparing: INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
==> Parameters: 1572494254536179714(Long), ybc0(String), 20(Integer)
==> Parameters: 1572494254691368962(Long), ybc1(String), 21(Integer)
==> Parameters: 1572494254691368963(Long), ybc2(String), 22(Integer)
==> Parameters: 1572494254691368964(Long), ybc3(String), 23(Integer)
==> Parameters: 1572494254691368965(Long), ybc4(String), 24(Integer)
==> Parameters: 1572494254691368966(Long), ybc5(String), 25(Integer)
==> Parameters: 1572494254691368967(Long), ybc6(String), 26(Integer)
==> Parameters: 1572494254691368968(Long), ybc7(String), 27(Integer)
==> Parameters: 1572494254691368969(Long), ybc8(String), 28(Integer)
==> Parameters: 1572494254691368970(Long), ybc9(String), 29(Integer)
true

 2 常用注解

(1) @TableName

        当数据库名与实体类名不相同时,可以在实体类名上加上@TableName即可

        在当所有表都需要加前缀而和类不一致时,可在配置文件mybatis-plus中进行相关配置。

        

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # 设置实体类所对应的表名
  global-config:
    db-config:
      table-prefix: t_

 (2)TableId

表明主键不叫id的时候

   //将属性所对应的字段指定为主键
    @TableId
    private Long uid;
 // value属性用于指定主键的字段
    @TableId(value = "uid")

    private Long id;
  //将属性所对应的字段指定为主键
    // value属性用于指定主键的字段
    // type设置主键生成策略,默认雪花算法
    @TableId(value = "uid",type = IdType.AUTO)

全局设置自增:

 

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # 设置实体类所对应的表名
  global-config:
    db-config:
      table-prefix: t_
      #设置统一主键生成策略
      id-type: auto

 (3)TableField

当实体类与表字段名不一致时:

// 指定属性所对应的字段名
    @TableField("user_name")
    private String name;

 (4)@TableLogic

逻辑删除,可以使字段is_deleted 由0变为1,可做数据恢复,并在查询所有数据时标1的将不显示。

    @TableLogic
    private Integer isDeleted;

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值