1、数据库的表信息
CREATE TABLE `STUDENT1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`local` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`status` int(20) NULL DEFAULT 1,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1025 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
2、pom依赖
com.baomidou
mybatis-plus-boot-starter
3.5.0
org.webjars
jquery
3.5.1
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
com.mysql
mysql-connector-j
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
3、application.propertis文件
# 配置数据库源连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db2?useSSL=true&useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=****
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.logic-delete-value=0
mybatis-plus.global-config.db-config.logic-not-delete-value=1
# 开发阶段关闭thymeleaf的模板缓存
spring.thymeleaf.cache=false
3、mybatis-plus5.3.0的分页配置
@Configuration
public class Myconfig {
//mybatisPlus 分页配置
@Bean
public PaginationInnerInterceptor paginationInnerInterceptor() {
return new PaginationInnerInterceptor();
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor ();
mybatisPlusInterceptor.setInterceptors (Collections.singletonList (paginationInnerInterceptor ()));
return mybatisPlusInterceptor;
}
}
1、 pojo
2、mapper
@Mapper
public interface StudentMapper extends BaseMapper {
}
3、service
public interface StudentService extends IService {
}
@Service
public class StudentServiceImpl extends ServiceImpl implements StudentService{
}
4、controller
@Controller
public class StudentController {
@Resource
private StudentServiceImpl service;
@Resource
private StudentMapper studentMapper;
@RequestMapping("/show")
public String show(Model model,
@RequestParam(required = true, defaultValue = "1") Integer pageNum,
@RequestParam(required = true, defaultValue = "3") Integer pageSize) {
Page page = new Page<>(pageNum, pageSize);
// Page pages = studentMapper.selectPage(page, null);
Page pages = service.page(page, null);
model.addAttribute("pages", pages);
return "show";
}
@RequestMapping("/show1")
public String show1(Model model,
@RequestParam(required = true, defaultValue = "1") Integer pageNum,
@RequestParam(required = true, defaultValue = "3") Integer pageSize,
String likename) {
Page page = new Page<>(pageNum, pageSize);
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.like("name",likename);
// Page pages = studentMapper.selectPage(page, queryWrapper);
Page pages = service.page(page, queryWrapper);
model.addAttribute("pages", pages);
model.addAttribute("likename",likename);
return "show";
}
@RequestMapping("/toAddStudent")
public String toAddStudent() {
return "addUser";
}
@RequestMapping("/addStudent")
public String addUser(Student1 student1) {
studentMapper.insert(student1);
return "redirect:show";
}
@RequestMapping("/toUpdate")
public String toUpdate(int id,
Model model) {
Student1 student1 = studentMapper.selectById(id);
model.addAttribute("list", student1);
return "update";
}
@RequestMapping("/update")
public String update(Student1 student1) {
studentMapper.updateById(student1);
return "redirect:show";
}
@RequestMapping("/deleteUserByIds")
public String deleteUserByIds(String ids) {
ArrayList list = new ArrayList<>();
String[] split = ids.split("-");
for (String s : split) {
list.add(Integer.parseInt(s));
}
studentMapper.deleteBatchIds(list);
return "redirect:/show";
}
@GetMapping("deleteById")
public String deleteById(int id) {
studentMapper.deleteById(id);
return "redirect:/show";
}
}
1、show.html
Title
2、addStudent.html
3、update.html