提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、项目搭建
- 二、 配置文件
- 1.配置启动类映射mapper
- 2.配置mybtis-publs分页
- 3.配置Result返回类
- 4.新建 WebConstant 添加接口访问
- 5.配置yml
- 三、 实现mybtis-publs增删改查
- 四、 层级关联说明
- 五、 利用mybtis-publs 生成插件
- 总结
前言
提示:以下是本篇文章正文内容,下面案例可供参考
一、项目搭建
- 添加如下依赖
<!-- 引入mybatisPlus 包含了 jdbc --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><!-- mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- 引入durid數據源--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.18</version></dependency>
二、 配置文件
1.配置启动类映射mapper
代码如下(示例):
@SpringBootApplication
@MapperScan("com.example.demoboot.*.mapper")
public class DemobootApplication {public static void main(String[] args) {SpringApplication.run(DemobootApplication.class, args);}}
2.配置mybtis-publs分页
代码如下(示例):
@Configuration
public class MyBaitsPlusConfig {//配置分页插件@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//数据库类型是MySql,因此参数填写DbType.MYSQLinterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}
3.配置Result返回类
package com.example.demoboot.config;import java.io.Serializable;public class Result<T> implements Serializable {private static final long serialVersionUID = 1L;private boolean success = true; // 成功标志private boolean crypto = false; // 是否响应结果解密/ SM4/CBCprivate String message = ""; // 返回处理消息private Integer code = 0; // 返回代码private T result; // 返回数据对象private long timestamp = System.currentTimeMillis(); // 时间戳// 默认构造函数public Result() {}// 带参数的构造函数public Result(Integer code, String message) {this.code = code;this.message = message;}// 设置成功结果public Result<T> success(String message) {this.message = message;this.code = 200;this.success = true;return this;}// 创建成功结果public static <T> Result<T> OK() {return new Result<>(200, "操作成功");}// 创建成功结果,带数据public static <T> Result<T> OK(T data) {Result<T> result = OK();result.setResult(data);return result;}// 创建成功结果,带消息和数据public static <T> Result<T> OK(String msg, T data) {Result<T> result = OK();result.setMessage(msg);result.setResult(data);return result;}// 创建错误结果,带消息和数据public static <T> Result<T> error(String msg, T data) {return error(500, msg, data);}private static <T> Result<T> error(int i, String msg, T data) {Result<T> result = error(i, msg);result.setResult(data);return result;}// 创建错误结果,带消息public static <T> Result<T> error(String msg) {return error(500, msg);}// 创建错误结果,带状态码和消息public static <T> Result<T> error(int code, String msg) {Result<T> result = new Result<>();result.setCode(code);result.setMessage(msg);result.setSuccess(false);return result;}// 设置 500 错误public Result<T> error500(String message) {return error(500, message);}// 设置未授权结果public static <T> Result<T> noAuth(String msg) {return error(510, msg);}// Getter 和 Setter 方法public boolean isSuccess() { return success; }public boolean isCrypto() { return crypto; }public String getMessage() { return message; }public Integer getCode() { return code; }public T getResult() { return result; }public long getTimestamp() { return timestamp; }public void setSuccess(boolean success) { this.success = success; }public void setCrypto(boolean crypto) { this.crypto = crypto; }public void setMessage(String message) { this.message = message; }public void setCode(Integer code) { this.code = code; }public void setResult(T result) { this.result = result; }public void setTimestamp(long timestamp) { this.timestamp = timestamp; }// equals 和 hashCode 方法@Overridepublic boolean equals(Object o) {if (this == o) return true;if (!(o instanceof Result)) return false;Result<?> other = (Result<?>) o;return success == other.success && crypto == other.crypto && timestamp == other.timestamp &&code.equals(other.code) && message.equals(other.message) && result.equals(other.result);}@Overridepublic int hashCode() {return 31 * (success ? 1 : 0) +(crypto ? 1 : 0) +code.hashCode() +message.hashCode() +result.hashCode() +Long.hashCode(timestamp);}@Overridepublic String toString() {return "Result{" +"success=" + success +", crypto=" + crypto +", message='" + message + '\'' +", code=" + code +", result=" + result +", timestamp=" + timestamp +'}';}
}
4.新建 WebConstant 添加接口访问
package com.example.demoboot.config;public class WebConstant {public static final String API_PATH = "/api"; // 基础 API 路径
}
5.配置yml
spring:thymeleaf:cache: true # 开启模板缓存(默认值:true)check-template: true # 检查模板是否存在并解析check-template-location: true # 检查模板位置是否正确(默认值:true)content-type: text/html # Content-Type 的值(默认值:text/html)enabled: true # 开启 MVC Thymeleaf 视图解析(默认值:true)encoding: UTF-8 # 模板编码excluded-view-names: "" # 被排除在解析之外的视图名称列表mode: HTML5 # 模板模式(默认值:HTML5)prefix: classpath:/templates/ # 视图名称前缀(默认值:classpath:/templates/)suffix: .html # 视图名称后缀(默认值:.html)datasource:type: com.alibaba.druid.pool.DruidDataSource # 数据源类型username: root # 数据库用户名password: 111111 # 数据库密码driver-class-name: com.mysql.cj.jdbc.Driver # 驱动类名url: jdbc:mysql://localhost:3306/ceshi # 数据库连接URL,mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 日志实现map-underscore-to-camel-case: true # 开启驼峰命名
server:port: 8033 # 设置服务器启动端口为8033
三、 实现mybtis-publs增删改查
四、 层级关联说明
- moduleController
@RestController
@RequestMapping(value = WebConstant.API_PATH + "/modou")
public class moduleController {}
- moduleService
public interface moduleService extends IService<module> {}
- moduleServiceImpl
@Service
public class moduleServiceImpl extends ServiceImpl<moduleMapper, module> implements moduleService {}
- moduleMapper
@Mapper
public interface moduleMapper extends BaseMapper<module> {}
五、 利用mybtis-publs 生成插件
-
连接数据库
-
代码生成
-
自行配置
- 生成之后自行修改不足之处即可
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。