新建项目
版本控制
.gitignore
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*/.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
常用的规则
/mtk/ 过滤整个文件夹
*.zip 过滤所有.zip文件
/mtk/do.c 过滤某个具体文件
以上规则意思是:被过滤掉的文件就不会出现在你的GitHub库中了,当然本地库中还有,只是push的时候不会上传。
除了以上规则,它还可以指定要将哪些文件添加到版本管理中。
!src/ 不过滤该文件夹
!*.zip 不过滤所有.zip文件
!/mtk/do.c 不过滤该文件
配置语法:
- 以斜杠/开头表示目录;
- 以星号*通配多个字符;
- 以问号?通配单个字符
- 以方括号[]包含单个字符的匹配列表;
- 以叹号!表示不忽略(跟踪)匹配到的文件或目录;
- 此外,git 对于 .ignore 配置文件是按行从上到下进行规则匹配的,意味着如果前面的规则匹配的范围更大,则后面的规则将不会生效;
示例说明
- List item
规则:fd1/*
说明:忽略目录 fd1 下的全部内容;注意,不管是根目录下的 /fd1/ 目录,还是某个子目录 /child/fd1/ 目录,都会被忽略;
- List item
规则:/fd1/*
说明:忽略根目录下的 /fd1/ 目录的全部内容;
- 规则:
/*
!.gitignore
!/fw/bin/
!/fw/sf/
说明:忽略全部内容,但是不忽略 .gitignore 文件、根目录下的 /fw/bin/ 和 /fw/sf/ 目录;
创建.gitignore文件
常规的windows操作
根目录下创建gitignore.txt;
编辑gitignore.txt,写下你的规则,例如加上node_modules/;
打开命令行窗口,切换到根目录(可以直接在文件夹上面的地址栏输入cmd回车);
直接新建.gitignore也可以
执行命令ren gitignore.txt .gitignore。
用Git Bash
根目录下右键选择“Git Bash Here”进入bash命令窗口;
输入vim .gitignore或touch .gitignore命令,打开文件(没有文件会自动创建);
按i键切换到编辑状态,输入规则,例如node_modules/,然后按Esc键退出编辑,输入:wq保存退出。
# dependencies npm包文件
/node_modules
# production 打包文件
/build
# misc
.DS_Store
npm-debug.log*
最后需要强调的一点是,如果你不慎在创建.gitignore文件之前就push了项目,那么即使你在.gitignore文件中写入新的过滤规则,这些规则也不会起作用,Git仍然会对所有文件进行版本管理。
简单来说,出现这种问题的原因就是Git已经开始管理这些文件了,所以你无法再通过过滤规则过滤它们。因此一定要养成在项目开始就创建.gitignore文件的习惯,否则一旦push,处理起来会非常麻烦。
git回退 reset
## 查看提交日志
git reflog
## 回退到前1次提交之前,以此类推,回退到n次提交之前,1就是上次,用git reflog
## 更方便查看
git reset --hard HEAD~1
## 退到/进到 指定commit的sha码
git reset --hard commit_id 退到/进到 指定commit的sha码(使用git reflog看sha)
reset三种模式
–hard 直接变到那个提交,其他都不要 (尽量别用)
–soft 变成指定的那个提交,但是中途 未提交/提交 的修改都还在(提交过的在暂存区)
–mixed 变成指定的那个提交,但是中途 未提交/提交 的修改都还在(提交过的在工作区)
依赖管理
不使用parent POM,自己配置Spring Boot
补充:
引入spring-boot-starter-parent后就不用引入spring-boot-dependencies了,因为spring-boot-starter-parent的里面就包含了spring-boot-dependencies
如果你不想使用spring-boot-starter-parent,你依然可以通过使用scope=import利用依赖管理的便利:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
这种方式不能使用property的形式覆盖原始的依赖项。要达到同样的效果,需要在dependencyManagement里面的spring-boot-dependencies之前添加依赖的东西。
例如,要升级Spring Data releasetrain,pom.xml应该是这样的:
可以看到dependencyManagement里面已经包含Spring Data release train
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
代码
主pom
<?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.hj</groupId>
<artifactId>base</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>base-core</module>
</modules>
<!--统一管理jar包和版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.20</lombok.version>
<mysql.version>8.0.18</mysql.version>
<druid.verison>1.1.16</druid.verison>
<mybatis.spring.boot.verison>1.3.0</mybatis.spring.boot.verison>
</properties>
<!--子模块继承之后,提供作用:锁定版本+子module不用写groupId和version-->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
新建模块base-core
<?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">
<parent>
<artifactId>base</artifactId>
<groupId>com.hj</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.hj</groupId>
<artifactId>base-core</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Mysql
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
password: root
url: jdbc:mysql://localhost:3306/test
username: root
连接池
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/base?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
initial-size: 8
max-active: 16
min-idle: 8
max-wait: 60000
test-while-idle: true
test-on-borrow: false
test-on-return: false
Redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
database: 0
host: x.x.x.x
port: 6379
password:
jedis:
pool:
max-active: 1000
max-wait: -1ms
max-idle: 16
min-idle: 8
测试
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* class: 测试controller
*
* @author: HJ
* @date: 2021/6/5 18:12
* @description: 测试controller
* @version: v1.0
*/
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* @author: HJ
* @date: 2021/6/5 20:47
* status: COMPLETE
* @description: 测试Redis连接
* @version: v1.0
*/
@GetMapping("/redis")
public String redis(){
ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
// 缓存数据
opsForValue.set("name", "lisi");
log.info("写入redis");
// 获取缓存数据
return opsForValue.get("name");
}
}