Mybatis-Plus官网:https://baomidou.com/
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库,具体支持情况如下,如果不在下列表查看分页部分教程 PR 您的支持。
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.2version>
dependency>
#server.port=8088
#server.servlet.context-path=/springboot
#spring.web.resources.static-locations=classpath:/templates,file:C:/Users/Admin/OneDrive/
#????
#?????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# ????
spring.datasource.url=jdbc:mysql://localhost:3307/springboot?characterEncoding=utf8&serverTimezone=UTC
# ??????
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.name=defaultDataSource
#mybatis?? sql??????
#????
mybatis-plus.type-aliases-package=com.example.springboot2.pojo
# sql????xml??
mybatis-plus.mapper-locations=classpath:mappers/*.xml
#??????
logging.level.com.example.springboot2=debug
package com.example.springboot2;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.springboot2.dao")
public class Springboot2Application {
// devTools ->热更新 ->代码修改不需要重启项目
public static void main(String[] args) {
SpringApplication.run(Springboot2Application.class, args);
}
}
package com.example.springboot2.pojo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
//TableName 对应的表名
@TableName("student")
@Data
public class Student {
// TableId 表的主键
@TableId
private int id;
@TableField("age")
private int age;
private String name;
}
package com.example.springboot2.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springboot2.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot2.dao.StudentMapper">
mapper>
package com.example.springboot2.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.springboot2.pojo.Student;
public interface StudentService extends IService<Student> {
}
package com.example.springboot2.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot2.dao.StudentMapper;
import com.example.springboot2.pojo.Student;
import com.example.springboot2.service.StudentService;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
}
package com.example.springboot2.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot2.pojo.Student;
import com.example.springboot2.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("studentlist")
public String studentlist(Model model){
model.addAttribute("studentlist",studentService.list());
return "studentlist.html";
}
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>学生列表h1>
<table border="1">
<tr>
<td>学生idtd>
<td>学生年龄td>
<td>学生姓名td>
tr>
<tr th:each="student:${studentlist}">
<td th:text="${student.id}">学生idtd>
<td th:text="${student.age}">学生年龄td>
<td th:text="${student.name}">学生姓名td>
tr>
table>
body>
html>
@RequestMapping("addstudent")
public String addstudent(){
return "addstudent.html";
}
@RequestMapping("addstudentcommit")
public String addstudentcommit(Student student){
studentService.save(student);
return "redirect:/studentlist";
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>增加学生h1>
<form th:action="@{/addstudentcommit}" method="post">
<div>学号:<input name="id">div>
<div>姓名:<input name="name">div>
<div>年龄:<input name="age">div>
<div><input type="submit" value="提交">div>
form>
body>
html>
<a th:href="@{/addstudent}">增加学生a>
@RequestMapping("deletestudentbyid")
public String deletestudentbyid(Model model,int id){
studentService.removeById(id);
return "forward:/studentlist";
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>学生列表h1>
<a th:href="@{/addstudent}">增加学生a>
<table border="1">
<tr>
<td>学生idtd>
<td>学生年龄td>
<td>学生姓名td>
<td>删除td>
tr>
<tr th:each="student:${studentlist}">
<td th:text="${student.id}">学生idtd>
<td th:text="${student.age}">学生年龄td>
<td th:text="${student.name}">学生姓名td>
<th>
<a th:href="@{deletestudentbyid(id=${student.id})}">删除学生a>
th>
tr>
table>
body>
html>
@RequestMapping("updatestudent")
public String updatestudent(Model model,Student student){
model.addAttribute("id",student.getId());
model.addAttribute("age",student.getAge());
model.addAttribute("name",student.getName());
return "updatestudent.html";
}
@RequestMapping("updatestudentcommit")
public String updatestudentcommit(Student student){
studentService.updateById(student);
return "redirect:/studentlist";
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>
更新页面
h1>
<form th:action="@{/updatestudentcommit}" method="post">
<div>id:<input name="id" th:value="${id}">div>
<div>姓名:<input name="name" th:value="${name}">div>
<div>年龄:<input name="age" th:value="${age}">div>
<div><input type="submit" value="提交">div>
form>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>学生列表h1>
<a th:href="@{/addstudent}">增加学生a>
<table border="1">
<tr>
<td>学生idtd>
<td>学生年龄td>
<td>学生姓名td>
<td>删除td>
<td>更新td>
tr>
<tr th:each="student:${studentlist}">
<td th:text="${student.id}">学生idtd>
<td th:text="${student.age}">学生年龄td>
<td th:text="${student.name}">学生姓名td>
<th>
<a th:href="@{deletestudentbyid(id=${student.id})}">删除学生a>
th>
<th>
<a th:href="@{/updatestudent(id=${student.id},age=${student.age},name=${student.name})}">更新学生a>
th>
tr>
table>
body>
html>
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>2.2version>
dependency>
package com.example.springboot2.utils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import java.util.Collections;
public class GenerateCode {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://localhost:3307/springboot?serverTimezone=UTC", "root", "123456")
.globalConfig(builder -> {
builder.author("邹荣辉") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("D://data//"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.example.springboot2") // 设置父包名
.moduleName("class") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://data//")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("class") // 设置需要生成的表名
.addTablePrefix("t_", "c_"); // 设置过滤表前缀
})
// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
@RequestMapping("studentlist")
public String studentlist(Model model){
// 根据字段查询name=sd 条件构造器
// select * from student where name=sd
QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
// queryWrapper.eq("name","sd");
queryWrapper.like("name","d");
model.addAttribute("studentlist",studentService.list(queryWrapper));
// model.addAttribute("studentlist",studentService.list());
return "studentlist.html";
}
@RequestMapping("studentlist")
public String studentlist(Model model){
// 根据字段查询name=sd 条件构造器
// select * from student where name=sd
// QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.eq("name","sd");
// queryWrapper.like("name","d");
// model.addAttribute("studentlist",studentService.list(queryWrapper));
// model.addAttribute("studentlist",studentService.list());
// 列表加分页 每页显示n条 1页1->n条 2页n+1->2*n条
// SQL->select * from student limit 20 skip 20*x
Page<Student> page=new Page<>(1,5);//当前页码,每页条数
IPage<Student> iPage=studentService.getBaseMapper().selectPage(page,null);
model.addAttribute("studentlist",iPage.getRecords());
model.addAttribute("total",iPage.getTotal());
return "studentlist.html";
}
package com.example.springboot2.utils;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
1、有的页面 不登录也能访问 首页 登录 注册
2、有的页面 需要登录才访问 个人信息有关
3、有的页面需要登录并需要有一定权限才能访问 新建作业(管理员或教师)
技术实现->SpringSercurity 验证用户登录(认证) 验证用户权限(授权)
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
package com.example.springboot2.utils;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
public class MySercurityConfig extends WebSecurityConfigurerAdapter {
// 认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.inMemoryAuthentication()
.withUser("zrh")
.password(new BCryptPasswordEncoder().encode("123456"))
.authorities("user");
}
@Bean
PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}