提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:以下是本篇文章正文内容,下面案例可供参考
一、项目搭建
- 添加如下依赖
<!-- 引入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 staticvoidmain(String[] args){
SpringApplication.run(DemobootApplication.class, args);}}
2.配置mybtis-publs分页
代码如下(示例):
@Configuration
public class MyBaitsPlusConfig {//配置分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//数据库类型是MySql,因此参数填写DbType.MYSQL
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}}
3.配置Result返回类
packagecom.example.demoboot.config;importjava.io.Serializable;publicclassResult<T>implementsSerializable{privatestaticfinallong serialVersionUID =1L;privateboolean success =true;// 成功标志privateboolean crypto =false;// 是否响应结果解密/ SM4/CBCprivateString message ="";// 返回处理消息privateInteger code =0;// 返回代码privateT result;// 返回数据对象privatelong timestamp =System.currentTimeMillis();// 时间戳// 默认构造函数publicResult(){}// 带参数的构造函数publicResult(Integer code,String message){this.code = code;this.message = message;}// 设置成功结果publicResult<T>success(String message){this.message = message;this.code =200;this.success =true;returnthis;}// 创建成功结果publicstatic<T>Result<T>OK(){returnnewResult<>(200,"操作成功");}// 创建成功结果,带数据publicstatic<T>Result<T>OK(T data){Result<T> result =OK();
result.setResult(data);return result;}// 创建成功结果,带消息和数据publicstatic<T>Result<T>OK(String msg,T data){Result<T> result =OK();
result.setMessage(msg);
result.setResult(data);return result;}// 创建错误结果,带消息和数据publicstatic<T>Result<T>error(String msg,T data){returnerror(500, msg, data);}privatestatic<T>Result<T>error(int i,String msg,T data){Result<T> result =error(i, msg);
result.setResult(data);return result;}// 创建错误结果,带消息publicstatic<T>Result<T>error(String msg){returnerror(500, msg);}// 创建错误结果,带状态码和消息publicstatic<T>Result<T>error(int code,String msg){Result<T> result =newResult<>();
result.setCode(code);
result.setMessage(msg);
result.setSuccess(false);return result;}// 设置 500 错误publicResult<T>error500(String message){returnerror(500, message);}// 设置未授权结果publicstatic<T>Result<T>noAuth(String msg){returnerror(510, msg);}// Getter 和 Setter 方法publicbooleanisSuccess(){return success;}publicbooleanisCrypto(){return crypto;}publicStringgetMessage(){return message;}publicIntegergetCode(){return code;}publicTgetResult(){return result;}publiclonggetTimestamp(){return timestamp;}publicvoidsetSuccess(boolean success){this.success = success;}publicvoidsetCrypto(boolean crypto){this.crypto = crypto;}publicvoidsetMessage(String message){this.message = message;}publicvoidsetCode(Integer code){this.code = code;}publicvoidsetResult(T result){this.result = result;}publicvoidsetTimestamp(long timestamp){this.timestamp = timestamp;}// equals 和 hashCode 方法@Overridepublicbooleanequals(Object o){if(this== o)returntrue;if(!(o instanceofResult))returnfalse;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);}@OverridepublicinthashCode(){return31*(success ?1:0)+(crypto ?1:0)+
code.hashCode()+
message.hashCode()+
result.hashCode()+Long.hashCode(timestamp);}@OverridepublicStringtoString(){return"Result{"+"success="+ success +", crypto="+ crypto +", message='"+ message +'\''+", code="+ code +", result="+ result +", timestamp="+ timestamp +'}';}}
4.新建 WebConstant 添加接口访问
packagecom.example.demoboot.config;publicclassWebConstant{publicstaticfinalString 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")publicclass moduleController {}
- moduleService
publicinterface moduleService extendsIService<module>{}
- moduleServiceImpl
@Servicepublicclass moduleServiceImpl extendsServiceImpl<moduleMapper,module>implements moduleService {}
- moduleMapper
@Mapperpublicinterface moduleMapper extendsBaseMapper<module>{}
五、 利用mybtis-publs 生成插件
- 连接数据库
- 代码生成
- 自行配置
- 生成之后自行修改不足之处即可
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
本文转载自: https://blog.csdn.net/weixin_44538590/article/details/143364600
版权归原作者 小扎仙森 所有, 如有侵权,请联系我们删除。
版权归原作者 小扎仙森 所有, 如有侵权,请联系我们删除。