MyBatisplus CRUD 就是增删改查

MyBatisplus CRUD 就是增删改查

@SpringBootTest
@RunWith(SpringRunner.class)
class UserMapperTest {

    @Autowired
    private UserMapper userMapper;


    /**
     * 根据id查询
     */
    @Test
    public void testSelectById() {

        User user = userMapper.selectById(1L);
        System.out.println(user);


    }



    /**
     * 添加
     */
    @Test
    public void testInsert() {

        User user = new User();

        //user.setId(6L);
        user.setUserName("itcast");
        user.setPassword("itheima");

        int count = userMapper.insert(user);
        System.out.println(count);


    }
    /**
     * 删除
     */
    @Test
    public void testDelete() {

/*

        //1. 根据id删除
        int count = userMapper.deleteById(8L);

*/
/*
        //2. 根据id集合批量删除
        List ids = new ArrayList();
        ids.add(6);
        ids.add(7);
        userMapper.deleteBatchIds(ids);
*/
        //3. 根据map构造条件,删除

        Map<String, Object> map = new HashMap<>();

        //delete from tb_user where user_name = ? and age = ?
        map.put("user_name","zhangsan");
        map.put("age","18");
        userMapper.deleteByMap(map);




    }


    /**
     * 修改
     */
    @Test
    public void testUpdateById() {

        User user = new User();
        user.setId(2L);
        user.setPassword("1111111");

        int count = userMapper.updateById(user);
    }


    /**
     * 分页查询:
     *  1. 当前页码:currentPage
     *  2. 每页显示条数:size
     *
     *  注意:使用mp的分页要设置一个拦截器!!!
            */
    @Test
    public void testSelectPage() {
        int current = 1;//当前页码
        int size = 2;//每页显示条数
        IPage<User> page = new Page(current,size);
        userMapper.selectPage(page,null);


        List<User> records = page.getRecords();//当前页的数据
        long pages = page.getPages();//总页数 2
        long total = page.getTotal();//总记录数 4

        System.out.println(records);
        System.out.println(pages);
        System.out.println(total);
    }
}

/**
 * 实体类的属性名和数据库的字段名 自动映射:
 *  1. 名称一样
 *  2. 数据库字段使用_分割,实体类属性名使用驼峰名称
 */

//@TableName("tb_user")
@Data
public class User {


    //设置id生成策略:AUTO 数据库自增
    //@TableId(type = IdType.AUTO)
    private Long id;
    //@TableField("user_name")
    private String userName;

    private String password;
    private String name;
    private Integer age;
    private String email;

    //不希望该值存入数据库
    //@TableField(exist = false)
    //private String info;

}

在springboot配置前缀
就可以不用写 @TableName注解,但类的名称要与数据库表名一致

mybatis-plus:
  global-config:
    db-config:
      # 表名前缀
      table-prefix: tb_
      # id生成策略 数据库自增
      id-type: auto
  configuration:
    # 日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

这是用配置文件,做的全局配置

进行分页查询,要使用分页插件,对分页插件配置。否则分页插件不会生效,这样就会去做查询全局。

@Configuration
public class PageConfig {

    /**
     * 3.4.0之前的版本用这个
     * @return
     */
   /* @Bean
    public PaginationInterceptor paginationInterceptor(){
        return  new PaginationInterceptor();
    }*/

    /**
     * 3.4.0之后提供的拦截器的配置方式
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

普希托夫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值