boot+mybatisplus+swagger+mysql简单实现dao层增删改模糊查以及前端测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NsO23Eaa-1671167535518)(C:\Users\19666\AppData\Roaming\Typora\typora-user-images\image-20221216124424149.png)]

**

**

一、dao层

1、

@Mapper
public interface GoodsDao extends BaseMapper<Goods> {

}

2、

@Mapper
public interface StoreDao extends BaseMapper<Store> {
    
}

3、

@Mapper
public interface WarehouseDao extends BaseMapper<Warehouse> {
    
}

二、实体类

1、Goods

@Data
@TableName("tb_goods")
public class Goods {

    @TableId(value="goods_id",type= IdType.AUTO)
    private Integer id;                   //商品id
    @TableField(value = "goods_no")
    private String no;                //商品编号
    @TableField(value = "goods_price")
    private Integer price;               //商品单价
    @TableField(value = "goods_unit")
    private String unit;             //商品计量单位
    @TableField(value = "goods_type")
    private String type;             //商品类型
    @TableField(value = "goods_introduce")
    private String introduce;       //商品介绍

    @TableField(value = "goods_delete")
//    @TableLogic(value = "false",delval = "true") //默认0代表未删除,1代表删除    //可以写到配置中去
    private boolean deleted;             //逻辑删除字段
}

2、Store

@Data
@TableName("tb_store")
public class Store {
    @TableId(value="store_id",type= IdType.AUTO)
    private Integer id;                   //商品id
    @TableField(value = "goods_no")
    private String goodNo;                //商品编号
    @TableField(value = "warehouse_no")
    private String warehouseNo;               //商品单价
    @TableField(value = "goods_amount")
    private String amount;             //商品计量单位
    @TableField(value = "goods_unit")
    private String unit;             //商品类型
    @TableField(value = "store_delete")
//    @TableLogic(value = "false",delval = "true") //默认0代表未删除,1代表删除    //可以写到配置中去
    private boolean deleted;             //逻辑删除字段

}

3、Warehouse

@Data
@TableName("tb_warehouse")
public class Warehouse {
    @TableId(value = "warehouse_id",type = IdType.AUTO)
    private Integer id;
    @TableField(value = "warehouse_no") //value可以省略
    private String no;
    @TableField(value = "warehouse_name")
    private String name;
    @TableField(value = "warehouse_delete")
    private boolean deleted;
}

三、业务层

1、

@RestController
@RequestMapping("/goods")   //也可以用的postman测试
public class GoodsController {
//    @Autowired
    @Resource
    private GoodsDao goodsDao;

    @GetMapping
    public List<Goods> selectAll(){
        //查询所有
        LambdaQueryWrapper lqw=new LambdaQueryWrapper(null);
        List<Goods> list = goodsDao.selectList(lqw);
        System.out.println(list);
        return list;
    }
    @GetMapping("/{type}")
    public List<Goods> selectByType(@PathVariable String type){
        //模糊查询
        LambdaQueryWrapper<Goods> lqw=new LambdaQueryWrapper<>();
        lqw.like(Goods::getType,type);
        List<Goods> list = goodsDao.selectList(lqw);
        System.out.println(list);
        return list;
    }

   @PostMapping
    public int save(@RequestBody Goods goods){
        //新增操作
       int insert = goodsDao.insert(goods);
       return insert;
   }

   @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Integer id){
        //逻辑删除
       int i = goodsDao.deleteById(id);
       return i;
   }
   @PutMapping
    public int update(@RequestBody Goods goods){
        //更新
       int i = goodsDao.updateById(goods);
       return i;
   }

}

2、

@RestController
@RequestMapping("/store") //这里也可以用postman测试
public class StoreController {

    @Resource
    private StoreDao storeDao;

    @GetMapping
    public List<Store> selectAll(){
        //查询所有
        LambdaQueryWrapper<Store> lqw = new LambdaQueryWrapper(null);
        List<Store> list = storeDao.selectList(lqw);
        System.out.println(list);
        return list;
    }

    @PutMapping
    public int update(@RequestBody Store store){
        //更新
        int i = storeDao.updateById(store);
        return i;
    }
}

3、

@RestController
@RequestMapping("/warehouse")  //也可以用的postman测试
public class WarehouseController {
    @Resource
    private WarehouseDao warehouseDao;
    @GetMapping
    public List<Warehouse> selectAll(){
        //查询所有
        List<Warehouse> warehouses = warehouseDao.selectList(null);
        return warehouses;
    }
    @GetMapping("/{name}")
    public List<Warehouse> selectByName(@PathVariable String name){
        //模糊查询
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
        lqw.like(Warehouse::getName,"仓库");
        List<Warehouse> warehouses = warehouseDao.selectList(lqw);
        return warehouses;
    }
    @PostMapping
    public int save(@RequestBody Warehouse warehouse){
        //新增仓库信息
        int insert = warehouseDao.insert(warehouse);
        return insert;
    }
    @PutMapping
    public int update(@RequestBody Warehouse warehouse){
        //修改仓库信息
        int i = warehouseDao.updateById(warehouse);
        return i;
    }
    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Integer id){
        int i = warehouseDao.deleteById(id);
        return i;
    }
}

四、配置swagger(config.SwaggerConfig)

@Configuration
@EnableSwagger2 //开启swagger
public class SwaggerConfig {
    //配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors配置扫描接口的方式
                //basePackage指定要扫描的包
                //any()扫描全部
                //none()不扫描
                .apis(RequestHandlerSelectors.basePackage("com.llw.controller"))
                //paths()过滤路径
                .build()
                ;
    }
    //配置swagger信息=apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact=new Contact("李龙威","http://llw.com","1966641163@qq.com");
        return new ApiInfo(
                "龙威",
                "杰杰",
                "v1.0",
                "http://llw.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}

//Test

@SpringBootTest
class LlwApplicationTests {

    @Resource
    private GoodsDao goodsDao;

    @Test
    void selectAll() {
        //查询全部
        List<Goods> list = goodsDao.selectList(null);
        System.out.println(list);
    }

    @Test
    void selectByType(){
        //模糊查询
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.like(Goods::getType,"水");
        List<Goods> goods = goodsDao.selectList(lqw);
        System.out.println(goods);
    }

    @Test
    void save(){
        //增加
        Goods goods=new Goods();
        goods.setNo("3");
        goods.setPrice(40);
        goods.setUnit("30");
        goods.setType("蔬菜");
        goods.setIntroduce("有营养");
        int insert = goodsDao.insert(goods);
        System.out.println(insert);
    }

    @Test
    void deleteById(){
        int i = goodsDao.deleteById(3);
        System.out.println(i);
    }

    @Test
    void update(){
        Goods goods=new Goods();
        goods.setId(4);
        goods.setIntroduce("好吃");
        int i = goodsDao.updateById(goods);
        System.out.println(i);
    }

    @Autowired
    private StoreDao storeDao;
    @Test
    void selectAllStore(){
        List<Store> stores = storeDao.selectList(null);
        System.out.println(storeDao);
    }

    @Autowired
    private WarehouseDao warehouseDao;
    @Test
    void selectAllWarehouse(){
        //查询全部
        List list = warehouseDao.selectList(null);
        System.out.println(list);
    }
    @Test
    void selectByName(){
        //模糊查询
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
        lqw.like(Warehouse::getName,"水果");
        List<Warehouse> warehouses = warehouseDao.selectList(lqw);
        System.out.println(warehouses);
    }
    @Test
    void saveWarehouse(){
        //增加仓库信息
        Warehouse warehouse=new Warehouse();
        warehouse.setNo("2");
        warehouse.setName("仓库2");
        int insert = warehouseDao.insert(warehouse);
        System.out.println(insert);
    }
    @Test
    void updateWarehouse(){
        //修改仓库信息
        Warehouse warehouse = new Warehouse();
        warehouse.setId(2);
        warehouse.setName("仓库t");
        int update = warehouseDao.updateById(warehouse);
        System.out.println(update);
    }

    @Test
    void deleteByIdWarehouse(){
        int i = warehouseDao.deleteById(2);
        System.out.println(i);
    }
}
    //修改仓库信息
    Warehouse warehouse = new Warehouse();
    warehouse.setId(2);
    warehouse.setName("仓库t");
    int update = warehouseDao.updateById(warehouse);
    System.out.println(update);
}

@Test
void deleteByIdWarehouse(){
    int i = warehouseDao.deleteById(2);
    System.out.println(i);
}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于生产级别的Spring应用程序的框架。它简化了Spring应用程序的配置和部署过程,并提供了一套强大的开发工具和约定,使开发人员能够更专注于业务逻辑的实现MyBatis Plus是MyBatis的增强工具,它提供了一系列的便利功能和增强特性,使得使用MyBatis更加简单和高效。它包括了代码生成器、分页插件、逻辑删除、乐观锁等功能,可以大大提高开发效率。 Redis是一个开源的内存数据库,它支持多种数据结构,如字符串、哈希、列表、集合、有序集合等。Redis具有高性能、高可用性和可扩展性的特点,常用于缓存、消息队列、分布式锁等场景。 Driver是指数据库驱动程序,它是用于连接数据库和执行SQL语句的软件组件。在Spring Boot中,我们可以通过配置数据源和引入相应的数据库驱动程序来实现与数据库的交互。 Knife4j是一款基于Swagger的API文档生成工具,它提供了更加美观和易用的界面,可以方便地看和测试API接口。 Swagger是一套用于设计、构建、文档化和使用RESTful风格的Web服务的工具。它可以自动生成API文档,并提供了交互式的界面,方便开发人员进行接口测试和调试。 JWT(JSON Web Token)是一种用于身份验证和授权的开放标准。它通过在用户和服务器之间传递加密的JSON对象来实现身份验证和授权功能,避免了传统的基于Session的身份验证方式带来的一些问题。 Spring Security是Spring提供的一个安全框架,它可以集成到Spring Boot应用程序中,提供身份验证、授权、攻击防护等安全功能。通过配置Spring Security,我们可以实现对API接口的访问控制和权限管理。 关于Spring Boot + MyBatis Plus + Redis + Driver + Knife4j + Swagger + JWT + Spring Security的Demo,你可以参考以下步骤: 1. 创建一个Spring Boot项目,并引入相应的依赖,包括Spring BootMyBatis Plus、Redis、数据库驱动程序等。 2. 配置数据源和数据库驱动程序,以及MyBatis Plus的相关配置,如Mapper扫描路径、分页插件等。 3. 集成Redis,配置Redis连接信息,并使用RedisTemplate或者Jedis等工具类进行操作。 4. 集成Knife4j和Swagger,配置Swagger相关信息,并编写API接口文档。 5. 集成JWT和Spring Security,配置安全相关的信息,如登录认证、权限管理等。 6. 编写Controller的代码,实现具体的业务逻辑。 7. 运行项目,通过Swagger界面进行接口测试。 希望以上内容对你有帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小白龙威

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

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

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

打赏作者

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

抵扣说明:

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

余额充值