1、创建controller包
2、创建
package com.xachen.product.controller;
import com.xachen.product.device.entity.Product;
import com.xachen.product.device.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//@RestController
@Controller
public class DeviceController {
@Autowired
private IProductService productService;
@ResponseBody
@RequestMapping("/getProduct")
public Product getUser(){
Product product = productService.getById(1);
System.out.println(product);
return product;
}
}
3、跑起来后可以测试下
4、也可以测试下分页功能
package com.xachen.product;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xachen.product.device.entity.Product;
import com.xachen.product.device.mapper.ProductMapper;
import com.xachen.product.device.service.IProductService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
class ProductApplicationTests {
@Autowired
private IProductService productService;
@Resource
private ProductMapper productMapper;
@Test
void contextLoads() {
}
@Test
void mybatisplusselect() {
Product product = productService.getById(1);
System.out.println(product);
}
@Test
void mybatisplusselectpage() {
LambdaQueryWrapper<Product> userLambdaQueryWrapper = Wrappers.lambdaQuery();
userLambdaQueryWrapper.like(Product::getName , "h");
Page<Product> userPage = new Page<>(1 , 2);//注意:第1页,每页2个
// Page<Map<String , Object>> mapPage = new Page<>(1 , 2 , false); //如果不查询总数
IPage<Product> userIPage = productMapper.selectPage(userPage , userLambdaQueryWrapper);
System.out.println("总页数: "+userIPage.getPages());
System.out.println("总记录数: "+userIPage.getTotal());
userIPage.getRecords().forEach(System.out::println);
}
}