package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author ${范涛之}
* @Description
* @create 2022-01-01 12:54
*/
@RestController
public class RootApp {
@GetMapping(value = "/")
public String Hello(){
return "Hello 范涛之";
}
}
@GetMapping(value = "/Insert")
public void Insert(){
User user = new User();
user.setName("ftz");
user.setAge(20);
user.setEmail("[email protected]");
int result = userMapper.insert(user);
System.out.println(result);
System.out.println(user);
}
具体来说,@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
具体来说,@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。
@GetMapping(value = "/Update")
public void Update(){
User user = new User();
user.setId(1L);
user.setAge(60);
user.setName("王老五");
int result = userMapper.updateById(user);
System.out.println(result);
}
/**
* 根据id查询记录
*/
@GetMapping(value = "/SelectById")
public void SelectById(){
User user = userMapper.selectById(1);
System.out.println(user);
}
/**
* 通过多个id批量查询
*/
@GetMapping(value = "/SelectByIdS")
public void SelectByIds(){
List<User> users = userMapper.selectBatchIds(Arrays.asList(1,2,3));
users.forEach(System.out::println);
}
/**
* 条件查询
*/
@GetMapping(value = "/SelectByMap")
public void SelectByMap(){
HashMap<String ,Object> map = new HashMap<>();
map.put("name","ftz");
map.put("age","20");
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
/**
* 分页
*/
@GetMapping(value = "/SelectPage")
public void SelectPage(){
Page<User> page = new Page<>(1,5);
userMapper.selectPage(page,null);
page.getRecords().forEach(System.out::println);
System.out.println(page.getCurrent());
System.out.println(page.getPages());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.hasNext());
System.out.println(page.hasPrevious());
}
/**
* selectMapsPage分页:结果集是Map
*/
@GetMapping(value = "/testSelectMapsPage")
public void testSelectMapsPage(){
Page<User> page = new Page<>(1,5);
IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, null);
//注意:此行必须使用 mapIPage 获取记录列表,否则会有数据类型转换错误
mapIPage.getRecords().forEach(System.out::println);
System.out.println(page.getCurrent());
System.out.println(page.getPages());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.hasNext());
System.out.println(page.hasPrevious());
}
/**
* 删除1、根据id删除记录
*/
@DeleteMapping (value = "/SelectByMap")
public void DeleteById(){
int result = userMapper.deleteById(4L);
System.out.println(result);
}
/**
* 删除2、批量删除
*/
@DeleteMapping (value = "/DeleteBatchIds")
public void DeleteBatchIds(){
int result = userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
System.out.println(result);
}
/**
* 删除3、简单的条件查询删除
*/
@DeleteMapping (value = "/DeleteByMap")
public void DeleteByMap(){
HashMap<String, Object> map = new HashMap<>();
map.put("name", "ftz");
map.put("age", 20);
int result = userMapper.deleteByMap(map);
System.out.println(result);
}
/**
* 逻辑删除
*/
@GetMapping (value = "/LogicDelete")
public void LogicDelete(){
int result = userMapper.deleteById(8L);
System.out.println(result);
}
/**
* 逻辑删除后的查询
*/
@GetMapping (value = "/LogicDeleteSelect")
public void LogicDeleteSelect(){
User user = new User();
List users = userMapper.selectList(null);
users.forEach(System.out::println);
}
/**
* 性能分析测试
*/
@GetMapping (value = "/Performance")
public void Performance(){
User user = new User();
user.setName("哈哈哈");
user.setEmail("[email protected]");
user.setAge(58);
userMapper.insert(user);
}