使用软件:IDEA,Hbuilder,navcat
一.创建springboot项目
二.导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cjt</groupId>
<artifactId>Vue-changjiatao</artifactId>
<version>1.0-SNAPSHOT</version>
<!--SpringBoot 启 动 父 依 赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 启动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- JWT的依赖 -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- poi操作excel 开始-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
<!-- beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<!-- commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- poi操作excel 结束-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.23</version>
</dependency>
</dependencies>
</project>
三.由于我们需要分页功能,所以需要使用一个page工具类
PageUtil类
import lombok.Data;
/**
* 封装分页类
*
* @author cjt
*
*/
@Data
public class PageUtil {
// 这两个属性叫这个名字,是为了对应layui的laytable分页参数
private int page = 1;// 当前页数
private int limit = 10;// 每页显示多少条
private int offset;// 偏移量 从哪个下标开始,sql语句limit后的第一个数字
/*
* 其实不需要这样设置offset 1.前端layui自己管理分页 2.持久层mybatis plus自己处理偏移量 3.以上两者都只需要页码和条数就行了
*/
public void setOffset(int page) {
this.page = page;
this.offset = (this.page - 1) * this.limit;
}
}
四.添加配置
spring.datasource.url=jdbc:mysql://localhost:3306/vue?characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#新版名称已更新,请注意
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
## Mybatis 配置
# 实体所在包,给实体类起别名,xml里面的返回值和入参用到时直接写类名就行了
mybatis.typeAliasesPackage=com.cjt.domain
# 映射文件所在路径
mybatis.mapperLocations=classpath:mapper/*.xml
五.mybatis反向生成
CodeGenerator类
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {
public static void main(String[] args) {
// assert (false) : "代码生成属于危险操作,请确定配置后取消断言执行代码生成!";
AutoGenerator mpg = new AutoGenerator();
// 选择 freemarker 引擎,默认 Velocity
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setAuthor("cjt");
gc.setOpen(false);//不要打开生成后的目录
String projectPath = System.getProperty("user.dir");//获取项目根目录
gc.setOutputDir(projectPath + "/src/main/java");
gc.setFileOverride(false);// 是否覆盖同名文件,默认是false
gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
/* 自定义文件命名,注意 %s 会自动填充表实体属性! */
gc.setServiceName("%sService");//默认生成的service接口会以I开头
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
//dsc.setDbType(DbType.MYSQL);
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setUrl("jdbc:mysql://localhost:3306/vue?serverTimezone=GMT%2B8");//此处填写数据库名称(我的是vue)
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
//strategy.setTablePrefix(new String[]{""});// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略,下划线转驼峰
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//列名下划线转驼峰
strategy.setInclude(new String[]{"user"}); // 需要生成的表
// strategy.setExclude(new String[]{"test"}); // 排除生成的表
strategy.setEntityLombokModel(true);//实体类使用lombok
// 自定义实体父类
// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
// 自定义实体,公共字段
// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
// 自定义 mapper 父类
// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
// 自定义 service 父类
// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
// 自定义 service 实现类父类
// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
// 自定义 controller 父类
// strategy.setSuperControllerClass("com.baomidou.demo.TestController");
// 【实体】是否生成字段常量(默认 false)
// public static final String ID = "test_id";
// strategy.setEntityColumnConstant(true);
// 【实体】是否为构建者模型(默认 false)
// public User setName(String name) {this.name = name; return this;}
// strategy.setEntityBuilderModel(true);
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.cjt");//包名
// pc.setModuleName("test");
mpg.setPackageInfo(pc);
// 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// // 自定义 xxList.jsp 生成
List<FileOutConfig> focList = new ArrayList<>();
// focList.add(new FileOutConfig("/template/list.jsp.vm") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定义输入文件名称
// return "D://my_" + tableInfo.getEntityName() + ".jsp";
// }
// });
// cfg.setFileOutConfigList(focList);
// mpg.setCfg(cfg);
//
// 调整 xml 生成目录演示
// 自定义输出配置
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 关闭默认 xml 生成,调整生成 至 根目录
TemplateConfig tc = new TemplateConfig();
tc.setXml(null);
mpg.setTemplate(tc);
// 执行生成
mpg.execute();
}
}
六.Result结果返回类
这个功能已经在前面一篇文章讲过了,可以直接查看
https://blog.csdn.net/weixin_43709538/article/details/109321140
七.StudentController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* 前端控制器
*
*
* @author cjt
* @since 2020-10-26
*/
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/getStu")// 客户端请求地址
public Object getAllStudent(Student student, Page<Student> page) {//返 回值和方法名,object表示可以返回任何对象,因为object是任何对象的父对象,要父给子没问匙
IPage<Student> allStu = studentService.getStudent(page, student);
return Result.success(allStu);
}
}
八.StudentService类
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjt.domain.Student;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.ibatis.annotations.Param;
/**
*
* 服务类
*
*
* @author cjt
* @since 2020-10-26
*/
public interface StudentService extends IService<Student> {
IPage<Student> getAllStudent(@Param("page") Page<Student> page, @Param("student") Student student, @Param("uid") Integer uid, @Param("dataEntryClerk") String dataEntryClerk);
IPage<Student> getStudent(@Param("page") Page<Student> page, @Param("student") Student student);
}
九.StudentServiceImpl类
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjt.domain.Student;
import com.cjt.mapper.StudentMapper;
import com.cjt.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* 服务实现类
*
*
* @author cjt
* @since 2020-10-26
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public IPage<Student> getAllStudent(Page<Student> page, Student student, Integer uid, String dataEntryClerk) {
return studentMapper.getAllStudent(page,student,uid,dataEntryClerk);
}
@Override
public IPage<Student> getStudent(Page<Student> page, Student student) {
return studentMapper.getStudent(page,student);
}
}
十.StudentMapper类
IPage<Student> getAllStudent(@Param("page") Page<Student> page, @Param("student") Student student,@Param("uid") Integer uid,@Param("dataEntryClerk") String dataEntryClerk);
IPage<Student> getStudent(@Param("page") Page<Student> page, @Param("student") Student student);
十一.StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cjt.mapper.MembershipMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.cjt.domain.Membership">
<id column="m_id" property="mId" />
<result column="m_dep" property="mDep" />
<result column="m_spe" property="mSpe" />
<result column="m_status" property="mStatus" />
<result column="re_cord" property="reCord" />
</resultMap>
</mapper>
十二.ApplicationVue
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.cjt.mapper")
public class ApplicationVue {
public static void main(String[] args) {
SpringApplication.run(ApplicationVue.class, args);
}
}
启动:输入网址http://localhost:8080/student/getStu
此处的端口号是你设置的端口号,如有不同,请设置