Mybatis-Plus实现CRUD分页查询

Mybatis-plus实现简单的CRUD

Mybatis-plus简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生

代码

entity.java

entity.java
`@TableName("location")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Location {
    @TableField(value = "id")
    private int id;
    @TableField(value = "name")
    private String name;

    @TableField(value = "latitude")
    private String latitude;


}
`

service接口


    int add(Location location);


    int delete(int id);

    int update(Location id);

    List<Location> find(String name);

   List<Location> selectPage(Integer current,Integer size);


}

serviceImpl


@Service("locationService")
@Transactional

public class LocationServiceImpl implements LocationService{

@Autowired
private  LocationMapper locationMapper;

    @Override
    public int add(Location location) {
      return locationMapper.insert(location);
    }

    @Override
    public int delete(int id) {
        return   locationMapper.deleteById(id);

    }

    @Override
    public int update(Location id) {
        return locationMapper.updateById(id);
    }


    @Override
    public List<Location> find(String name) {
    Wrapper<Location> wrapper =new EntityWrapper<>();
    return  locationMapper.selectList(wrapper);

    }

  @Override
   public List<Location> selectPage(Integer current,Integer size) {
       Wrapper<Location> wrapper = new EntityWrapper<>();
       Page<Location> page = new Page<>(current,size);
     List<Location> locations = locationMapper.selectPage(page,wrapper);
     return  locations;

 }



Controller层

package com.liu.controller;

import com.liu.entity.Location;
import com.liu.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/cc")
public class LocationController {

    @Autowired
    private LocationService locationService;

    @RequestMapping("/add")
    public int add(@RequestBody Location location) {
        int a = locationService.add(location);
        return a;
    }

    @RequestMapping("/delete/{id}")
    public int delete(@PathVariable("id") int id) {
        return locationService.delete(id);


    }

    @RequestMapping("/update")
    public int update(@RequestBody Location id) {
        return locationService.update(id);
    }

    @RequestMapping("/find/{name}")
    public List<Location> findByName(@PathVariable("name") String name) {
        List<Location> name1 = locationService.find(name);
        return name1;

    }

    @RequestMapping("/page/{current}/{size}")
    public List<Location> selectBookPage(@PathVariable Integer current, @PathVariable Integer size) {
        List<Location> locations = locationService.selectPage(current, size);
        return locations;
    }

}



mapper层

@Mapper
public interface LocationMapper extends BaseMapper<Location> {

配置类
config


@EnableTransactionManagement
@Configuration
@MapperScan("com.liu.mapper") //Dao接口所在包
public class MybatisConfig {

        @Bean
        public PaginationInterceptor paginationInterceptor(){
            PaginationInterceptor page = new PaginationInterceptor();
           page.setDialectType("mysql");
            return page;
        }


}

.yml

server:
  port: 8080
spring:
 datasource:
   url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8
   username: 自己的账户
   password: 自己的密码
   driver-class-name: com.mysql.jdbc.Driver

#mybatis
mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml
  #实体扫描,多个package用逗号或者分号分隔
  type-aliases-package:  com.liu.entity
  #typeEnumsPackage: com.baomidou.springboot.entity.enums

整体框架结构图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值