Java开发个人常用(持续更新中....)

##Gitee地址

​​​​​​开发常用相关文档Gitee地址

1. 数据库导入导出

导出命令

mysqldump -h IP地址 -P 端口 -u 用户名 -p 数据库名 > 导出的文件名

示例:

mysqldump -h localhost -P 3306 -u root -p tkl_diamond > C:\Users\DELL\Desktop\tkl-diamond.sql

导出后 添加

create database if not exists 数据库名; use 数据库名;

导入命令

登录mysql 或者idea run执行

source 文件.sql

示例:

source C:\Users\DELL\Desktop\tkl-diamond.sql

2. MAVEN

目录

配置文件(settings)

/开发常用/配置文件/maven/settings.xml

安装包(3.6.3版本)

/开发常用/安装包/apache-maven-3.6.3.zip

操作文档

/开发常用/操作文档/maven_git配置(2023-4-7).pdf

setting文件

配置项

55 行 localRepository

用于配置本地依赖库地址

配置文件中为:

<localRepository>C:\greensoft\repository</localRepository>

160行 mirror

用于配置镜像地址

配置文件中为:

  <mirror>
  <id>aliyun</id>
  <mirrorOf>*</mirrorOf>
  <name>aliyun Maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

228行 profile

用于配置JDK版本

<profile>
  <id>jdk-1.8</id>
  <activation>
    <activeByDefault>true</activeByDefault>
    <jdk>1.8</jdk>
  </activation>
  <properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  </properties>
</profile>

3. LomBok

添加依赖

<!--引入Lombok依赖--> 
<dependency> 
    <groupId>org.projectlombok</groupId> 
    <artifactId>lombok</artifactId> 
</dependency>

注解简单描述

@Data 注解
包含 @Getter  生成get方法
    @Setter  生成set方法
    @RequiredArgsConstructor  生成必须参数的构造方法
    @ToString   生成toString方法
    @EqualsAndHashCode  生成equals 和 hashcode 方法
​
@AllArgsConstructor
    生成全参构造方法
​
@NoArgsConstructor
    生成无参构造方法
​
@Accessors(chain = true)
    链式调用,使用set方法后返回自身对象,链式调用set方法
    

4. Knife4j

目录

配置文件

/开发常用/配置文件/knife4j/Knife4jConfig.java

操作文档

/开发常用/操作文档/Knife4j.pdf

配置类

添加依赖

<!-- 
    knife4j-spring-boot-starter 是一个开源的Swagger工具,它提供了方便的Swagger UI界面,对于开发人员来说可以通过Swagger UI方便的测试API接口。而knife4j-openapi2-spring-boot-starter则是在knife4j-spring-boot-starter基础上扩展了OpenAPI 2.0规范的支持,它支持更多的数据类型,更灵活的API定义和更好的安全性能。所以,knife4j-openapi2-spring-boot-starter具有更加丰富的功能和更好的灵活性。
--!>
​
​
<!--添加Knife4j依赖-->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
    <version>4.1.0</version>
</dependency>
​
​
<!--添加Knife4j依赖-->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.9</version>
</dependency>

使用描述

@Api 文档类注解
    // tags 指定文档标签名称(在tags前面指定数字会自动排序)
    @Api(tags = "02.微博模块")
    
@ApiOperation 方法类注解
    // value 指定方法的标签名
    @ApiOperation(value = "发布微博功能")
​
// 给方法添加排序注解, 建议按 增 100 |删 200 | 改 300 | 查 400
@ApiOperationSupport(order = 100) 
​
    
@ApiModelProperty POJO类注解
    // value 属性名称
    // required 是否可为空
    // example 示例
    // dataType 数据类型
    // hidden 是否隐藏,隐藏则不会出现在文档中
    @ApiModelProperty(value = "用户名",required = true,example = "tony") 
    
@ApiImplicitParam 参数注解
    // 示例
    @ApiImplicitParams({
        @ApiImplicitParam(name="id", required = true, dataType = "long"),
    })
    
    
注: 1.使用RequestMapping会生成全部类型的请求
    2.required 只是显示,不做限制

5. 全局统一异常处理

目录

/开发常用/配置文件/统一异常处理/GlobalExceptionHandler.java

说明

//目前已捕获异常,待补充  包名 exception
    // 运行时异常
    RuntimeException.class
    // 违反数据库异常
    ConstraintViolationException.class
    // 参数列表异常(Spring Validation)
    MethodArgumentNotValidException.class
    // 用户名或密码错误(Spring Security)
    InternalAuthenticationServiceException.class //返回null值抛出的异常
    BadCredentialsException.class // 密码错误抛出的异常
    // 自定义业务异常
    ServiceException
    // 请求参数格式错误
    BindException

6. 全局统计返回结果封装

目录

/开发常用/配置文件/统一结果封装

说明

目前使用: JsonResult  \  ServiceCode
// 封装结果类   包名response
    JsonResult
        state 状态码
        message 状态信息
        data 数据对象
添加注解 @JsonInclude(JsonInclude.Include.NON_NULL)
可以将响应的Json结果中的空值不返回
    
        
// 目前返回结果枚举类,待补充
    /**
     * 操作成功
     */
    OK(20000),
    /**
     * 错误:请求参数格式错误
     */
    ERROR_BAD_REQUEST(40000),
    /**
     * 错误:数据不存在
     */
    ERROR_NOT_FOUND(40400),
    /**
     * 错误:数据冲突
     */
    ERROR_CONFLICT(40900),
    /**
     * 错误:其它异常
     */
    ERROR_UNKNOWN(99999);

7. SpringBoot

目录

/开发常用/配置文件/SpringBoot

说明

​
# 配置数据库链接
spring.datasource.url=jdbc:mysql://localhost:3306/baking?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
# 配置数据库用户
spring.datasource.username=root
# 配置数据库密码
spring.datasource.password=root
# 配置mapper扫描路径
mybatis.mapper-locations=classpath:mappers/*.xml
# 开启日志级别
logging.level.cn.tedu=debug
​
# 配置表字段名和属性名命名规范不一致自动匹配(规则可能是去除下划线,然后下划线后字母大写)
mybatis.configuration.map-underscore-to-camel-case=true
​
# 设置单个上传文件的大小 默认1M
spring.servlet.multipart.max-file-size=100MB
​
# 设置多个文件上传总大小
spring.servlet.multipart.max-request-size=200MB
​
# 客户端只能访问静态资源文件夹中的文件,设置某个文件夹为静态资源文件夹(file:本地路径  classpath: 指定路径)
spring.web.resources.static-locations=file:d:/files,classpath:static

常用注解

1.SpringBootTest
    // 测试类
2.SpringBootApplication
    // 启动类
3.Value (不知道归属哪个框架,先写到这,可以使用配置参数)
    @Value("${tea-store.dao.default-query-page-size}")

8. MyBatis&MyBatisPlus

目录

/开发常用/配置文件/mabatis

依赖

<!-- MyBatis Plus整合Spring Boot的依赖项 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.3.1</version>
</dependency>
​
​
<!-- MyBatis Plus整合Spring Boot的依赖项(包含MyBatis) -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.0</version>
</dependency>

说明

MyBatis
// MyBatisConfig
    // 配置mapper扫描路径,@MapperScan 注解修改
​
// Mapper.xml
    // 修改Mapper  namespace 指定mapper路径
    
MyBatisPlus
    

其他相关

分页工具(PageHelper)
文件路径

开发常用\配置文件\mybatis\分页(PageHelper)

依赖
<!-- PageHelper:专用于MyBatis的分页框架 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.3.0</version>
</dependency>
使用方式
List<TagListItemVO> typeList = mapper.list();
​
PageInfo<TagListItemVO> pageInfo = new PageInfo<>(typeList);
PageData<TagListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
日期自动更新处理
配置时间
// 创建时间字段 字段名: gmtCreate
public static final String FIELD_CREATE_TIME = "gmtCreate";
// 修改时间字段 字段名: gmtModified
public static final String FIELD_UPDATE_TIME = "gmtModified";
​
// 在实体类 中添加注解
​
    /**
     * 数据创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime gmtCreate;
​
    /**
     * 数据最后修改时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime gmtModified;
​
​

9. SpringSecurity

目录

/开发常用/配置文件/SpringSecurity

注:不同的SpringBoot版本,SpringSecurity方法可能不同,

2.5使用 SecurityConfiguration-2.5.java

2.7使用 SecurityConfig-2.7.java

说明

// 使用方式
    // SecurityConfig  配置密码加密,黑白名单,登录页面
    // UserDetailServiceImpl 配置用户登录及权限
    // CustomUserDetails 添加所需的字段,Security默认只有用户名和密码
// 注:1.配置加密,注册时的用户密码也需要加密,可以通过passwordEncoder.encode进行加密
//    2.配置权限, 在SecurityConfig 中添加注解,开启授权方法检测 
//      @EnableGlobalMethodSecurity(prePostEnabled = true)//开启方法授权的检测
//      在方法上添加注解 ,进行配置权限
//      @PreAuthorize("hasAnyAuthority('ADMIN')")
//Controller
        //通过认证管理器启动Security的认证流程  返回认证结果对象
        Authentication result = manager.authenticate(new UsernamePasswordAuthenticationToken(
                userLoginDTO.getUserName(), userLoginDTO.getPassword()));
        //将认证结果保存到Security上下文中   让Security框架记住登录状态
        SecurityContextHolder.getContext().setAuthentication(result);
        //代码执行到这里时代表登录成功!如果登录失败Security框架会抛出异常
​

10.字面量(待补充)

11.常用函数及注解

(1 日期转换注解

@JsonFormat(pattern="yyyy年MM月dd日 HH时mm分ss秒",timezone="GMT+8")
 // pattern 需要转换的格式
 // timezone 时区   国内是 GMT+8  东八区
​

12.常用HTML引用

1.npm相关

1)npm相关命令
// 查看版本号
npm -v
// 配置npm源
npm config set registry https://registry.npmmirror.com
// 查看npm源
npm config get registry
// 安装vue
npm install -g @vue/cli
​
// 查看vue版本
vue -V
// 创建vue工程
vue create 工程名称
// 启动vue工程
npm run serve

2).npm 引入elementUI

elementUI官网 Element - The world's most popular Vue UI framework

(1) 安装elementUI

npm i element-ui -S

(2)main.js引入
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
3)安装axios
(1) 安装axios

npm i axios -S

(2)main.js引入
import axios from 'axios';
Vue.prototype.axios=axios;

4) 安装qs
(1) 安装qs

npm i qs -S

(2) main.js引入
import qs from 'qs';
Vue.prototype.qs=qs;

2.script

​
<!-- import Vue before Element 导入vue -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
​
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
​
            }
        },
        methods: {
​
        }
    })
</script>
​
<!-- import JavaScript 导入ElementUI -->
<!-- CSS样式 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
<!-- JS -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    
    
<!--引入Axios  异步请求-->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    
 <!-- 引入富文本编辑器 -->
<script src="https://cdn.staticfile.org/wangeditor5/5.1.23/index.min.js"></script>
<script>
    /**********富文本编辑器相关js开始***********/
    const { createEditor, createToolbar } = window.wangEditor
    const editorConfig = {
        placeholder: '请填写详情...',
        onChange(editor) {
            const html = editor.getHtml()
            console.log('editor content', html)
            v.c.content = html;
        }
    }
    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'simple',
    })
    const toolbarConfig = {}
    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'simple',
    })
    /**********富文本编辑器相关js结束***********/
</script>

13. SpringValidation

1.依赖

<!-- Spring Validation -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
配置文件路径:

开发常用\配置文件\validation

2.使用方法

(1). pojo类
1. pojo类中属性上添加校验注解
示例: 
    @NotNull(message = "标签名称不能为空")
    private String name;
2. Controller pojo类参数前添加 @Validated
示例: 
 public JsonResult test(@Validated TagAddNewParam tagAddNewParam) {
        return JsonResult.ok();
    }

(2). 参数列表
1. Controller 类上添加@Validated注解
    @Validated
    public class TestController {}
2. 参数前添加校验注解
示例: 
    public JsonResult test(@Range(min = 1,message = "删除标签失败,请提交合法的ID值") Long id){
        return JsonResult.ok();
    }

3. 常用注解

1. Range 指定最小值和最大值, 内部使用了@Min ,@Max 标签
    @Range(min = 1, max = 99, message = "删除标签失败,请提交合法的ID值")
2. NotNull 不能为空,(空字符串不为空.NotBlank,NotEmpty,可以排除空字符串)
    @NotNull(message = "是否启用不能为空")
3. Pattern 使用正则表达式判断,写正则表达式可以使用CharGPT
    @Pattern(regexp = "^[a-zA-Z\\u4e00-\\u9fa5]{2,10}$" , message = "标签类别必须是2~10长度的字符组成,且不允许使用标点符号")
​

14. SpringMVC 架构相关

1. 各个分层模板(MyBatisPlus)

(1). Mapper
​
/**
 * 处理分类数据的Mapper接口
 *
 * @author zhr@tkl.cn
 * @version 1.0
 */
@Repository
public interface TestMapper extends BaseMapper<Category> {
​
    // 1. 增加相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    // 2. 删除相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 3. 修改相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
    
    // 4. 查询相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // 4.1 单个对象查询
        /**
     * 根据ID查询分类
     *
     * @param id 分类ID
     * @return 匹配的分类,如果没有匹配的数据,则返回null
     */
        TestVO Test(Long id);
    // 4.2 集合查询
        
}

(2). Repository
/**
 * 处理标签数据的业务接口
 *
 * @author zhr@tkl.cn
 * @version 1.0
 */
public interface TestRepository {
​
    // 1. 增加相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /**
     * 插入分类数据
     *
     * @param category 分类数据
     * @return 受影响的行数
     */
    int Test(Category category);
​
​
    // 2. 删除相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 3. 修改相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 4. 查询相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // 4.1 单个对象查询
​
​
    // 4.2 集合查询
​
​
}
(3). Repository 实现类
​
@Repository
@Slf4j
public class TestRepositoryImpl implements TestRepository {
​
    @Autowired
    private TestMapper mapper;
    public TestRepositoryImpl() {
        log.info("创建存储库对象:TestRepositoryImpl");
    }
​
​
    // 1. 增加相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    @Override
    public int insert(Category category) {
        log.debug("开始执行【向分类表中插入数据】,参数:{}", category);
        return mapper.insert(category);
    }
​
​
    // 2. 删除相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 3. 修改相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 4. 查询相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // 4.1 单个对象查询
​
    // 4.2 集合查询
​
}
(4). Service
/**
 * @author zhr@tkl.cn
 * @version 1.0
 */
@Transactional
public interface TestService {
    
    // 1. 增加相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /**
     * 新增分类类型
     *
     * @param categoryTypeAddNewParam 新增的分类类型数据
     */
    void addNew(CategoryTypeAddNewParam categoryTypeAddNewParam);
​
​
    // 2. 删除相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 3. 修改相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 4. 查询相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // 4.1 单个对象查询
​
    // 4.2 集合查询
​
}

(5). Service 实现类
/**
 * 处理分类数据的业务实现类
 *
 * @author zhr@tkl.cn
 * @version 1.0
 */
@Slf4j
@Service
public class TestServiceImpl implements TestService {
​
    @Autowired
    private TestRepository repository;
​
    public TestServiceImpl() {
        log.info("创建业务对象:TestServiceImpl");
    }
​
    // 1. 增加相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 2. 删除相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 3. 修改相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 4. 查询相关逻辑~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // 4.1 单个对象查询
​
    // 4.2 集合查询
​
    @Override
    public PageData<CategoryTypeListItemVO> listCategoryType(Integer pageNum) {
        log.debug("开始处理【查询分类类别列表】业务,页码:{},每页记录数:{}", pageNum);
        PageData<CategoryTypeListItemVO> pageData = repository.listCategoryType(pageNum, defaultQueryPageSize);
        return pageData;
    }
}

(6). Controller
/**
 * 处理分类相关请求的控制器类
 *
 * @author zhr@tkl.cn
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/content/category")
@Api(tags = "1.1. 内容管理-分类管理")
@Validated
public class CategoryController {
​
    @Autowired
    private ICategoryService service;
​
    public CategoryController() {
        log.info("创建控制器对象:CategoryController");
    }
​
    // 添加@RequestBody:接收对象参数
    // 不添加@RequestBody:接收FormData参数
    // 选择自己的习惯使用
    // 增 100 单条增加/100,多条增加/110~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    @ApiOperation("新增分类类别")
    @ApiOperationSupport(order = 100)
    @PostMapping("/type/add-new")
    public JsonResult addNew(@RequestBody @Validated CategoryTypeAddNewParam categoryTypeAddNewParam) {
        log.debug("开始处理【新增标签类别】的请求,参数:{}", categoryTypeAddNewParam);
        service.addNew(categoryTypeAddNewParam);
        return JsonResult.ok();
    }
    // 删 200 单条删除/200,多条删除/210~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
    // 改 300 单条修改/300,多条修改/310~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
​
​
    // 查 400 单条查询/400,多条查询/410~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
​
}

15. 工具类

1. Json 对象转换为 字符串

(1). fastjson
添加依赖
<!-- fastjson:实现对象与JSON的相互转换 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.75</version>
</dependency>
使用方法
JsonResult jsonResult = JsonResult.fail(ServiceCode.ERROR_UNAUTHORIZED, message);
JSON.toJSONString(jsonResult);

XX.问题模板

问题背景及描述 :

问题原因 :

解决方法 :

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值