SpringBoot&CRUD练习

一.Category

Mapper:
 


@Mapper
public interface CategoryMapper {
 
    @Select("select * from category_ ")
    List<Category> findAll();
     
    @Insert(" insert into category_ ( name ) values (#{name}) ")
    public int save(Category category); 
     
    @Delete(" delete from category_ where id= #{id} ")
    public void delete(int id);
         
    @Select("select * from category_ where id= #{id} ")
    public Category get(int id);
       
    @Update("update category_ set name=#{name} where id=#{id} ")
    public int update(Category category); 
 
}

        用于操作数据库中的 category_ 表。通过在接口中使用 MyBatis 提供的注解,可以方便地实现对数据库的增删改查操作。同时,这个接口也可以被注入到 Spring 容器中,从而在其他组件中使用。在使用时,只需要在需要的地方通过 @Autowired 注解将其注入即可。通过这种方式,可以方便地实现对数据库的访问和操作。

        在Spring Boot中使用MyBatis进行CRUD操作,可以通过配置数据源、定义实体类和Mapper接口、使用Mapper接口进行数据访问和使用MyBatis的注解或XML配置文件等多种方式来实现。这些方式都能够很好地结合Spring Boot的特性,使得CRUD操作更加简单

Controller:

@Controller
public class CategoryController {
    @Autowired 
    CategoryMapper categoryMapper;
      
    @RequestMapping("/addCategory")
    public String listCategory(Category c) throws Exception {
        categoryMapper.save(c);
        return "redirect:listCategory";
    }
    @RequestMapping("/deleteCategory")
    public String deleteCategory(Category c) throws Exception {
        categoryMapper.delete(c.getId());
        return "redirect:listCategory";
    }
    @RequestMapping("/updateCategory")
    public String updateCategory(Category c) throws Exception {
        categoryMapper.update(c);
        return "redirect:listCategory";
    }
    @RequestMapping("/editCategory")
    public String listCategory(int id,Model m) throws Exception {
        Category c= categoryMapper.get(id);
        m.addAttribute("c", c);
        return "editCategory";
    }
     
    @RequestMapping("/listCategory")
    public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
        PageHelper.startPage(start,size,"id desc");
        List<Category> cs=categoryMapper.findAll();
        PageInfo<Category> page = new PageInfo<Category>(cs);
        m.addAttribute("page", page);        
        return "listCategory";
    }
     

         使用了Spring的注解来实现请求映射和依赖注入等功能。在Spring Boot中,使用@Controller注解来标记一个类为控制器,同时使用@RequestMapping注解来指定请求的URL路径和请求方法。在控制器中,使用@Autowired注解来自动注入CategoryMapper类的实例,从而可以在控制器中使用该实例来进行数据访问操作。另外,该控制器还使用了PageHelper插件来实现分页查询,并使用Model对象将查询结果传递给视图层进行展示。

二.Product

 Mapper:

 Controller:
 

@Controller
public class ProductController {
    @Autowired 
    ProductMapper productMapper;
      
    @RequestMapping("/addProduct")
    public String listProduct(Product c) throws Exception {
    	productMapper.save(c);
        return "redirect:listProduct";
    }
    @RequestMapping("/deleteProduct")
    public String deleteProduct(Product c) throws Exception {
    	productMapper.delete(c.getId());
        return "redirect:listProduct";
    }
    @RequestMapping("/updateProduct")
    public String updateProduct(Product c) throws Exception {
    	productMapper.update(c);
        return "redirect:listProduct";
    }
    @RequestMapping("/editProduct")
    public String listProduct(int id,Model m) throws Exception {
    	Product c= productMapper.get(id);
        m.addAttribute("c", c);
        return "editProduct";
    }
    @RequestMapping("/listProduct")
    public String listProduct(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
        PageHelper.startPage(start,size,"id desc");
        List<Product> cs=productMapper.findAll();
        PageInfo<Product> page = new PageInfo<Product>(cs);
        m.addAttribute("page", page);        
        return "listProduct";
    }
     
}

 

        这段代码是一个基于Spring MVC框架的Java控制器类,用于处理与产品相关的HTTP请求。在Spring Boot框架中,该控制器类可以直接被自动扫描并注册为一个Bean,无需手动配置。

在Spring Boot框架中,使用@Controller注释来标识该类是一个控制器,使用@Autowired注释将ProductMapper接口的实现类注入到控制器中。此外,使用@RequestMapping注释将HTTP请求映射到方法,使用@RequestParam注释指定请求参数,并使用Model对象将数据传递到视图中。

在列出方法中,使用PageHelper类来实现分页功能,该类可以自动拦截MyBatis执行的SQL语句,并在其前面添加分页相关的SQL语句,以实现分页查询。这体现了Spring Boot框架对第三方库的无缝集成能力,使得开发者可以更加方便地使用第三方库的功能。

此外,该控制器类还使用了@RestControllerAdvice注释来定义全局异常处理器,用于处理控制器中抛出的异常。这体现了Spring Boot框架对全局异常处理的支持

三.Users

 代码的修改原理同上,不再赘述

四.概述

        首先,我们需要在Spring Boot中集成Mybatis。这个过程可以通过添加相应的依赖和配置来完成。具体来说,我们需要添加Mybatis和Mybatis-Spring的依赖,同时在application.properties文件中配置Mybatis的相关属性,如数据源、Mapper文件的路径等等。

        其次,需要编写Mapper接口和对应的Mapper XML文件。这个过程是实现CRUD和分页功能的核心部分。在Mapper接口中,我们需要定义一些方法,用于实现各种操作,如插入数据、更新数据、查询数据等等。这些方法的实现是通过Mapper XML文件来完成的。在Mapper XML文件中,编写相应的SQL语句,并将其映射到Mapper接口中的方法上。

最后,需要在Controller中调用Mapper接口中的方法,来实现具体的业务逻辑。在这个过程中,我们需要注意一些细节问题,如参数的传递、异常的处理等等。同时还需要实现分页功能,这个可以通过调用Mybatis的分页插件来完成。分页插件可以帮助我们快速地实现分页功能,而不需要手动编写复杂的SQL语句。        

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值