mirror of
https://gitee.com/coder-xiaomo/java-note
synced 2025-01-26 03:00:28 +08:00
2153 lines
52 KiB
Markdown
2153 lines
52 KiB
Markdown
# Java
|
||
|
||
## Java技术体系
|
||
|
||
```
|
||
1. Java基础:
|
||
变量 选择结构(if) 循环结构(for) 数组
|
||
============================================================================
|
||
2. Java面向对象
|
||
类 方法 属性 对象
|
||
创建类的语法:
|
||
访问修饰符(public/private) class 类名{
|
||
// 类名首字母大写
|
||
}
|
||
创建方法的语法:
|
||
访问修饰符 返回值类型(void/基本数据类型/对象/集合) 方法名称(有参/无参){
|
||
|
||
}
|
||
类中包含的内容: a.属性 b.方法
|
||
对象创建的语法: 类名 对象名=new 类名();
|
||
字符串的常用方法
|
||
============================================================================
|
||
3. Java高级部分
|
||
Java面向对象的三大特性:
|
||
a. 封装: (1).属性私有化 (2).生成get和set方法
|
||
b. 继承: Java支持单根继承 extends 好处: 父类有的,子类完全可以使用
|
||
c. 多态: 子类指向父类对象
|
||
d. 接口: interface 接口用法: (1)接口中所有的方法都是抽象方法 (2) 方法没有方法体 (3)方法重写(implements)
|
||
e. IO流: InputStream OutputStream
|
||
f. 线程:
|
||
h. 集合: List集合 set集合 Map集合 size()
|
||
泛型集合: 存储有相同类型的数据
|
||
==============================Java控制台项目================================
|
||
4. Javaweb
|
||
a. JDBC:java连接数据库的技术
|
||
b. Servlet: 后端与前端进行数据交互和页面传递 ? servlet中跳转页面(方法:转发 重定向)
|
||
c. JSP: 动态网页(html代码+java代码) ? <%%>
|
||
d. HTML5:静态网页
|
||
e. css: 页面样式
|
||
f. javaScript:
|
||
g. JavaQuery: 封装js
|
||
h. layui: 前端框架 vue.js elementUI
|
||
i. 数据库:Sqlserver mysql oracle 关系型数据库 非关系型数据库(redis)
|
||
===============================Javaweb项目(前端+后端)======================
|
||
5. 企业级框架SSM
|
||
MyBatis框架: 数据访问(操作数据库)====JDBC角色
|
||
Spring框架: 胶水框架(主要跟其他框架组合) Spring全家桶(Springmvc springboot springcloud)
|
||
SpringMVC框架: web框架 =============Servlet角色
|
||
Thymeleaf:模板引擎
|
||
=============================Javaweb企业级项目(前端+后端)=====================
|
||
6.前后端分离
|
||
a.springboot + vue
|
||
7.微服务:
|
||
springcloud+ES+Redis =====架构师
|
||
```
|
||
|
||
|
||
|
||
### Java基础、高级(控制台项目)
|
||
|
||
#### 新建类
|
||
|
||
![image-20220116152649260](张博凯的Java学习笔记.assets/image-20220116152649260.png)
|
||
|
||
(可以和包一起建)
|
||
|
||
`org.example.entity.User` 或 `org.example.pojo.User`
|
||
|
||
> 类名首字母大写;包名首字母小写
|
||
|
||
![image-20220116153103336](张博凯的Java学习笔记.assets/image-20220116153103336.png)
|
||
|
||
|
||
|
||
##### 创建main方法
|
||
|
||
`main + <回车>`
|
||
|
||
![image-20220116153728996](张博凯的Java学习笔记.assets/image-20220116153728996.png)
|
||
|
||
![image-20220116153844861](张博凯的Java学习笔记.assets/image-20220116153844861.png)
|
||
|
||
|
||
|
||
#### 新建接口
|
||
|
||
![image-20220116153417709](张博凯的Java学习笔记.assets/image-20220116153417709.png)
|
||
|
||
> 接口没有方法体,不可以new对象
|
||
|
||
|
||
|
||
#### 实现接口
|
||
|
||
UserDao
|
||
|
||
![image-20220116154126155](张博凯的Java学习笔记.assets/image-20220116154126155.png)
|
||
|
||
![image-20220116154238246](张博凯的Java学习笔记.assets/image-20220116154238246.png)
|
||
|
||
![image-20220116154415712](张博凯的Java学习笔记.assets/image-20220116154415712.png)
|
||
|
||
![image-20220116154448653](张博凯的Java学习笔记.assets/image-20220116154448653.png)
|
||
|
||
|
||
|
||
#### 集合与数组
|
||
|
||
```java
|
||
/*
|
||
* 数组:存储相同数据类型的一组数据
|
||
* 集合:可以存储不同数据类型的数据
|
||
* 泛型集合:存储有相同类型的数据
|
||
*/
|
||
|
||
// 集合
|
||
List list = new List();
|
||
list.add("aa");
|
||
list.add(1);
|
||
list.add(new Person());
|
||
list.add(2.34);
|
||
|
||
// 泛型集合
|
||
List<String> a = new ArrayList<String>();
|
||
```
|
||
|
||
|
||
|
||
#### 导入依赖
|
||
|
||
##### 传统jar包
|
||
|
||
1. 下载jar包
|
||
|
||
2. 将下载的jar包拷贝到项目中
|
||
|
||
> web项目放在web中,
|
||
|
||
3. 选择jar文件,右键 Add as Library
|
||
|
||
> 存在的问题
|
||
>
|
||
> 1. 步骤多(相对)
|
||
> 2. 在不同项目中如果需要使用同一jar包,需要分别存储这个jar文件,导致项目体积大
|
||
> 3. 在不同的环境下可能因为jar版本不一致导致项目无法运行
|
||
|
||
##### Maven导入依赖
|
||
|
||
> Maven是一个**基于项目对象模型(pom)用于进行项目的依赖管理**的工具软件
|
||
|
||
###### 核心功能
|
||
|
||
1. 依赖聚合
|
||
|
||
2. 聚合工程
|
||
|
||
> javaweb三层架构:Dao层、Service层 Servlet jsp (可以每一层放在一工程里面)
|
||
|
||
###### 官网
|
||
|
||
Maven官网:https://maven.apache.org/
|
||
|
||
![image-20220117103539600](张博凯的Java学习笔记.assets/image-20220117103539600.png)
|
||
|
||
###### 安装和配置
|
||
|
||
**1. 安装及目录介绍**
|
||
|
||
下载:https://maven.apache.org/download.cgi
|
||
|
||
> tar.gz是linux下的,zip是windows下的
|
||
>
|
||
> 注意:Maven使用前JDK需要先安装配置好
|
||
>
|
||
> - 解压
|
||
>
|
||
> - 目录结构
|
||
>
|
||
> - bin目录:存储指令文件(Maven提供了一个mvn指令)
|
||
>
|
||
> - boot目录:包含了一个类加载框架的jar文件
|
||
>
|
||
> - conf目录
|
||
>
|
||
> - settings.xml:核心配置文件
|
||
>
|
||
> - 配置本地存储库 `<localRepository></localRepository>`
|
||
>
|
||
> ![image-20220117114331099](张博凯的Java学习笔记.assets/image-20220117114331099.png)
|
||
>
|
||
> - 配置镜像
|
||
>
|
||
> ```xml
|
||
> <mirrors>
|
||
> <mirror>
|
||
> <id>aliyunmaven</id>
|
||
> <mirrorOf>*</mirrorOf>
|
||
> <name>阿里云公共仓库</name>
|
||
> <url>https://maven.aliyun.com/repository/public</url>
|
||
> </mirror>
|
||
> </mirrors>
|
||
> ```
|
||
>
|
||
> - lib目录:存放了Maven运行所需的jar文件
|
||
|
||
**2. 配置环境变量**
|
||
|
||
> | 环境变量 | 说明 |
|
||
> | ---------- | ------------------------------------------------------------ |
|
||
> | MAVEN_HOME | 压缩包解压出来的地址,例如:`D:\Program\Java\apache-maven-3.6.3` |
|
||
> | PATH | `%MAVEN_HOME%\bin` |
|
||
|
||
**3. 测试**
|
||
|
||
`mvn -version` 或 `mvn -version`
|
||
|
||
![image-20220117105823106](张博凯的Java学习笔记.assets/image-20220117105823106.png)
|
||
|
||
**4. 依赖管理流程**
|
||
|
||
![Maven.drawio](张博凯的Java学习笔记.assets/Maven.drawio.png)
|
||
|
||
本地仓库:就是本地计算机上的某个文件夹(可以自定义的任何文件夹)
|
||
|
||
远程仓库:就是远程主机上的jar文件仓库
|
||
|
||
|
||
|
||
### Java项目打包三种方式
|
||
|
||
1. pom(Maven项目)
|
||
2. jar(Java普通项目)
|
||
3. war(Javaweb项目)
|
||
|
||
|
||
|
||
## IntelliJ IDEA开发
|
||
|
||
### 配置
|
||
|
||
#### 主题配置
|
||
|
||
Settings > Appearance & Behavior > Appearance
|
||
|
||
![image-20220116150937119](张博凯的Java学习笔记.assets/image-20220116150937119.png)
|
||
|
||
|
||
|
||
#### 编码设置
|
||
|
||
Settings > Editor > File Encodings
|
||
|
||
改成UTF-8
|
||
|
||
![image-20220116150815030](张博凯的Java学习笔记.assets/image-20220116150815030.png)
|
||
|
||
|
||
|
||
#### 设置IDEA启动时不自动打开上次打开的项目
|
||
|
||
Settings > Appearance & Behavior > System Settings
|
||
|
||
![image-20220118002219620](张博凯的Java学习笔记.assets/image-20220118002219620.png)
|
||
|
||
|
||
|
||
#### 查看JDK版本
|
||
|
||
```bash
|
||
java -version
|
||
```
|
||
|
||
![image-20220116151812896](张博凯的Java学习笔记.assets/image-20220116151812896.png)
|
||
|
||
|
||
|
||
#### 配置idea中的JDK
|
||
|
||
File > Project Structure
|
||
|
||
![image-20220116161629613](张博凯的Java学习笔记.assets/image-20220116161629613.png)
|
||
|
||
![image-20220116161555885](张博凯的Java学习笔记.assets/image-20220116161555885.png)
|
||
|
||
|
||
|
||
#### 配置idea中的Maven
|
||
|
||
Settings > Build, Execution, Deployment > Build Tools > Maven
|
||
|
||
![image-20220117142419973](张博凯的Java学习笔记.assets/image-20220117142419973.png)
|
||
|
||
|
||
|
||
#### IDEA创建自定义模板
|
||
|
||
[点击文档内跳转](#IDEA自定义模板)
|
||
|
||
|
||
|
||
### 快捷键
|
||
|
||
> 有些快捷键在其他部分顺带提到了,这里仅写其他部分没有提到的快捷键
|
||
|
||
- main+回车 创建main方法
|
||
|
||
```java
|
||
public static void main(String[] args) {
|
||
|
||
}
|
||
```
|
||
|
||
- xxxx.var+回车 生成赋值语句
|
||
|
||
```java
|
||
"apple".var
|
||
// 👇
|
||
String apple = "apple";
|
||
```
|
||
|
||
- sout+回车 输出语句
|
||
|
||
```java
|
||
System.out.println();
|
||
```
|
||
|
||
- Alt+Enter 添加try catch ...
|
||
|
||
- Alt+Insert 生成构造器、Getter and Setter等
|
||
|
||
![image-20220118005611780](张博凯的Java学习笔记.assets/image-20220118005611780.png)
|
||
|
||
-
|
||
|
||
|
||
|
||
### 创建项目
|
||
|
||
#### 创建简单Java项目(控制台项目)
|
||
|
||
![image-20220116152139474](张博凯的Java学习笔记.assets/image-20220116152139474.png)
|
||
|
||
不使用模板,直接Next
|
||
|
||
![image-20220116152438232](张博凯的Java学习笔记.assets/image-20220116152438232.png)
|
||
|
||
设置项目名称和路径,点击完成
|
||
|
||
![image-20220116152545188](张博凯的Java学习笔记.assets/image-20220116152545188.png)
|
||
|
||
|
||
|
||
#### 创建Javaweb项目
|
||
|
||
2021.3与2019版本不同
|
||
|
||
##### 2021.3版本idea
|
||
|
||
参考了:https://blog.csdn.net/znufe_newer/article/details/113115002
|
||
|
||
**2021.3版本不是选左边的Java Enterprise,而是选Java!!!**
|
||
|
||
![image-20220116163216848](张博凯的Java学习笔记.assets/image-20220116163216848.png)
|
||
|
||
![image-20220116163237365](张博凯的Java学习笔记.assets/image-20220116163237365.png)
|
||
|
||
![image-20220116163251053](张博凯的Java学习笔记.assets/image-20220116163251053.png)![image-20220116163341532](张博凯的Java学习笔记.assets/image-20220116163341532.png)
|
||
|
||
![image-20220116163519894](张博凯的Java学习笔记.assets/image-20220116163519894.png)
|
||
|
||
|
||
|
||
##### 2019版本idea
|
||
|
||
步骤如下
|
||
|
||
**图中要勾上Create web.xml!!!**
|
||
|
||
![image-20220116163623140](张博凯的Java学习笔记.assets/image-20220116163623140.png)
|
||
|
||
![image-20220116163649763](张博凯的Java学习笔记.assets/image-20220116163649763.png)
|
||
|
||
|
||
|
||
#### 创建普通Maven项目(控制台项目)
|
||
|
||
![image-20220117142625153](张博凯的Java学习笔记.assets/image-20220117142625153.png)
|
||
|
||
![image-20220117142728008](张博凯的Java学习笔记.assets/image-20220117142728008.png)
|
||
|
||
|
||
|
||
##### 项目目录介绍
|
||
|
||
![image-20220117145353469](张博凯的Java学习笔记.assets/image-20220117145353469.png)
|
||
|
||
```
|
||
src/main/java # Java后端代码
|
||
src/main/resources # 配置文件
|
||
src/test/java
|
||
pom.xml # Maven依赖
|
||
```
|
||
|
||
|
||
|
||
##### Maven的pom.xml配置
|
||
|
||
> **artifactId配置中不可以有中文!**artifictid的正则表达式为`[A-Za-z0-9_\-.]`
|
||
|
||
```xml
|
||
<?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>org.example</groupId><!-- 包名 -->
|
||
<artifactId>maven_project</artifactId><!-- 项目名(不可以是中文) -->
|
||
<version>1.0-SNAPSHOT</version><!-- 版本 -->
|
||
|
||
<properties>
|
||
<maven.compiler.source>8</maven.compiler.source>
|
||
<maven.compiler.target>8</maven.compiler.target>
|
||
</properties>
|
||
|
||
<dependencies>
|
||
<!-- 项目依赖 -->
|
||
</dependencies>
|
||
|
||
</project>
|
||
```
|
||
|
||
在 `<dependencies></dependencies>` 标签中添加依赖(如果需要的话),依赖可以在 https://mvnrepository.com/ 这个网站上找到
|
||
|
||
![image-20220117143830281](张博凯的Java学习笔记.assets/image-20220117143830281.png)
|
||
|
||
依赖添加后需要点击图中 Load Maven Changes 按钮
|
||
|
||
![image-20220117144038244](张博凯的Java学习笔记.assets/image-20220117144038244.png)
|
||
|
||
如果没有的话可以右键 Maven > Reload Project
|
||
|
||
![image-20220118110133670](张博凯的Java学习笔记.assets/image-20220118110133670.png)
|
||
|
||
|
||
|
||
#### 创建Mavenweb项目
|
||
|
||
> 有两种方式
|
||
>
|
||
> 1. 创建项目时,选择Maven,勾上Create from archtype
|
||
>
|
||
> 不建议使用这种方式,因为会生成很多依赖和没有用的东西在项目中
|
||
>
|
||
> ![image-20220118002503343](张博凯的Java学习笔记.assets/image-20220118002503343.png)
|
||
>
|
||
> 2. 手动操作(见下方步骤)
|
||
|
||
![image-20220118002605490](张博凯的Java学习笔记.assets/image-20220118002605490.png)
|
||
|
||
![image-20220118002804532](张博凯的Java学习笔记.assets/image-20220118002804532.png)
|
||
|
||
配置打包方式:war包
|
||
|
||
pom.xml中添加 `<packaging>war</packaging>` ,如下:
|
||
|
||
```xml
|
||
<?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>org.example</groupId>
|
||
<artifactId>mavenweb项目</artifactId>
|
||
<version>1.0-SNAPSHOT</version>
|
||
<packaging>war</packaging>
|
||
|
||
</project>
|
||
```
|
||
|
||
添加 web.xml 文件
|
||
|
||
![image-20220118003605867](张博凯的Java学习笔记.assets/image-20220118003605867.png)
|
||
|
||
![image-20220118003711365](张博凯的Java学习笔记.assets/image-20220118003711365.png)
|
||
|
||
在 `WEB-INF` 前添加 `src\main\webapp\`
|
||
|
||
![image-20220118003805398](张博凯的Java学习笔记.assets/image-20220118003805398.png)
|
||
|
||
删除 Web Resource Directory 重新加载
|
||
|
||
![image-20220118004012885](张博凯的Java学习笔记.assets/image-20220118004012885.png)
|
||
|
||
![image-20220118004227647](张博凯的Java学习笔记.assets/image-20220118004227647.png)
|
||
|
||
即可
|
||
|
||
![image-20220118004559833](张博凯的Java学习笔记.assets/image-20220118004559833.png)
|
||
|
||
|
||
|
||
### 连接数据库
|
||
|
||
##### IDEA连接MySQL
|
||
|
||
![image-20220116172510338](张博凯的Java学习笔记.assets/image-20220116172510338.png)
|
||
|
||
![image-20220116172545108](张博凯的Java学习笔记.assets/image-20220116172545108.png)
|
||
|
||
注意:
|
||
|
||
![image-20220116172803614](张博凯的Java学习笔记.assets/image-20220116172803614.png)
|
||
|
||
填写用户名&密码
|
||
|
||
![image-20220116172859540](张博凯的Java学习笔记.assets/image-20220116172859540.png)
|
||
|
||
连接之后默认值会显示一个数据库
|
||
|
||
![image-20220117100543999](张博凯的Java学习笔记.assets/image-20220117100543999.png)
|
||
|
||
选择其他数据库
|
||
|
||
![image-20220117100636687](张博凯的Java学习笔记.assets/image-20220117100636687.png)
|
||
|
||
![image-20220117100732206](张博凯的Java学习笔记.assets/image-20220117100732206.png)
|
||
|
||
|
||
|
||
## Java
|
||
|
||
### Lombok
|
||
|
||
> Lombok是简化javabean(实体类),可以使用注解简化代码
|
||
|
||
#### 导入依赖
|
||
|
||
<span id='Lombok依赖'></span>
|
||
|
||
https://mvnrepository.com/artifact/org.projectlombok/lombok
|
||
|
||
```xml
|
||
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
|
||
<dependency>
|
||
<groupId>org.projectlombok</groupId>
|
||
<artifactId>lombok</artifactId>
|
||
<version>1.18.22</version>
|
||
<scope>provided</scope>
|
||
</dependency>
|
||
```
|
||
|
||
|
||
|
||
#### IDEA中使用
|
||
|
||
> IDEA中使用必须安装lombok插件,才可以生效
|
||
|
||
![image-20220118005337949](张博凯的Java学习笔记.assets/image-20220118005337949.png)
|
||
|
||
|
||
|
||
#### 常用注解
|
||
|
||
1. @Data
|
||
|
||
> 相当于实体类封装的get和set方法
|
||
|
||
2. @Setter
|
||
|
||
> 相当于实体类封装的set方法
|
||
|
||
3. @Getter
|
||
|
||
> 相当于实体类封装的get方法
|
||
|
||
4. @NoArgsConstructor
|
||
|
||
> 相当于无参构造函数
|
||
|
||
5. @AllArgsConstructor
|
||
|
||
> 相当于有参构造函数
|
||
|
||
|
||
|
||
## Javaweb
|
||
|
||
### JDBC(以MySQL为例)
|
||
|
||
> Java连接数据库的技术
|
||
|
||
#### 导入MySQL依赖包
|
||
|
||
从网上下载MySQL的jar包:https://mvnrepository.com/artifact/mysql/mysql-connector-java
|
||
|
||
> MySQL 5.x版本导入5.1.x版本jar包,8.x版本导入8.0.x版本jar包
|
||
|
||
![image-20220116164807251](张博凯的Java学习笔记.assets/image-20220116164807251.png)
|
||
|
||
下载后放入项目web/lib文件夹下:
|
||
|
||
![image-20220116164942847](张博凯的Java学习笔记.assets/image-20220116164942847.png)
|
||
|
||
添加jar包
|
||
|
||
![image-20220116165411807](张博凯的Java学习笔记.assets/image-20220116165411807.png)
|
||
|
||
创建User.java,并编写Java代码:
|
||
|
||
全限定名com.mysql.jdbc.Driver是这样复制出来的
|
||
|
||
![image-20220116165755225](张博凯的Java学习笔记.assets/image-20220116165755225.png)
|
||
|
||
![image-20220116165818350](张博凯的Java学习笔记.assets/image-20220116165818350.png)
|
||
|
||
```java
|
||
package org.exmple.entity;
|
||
|
||
import java.sql.*;
|
||
|
||
/**
|
||
* JDBC链接数据库的操作
|
||
*/
|
||
public class UserJdbc {
|
||
public static void main(String[] args) {
|
||
// 1. 加载驱动(全限定名com.mysql.jdbc.Driver是复制进来的,也可以一个一个 . 出来)
|
||
try { // 2. Alt + Enter 抛出异常
|
||
Class.forName("com.mysql.jdbc.Driver");
|
||
|
||
// 3. 创建连接对象(Connection是java.sql下的)
|
||
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/dormitory","root","111111");
|
||
// 4. Alt + Enter 抛出异常
|
||
|
||
// 5. 写SQL语句
|
||
String sql = "select * from d_student";
|
||
|
||
// 6. 创建对象 PreparedStatement
|
||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||
|
||
// 7. 执行命令
|
||
ResultSet resultSet = preparedStatement.executeQuery();
|
||
|
||
// 8. 循环遍历
|
||
while (resultSet.next()){
|
||
System.out.println(resultSet.getInt("s_id")+"\t"+resultSet.getString("s_name"));
|
||
}
|
||
|
||
} catch (ClassNotFoundException e) {
|
||
e.printStackTrace();
|
||
} catch (SQLException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
```
|
||
|
||
执行结果
|
||
|
||
![image-20220117101802262](张博凯的Java学习笔记.assets/image-20220117101802262.png)
|
||
|
||
|
||
|
||
#### Maven
|
||
|
||
<span id='mysql依赖'></span>
|
||
|
||
从网上下载MySQL的jar包:https://mvnrepository.com/artifact/mysql/mysql-connector-java
|
||
|
||
> MySQL 5.x版本选择5.1.x版本,8.x版本选择.0.x版本
|
||
|
||
下面以MySQL 5.7版本为例:
|
||
|
||
![image-20220117143510876](张博凯的Java学习笔记.assets/image-20220117143510876.png)
|
||
|
||
![image-20220117143541787](张博凯的Java学习笔记.assets/image-20220117143541787.png)
|
||
|
||
```xml
|
||
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
|
||
<dependency>
|
||
<groupId>mysql</groupId>
|
||
<artifactId>mysql-connector-java</artifactId>
|
||
<version>5.1.49</version>
|
||
</dependency>
|
||
```
|
||
|
||
|
||
|
||
|
||
|
||
### Servlet
|
||
|
||
> 后端与前端进行数据交互和页面传递
|
||
|
||
跳转页面的方式
|
||
|
||
- forward 转发
|
||
- redirect 重定向
|
||
|
||
### JSP
|
||
|
||
> 动态网页
|
||
|
||
JSP中Java代码需要用 `<% %>` 包起来
|
||
|
||
### Thymeleaf
|
||
|
||
> 模板引擎
|
||
|
||
### HTML5
|
||
|
||
> 静态网页
|
||
|
||
### CSS
|
||
|
||
> 页面样式
|
||
|
||
### JavaScript
|
||
|
||
#### JavaQuery(jQuery)
|
||
|
||
### 前端框架
|
||
|
||
#### Layui
|
||
|
||
#### Vue.js
|
||
|
||
#### elementUI
|
||
|
||
### 数据库
|
||
|
||
#### 关系型数据库
|
||
|
||
SQL Server
|
||
|
||
MySQL
|
||
|
||
Oracle
|
||
|
||
#### 非关系型数据库
|
||
|
||
redis
|
||
|
||
|
||
|
||
## 企业级框架SSM
|
||
|
||
> 框架:框架,就是软件的半成品,完成了软件开发过程中的通用操作,程序员只需很少或者不用进行加工就能实现特定的功能,从而简化开发人员在开发过程中的开发步骤,提高开发效率。
|
||
>
|
||
> 常用框架
|
||
>
|
||
> - MVC框架:简化了Servlet的开发步骤
|
||
> - struts
|
||
> - struts2
|
||
> - SpringMVC
|
||
> - 持久层框架:完成数据库操作的框架
|
||
> - Apache DBUtils
|
||
> - Hibernate
|
||
> - MyBatis
|
||
> - 胶水框架:Spring
|
||
>
|
||
> SSM:Spring SpringMVC MyBatis
|
||
>
|
||
> SSH:Spring Structs2 Hibernate
|
||
|
||
|
||
|
||
### MyBatis框架
|
||
|
||
> 数据访问(操作数据库),JDBC角色
|
||
>
|
||
> MyBatis是一个半自动的ORM框架
|
||
>
|
||
> ORM:Object Relational Mapping,对象关系映射,将Java中的一个对象与数据表中的一行记录一一对应。(表映射为类,类名一般是表名首字母大写,类中的属性就是表中的字段名)
|
||
>
|
||
> ORM框架提供了实体类与数据表的映射关系,通过映射文件的配置,实现对象的持久化
|
||
|
||
> MyBatis的前身是iBatis,是Apache提供 的一个开源项目
|
||
>
|
||
> 对JDBC进行了封装,几乎消除了所有的JDBC代码
|
||
>
|
||
> 支持xml和注解配置方式完成ORM操作
|
||
|
||
官网:https://mybatis.org/mybatis-3/
|
||
|
||
|
||
|
||
#### MyBatis框架部署及CRUD
|
||
|
||
> MyBatis操作数据库进行增加、删除、修改、查询操作
|
||
|
||
首先创建Maven项目(Maven普通或Mavenweb)
|
||
|
||
添加Maven依赖(目前官网最新版是3.5.9)
|
||
|
||
https://mvnrepository.com/artifact/org.mybatis/mybatis
|
||
|
||
```xml
|
||
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
|
||
<dependency>
|
||
<groupId>org.mybatis</groupId>
|
||
<artifactId>mybatis</artifactId>
|
||
<version>3.5.9</version>
|
||
</dependency>
|
||
```
|
||
|
||
同时还需要添加
|
||
|
||
- MySQL依赖:[点击文档内跳转](#mysql依赖)
|
||
- Lombok依赖:[点击文档内跳转](#Lombok依赖)
|
||
|
||
|
||
|
||
##### 创建MyBatis配置文件
|
||
|
||
在resources目录下创建mybatis-config.xml文件
|
||
|
||
```xml
|
||
<?xml version="1.0" encoding="UTF-8" ?>
|
||
<!DOCTYPE configuration
|
||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||
<configuration>
|
||
</configuration>
|
||
```
|
||
|
||
###### IDEA创建自定义模板
|
||
|
||
<span id='IDEA自定义模板'></span>
|
||
|
||
![image-20220118103532515](张博凯的Java学习笔记.assets/image-20220118103532515.png)
|
||
|
||
Name: `mybatis-config`
|
||
|
||
Extension: `xml`
|
||
|
||
File Name: `mybatis-config.xml`
|
||
|
||
勾选 Enable Live Templates 激活当前模板
|
||
|
||
模板代码👇
|
||
|
||
```xml
|
||
<?xml version="1.0" encoding="UTF-8" ?>
|
||
<!DOCTYPE configuration
|
||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||
<configuration>
|
||
|
||
</configuration>
|
||
```
|
||
|
||
出现如图问题解决方案
|
||
|
||
![image-20220118104342493](张博凯的Java学习笔记.assets/image-20220118104342493.png)
|
||
|
||
![image-20220118104541497](张博凯的Java学习笔记.assets/image-20220118104541497.png)
|
||
|
||
|
||
|
||
###### 在mybatis-config.xml中进行数据库连接信息配置
|
||
|
||
> environments标签中可以定义多个environment,每个environment标签可以定义一套连接配置default属性,用来指定使用哪个environment标签
|
||
>
|
||
> transactionManager标签用于配置数据库管理方式(默认是JDBC
|
||
>
|
||
> dataSource标签用于配置数据库的连接信息
|
||
|
||
配置模板(idea中Maven配置好后输入 `<en` 回车即可自动补全)
|
||
|
||
```xml
|
||
<configuration>
|
||
<environments default="">
|
||
<environment id="">
|
||
<transactionManager type=""></transactionManager>
|
||
<dataSource type=""></dataSource>
|
||
</environment>
|
||
</environments>
|
||
</configuration>
|
||
```
|
||
|
||
配置信息
|
||
|
||
```xml
|
||
<configuration>
|
||
<environments default="mysql">
|
||
<environment id="mysql">
|
||
<transactionManager type="JDBC"></transactionManager>
|
||
<dataSource type="POOLED">
|
||
<property name="driver" value="com.mysql.jdbc.Driver"/>
|
||
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
|
||
<property name="username" value="root"/>
|
||
<property name="password" value="111111"/>
|
||
</dataSource>
|
||
</environment>
|
||
</environments>
|
||
</configuration>
|
||
```
|
||
|
||
> 还可以将数据库密码写在配置文件中,避免直接将数据库配置信息写在mybatis-config.xml文件中,[传送门](#使用db.properties文件进行数据库配置)
|
||
|
||
> 如果出现了中文乱码,在jdbc:mysql://localhost:3306/mydb后面添加 `characterEncoding=utf-8`
|
||
|
||
|
||
|
||
##### MyBatis框架使用
|
||
|
||
> 案例:学生信息的数据库操作
|
||
|
||
###### 创建数据库表
|
||
|
||
创建一个数据库,创建student数据表,并插入数据
|
||
|
||
```sql
|
||
CREATE DATABASE `mydb` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_520_ci';
|
||
|
||
CREATE TABLE `student` (
|
||
`stuId` int(11) NOT NULL AUTO_INCREMENT,
|
||
`stuName` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
|
||
`stuGender` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
|
||
`stuAge` int(255) NOT NULL,
|
||
PRIMARY KEY (`stuId`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_520_ci;
|
||
|
||
INSERT INTO `mydb`.`student` (`stuName`, `stuGender`, `stuAge`) VALUES ('admin', '男', 18);
|
||
INSERT INTO `mydb`.`student` (`stuName`, `stuGender`, `stuAge`) VALUES ('tom', '男', 19);
|
||
```
|
||
|
||
###### 创建实体类
|
||
|
||
> ORM映射关系:类名就是表名的首字母大写,类中的属性就是表中的字段名
|
||
>
|
||
> @Data:类要进行封装: 1. 属性私有化(private); 2. 生成get和set方法
|
||
|
||
src/main/java/org/example/entity/Student.java
|
||
|
||
```java
|
||
package org.example;
|
||
|
||
import lombok.AllArgsConstructor;
|
||
import lombok.Data;
|
||
import lombok.NoArgsConstructor;
|
||
|
||
/**
|
||
* 学生实体类
|
||
*/
|
||
@Data
|
||
@NoArgsConstructor
|
||
@AllArgsConstructor
|
||
public class Student {
|
||
|
||
private int stuId;
|
||
|
||
private String stuName;
|
||
|
||
private String stuGender;
|
||
|
||
private int stuAge;
|
||
}
|
||
```
|
||
|
||
###### 创建Dao接口,定义操作方法
|
||
|
||
src/main/java/org/example/dao/StudentDao.java
|
||
|
||
```java
|
||
package org.example.dao;
|
||
|
||
import org.example.entity.Student;
|
||
|
||
/**
|
||
* 接口
|
||
*/
|
||
public interface StudentDao {
|
||
/**
|
||
* 添加学生
|
||
* @param student
|
||
* @return
|
||
*/
|
||
public int insertStu(Student student);
|
||
|
||
/**
|
||
* 删除学生
|
||
* @param stuId
|
||
* @return
|
||
*/
|
||
public int deletetStu(int stuId);
|
||
|
||
/**
|
||
* 查询
|
||
* @param stuName
|
||
* @param stuGender
|
||
* @return
|
||
*/
|
||
public Student login(String stuName, String stuGender);
|
||
|
||
/**
|
||
* 查询所有学生信息
|
||
* @return
|
||
*/
|
||
public List<Student> showStudent();
|
||
}
|
||
```
|
||
|
||
|
||
|
||
###### 创建Dao接口的映射文件
|
||
|
||
在resources目录下创建mapper文件
|
||
|
||
> 可以在IDEA中自定义一个模板,mapper文件模板如下,[参考](#IDEA自定义模板)
|
||
>
|
||
> ```xml
|
||
> <?xml version="1.0" encoding="UTF-8" ?>
|
||
> <!DOCTYPE mapper
|
||
> PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||
> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||
> <mapper>
|
||
>
|
||
> </mapper>
|
||
> ```
|
||
|
||
- **创建StudentMapper.xml文件**
|
||
|
||
src/main/resources/mappers/StudentMapper.xml
|
||
|
||
> 使用上方的mapper文件模板创建
|
||
|
||
- **StudentDao.java与StudentMapper.xml保持一致,将StudentDao.java接口改为StudentMapper.java**
|
||
|
||
###### 在映射文件中对DAO中的方法进行实现
|
||
|
||
```xml
|
||
<?xml version="1.0" encoding="UTF-8" ?>
|
||
<!DOCTYPE mapper
|
||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||
<!--
|
||
mapper文件相当于Dao接口的实现类
|
||
namespace要制定实现类的全限定名(从包名找到类)
|
||
-->
|
||
<mapper namespace="org.example.dao.StudentMapper">
|
||
<!--
|
||
id:对应接口中的方法名称
|
||
parameterType:传入的参数类型的全限定名
|
||
-->
|
||
<!-- 增加操作 -->
|
||
<insert id="instertStu" parameterType="org.example.entity.Student">
|
||
insert into student(stuName, stuGender, stuAge) VALUES (#{stuName}, #{stuGender}, #{stuAge});
|
||
</insert>
|
||
|
||
<!-- 删除操作 -->
|
||
<delete id="deleteStu">
|
||
delete from student where stuId=#{stuId};
|
||
</delete>
|
||
|
||
<!-- 查询操作 -->
|
||
<!--
|
||
resultType:返回值的类型,基本数据类型不用写,只有对象或者集合使用
|
||
-->
|
||
<select id="login" resultType="org.example.entity.Student">
|
||
select * from student where stuName=#{arg0} and stuGender=#{arg1}
|
||
</select>
|
||
<!--
|
||
模糊查询:
|
||
- stuName LIKE '%a' # 以 a 结尾
|
||
- stuName LIKE 'a%' # 以 a 开头
|
||
- stuName LIKE '%a%' # 包含 a
|
||
例如:
|
||
select * from student where stuName LIKE concat('%', #{arg0}, '%')
|
||
-->
|
||
|
||
<!-- 查询所有 -->
|
||
<select id="showStudent" resultType="org.example.entity.Student">
|
||
select * from student
|
||
</select>
|
||
</mapper>
|
||
```
|
||
|
||
> [多参数使用]
|
||
>
|
||
> **当传入参数大于或等于2个时,有三种方式实现**,上方代码使用的是第1种实现方式
|
||
>
|
||
> 1. `arg0`, `arg1`, `arg2`, `...` 实现
|
||
>
|
||
> 如上方代码中的实现方式
|
||
>
|
||
> ```xml
|
||
> <select id="login" resultType="org.example.entity.Student">
|
||
> select * from student where stuName=#{arg0} and stuGender=#{arg1}
|
||
> </select>
|
||
> ```
|
||
>
|
||
> 2. `param1`, `param2`, `param3`, `...` 实现
|
||
>
|
||
> ```xml
|
||
> <select id="login" resultType="org.example.entity.Student">
|
||
> select * from student where stuName=#{param1} and stuGender=#{param2}
|
||
> </select>
|
||
> ```
|
||
>
|
||
> 3. 使用注解配置(推荐)
|
||
>
|
||
> 首先在Dao接口中添加注解如下
|
||
>
|
||
> ```java
|
||
> /**
|
||
> * 查询
|
||
> * @param stuName
|
||
> * @param stuGender
|
||
> * @return
|
||
> */
|
||
> public Student login(@Param("name") String stuName, @Param("pwd") String stuGender);
|
||
> ```
|
||
>
|
||
> 然后在xml文件中如下配置
|
||
>
|
||
> ```xml
|
||
> <select id="login" resultType="org.example.entity.Student">
|
||
> select * from student where stuName=#{name} and stuGender=#{pwd}
|
||
> </select>
|
||
> ```
|
||
|
||
|
||
|
||
目前为止,项目的结构应该如下
|
||
|
||
![image-20220118161808106](张博凯的Java学习笔记.assets/image-20220118161808106.png)
|
||
|
||
|
||
|
||
###### 将映射文件添加到主配置文件中
|
||
|
||
在mybatis-config.xml中配置
|
||
|
||
src/main/resources/mybatis-config.xml
|
||
|
||
```
|
||
<?xml version="1.0" encoding="UTF-8" ?>
|
||
<!DOCTYPE configuration
|
||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||
<configuration>
|
||
<!-- environments标签 -->
|
||
|
||
<!-- 添加mapper映射文件 -->
|
||
<mappers>
|
||
<mapper resource="mappers/StudentMapper.xml"></mapper>
|
||
</mappers>
|
||
</configuration>
|
||
```
|
||
|
||
|
||
|
||
##### 单元测试
|
||
|
||
> junit单元测试
|
||
|
||
###### 添加单元测试的依赖
|
||
|
||
https://mvnrepository.com/artifact/junit/junit/4.12
|
||
|
||
```xml
|
||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||
<dependency>
|
||
<groupId>junit</groupId>
|
||
<artifactId>junit</artifactId>
|
||
<version>4.12</version>
|
||
<scope>test</scope>
|
||
</dependency>
|
||
```
|
||
|
||
|
||
|
||
###### 创建测试
|
||
|
||
创建TestStudent.java文件
|
||
|
||
src/test/java/org/example/test/TestStudent.java
|
||
|
||
```java
|
||
package org.example.test;
|
||
|
||
import org.apache.ibatis.io.Resources;
|
||
import org.apache.ibatis.session.SqlSession;
|
||
import org.apache.ibatis.session.SqlSessionFactory;
|
||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||
import org.example.dao.StudentMapper;
|
||
import org.example.entity.Student;
|
||
import org.junit.Test;
|
||
|
||
import java.io.IOException;
|
||
import java.io.InputStream;
|
||
|
||
public class TestStudent {
|
||
|
||
@Test
|
||
public void add() {
|
||
try {
|
||
// 1. 加载mybatis的主配置文件
|
||
// 选择 org.apache.ibatis 包下的
|
||
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
|
||
|
||
// 2. 创建会话工厂
|
||
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
|
||
|
||
// 3. 获取会话
|
||
SqlSession sqlSession = factory.openSession();
|
||
|
||
// 4. 通过会话获取dao对象
|
||
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
|
||
|
||
// 5. 调用方法
|
||
Student student = new Student();
|
||
student.setStuName("bb");
|
||
student.setStuGender("男");
|
||
student.setStuAge(18);
|
||
int i = mapper.insertStu(student);
|
||
if (i > 0) {
|
||
// 增删改会涉及到事务
|
||
// 进行事务提交
|
||
sqlSession.commit();
|
||
System.out.println("成功");
|
||
} else {
|
||
System.out.println("失败");
|
||
}
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
@Test
|
||
public void delete() {
|
||
// 此处省略
|
||
}
|
||
|
||
@Test
|
||
public void login() {
|
||
try {
|
||
// 1. ~ 4. 同 add()
|
||
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
|
||
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
|
||
SqlSession sqlSession = factory.openSession();
|
||
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
|
||
|
||
// 5. 调用方法
|
||
Student student = mapper.login("admin","男");
|
||
System.out.println(student);
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
@Test
|
||
public void showStudent() {
|
||
try {
|
||
// 1. ~ 4. 同 add()
|
||
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
|
||
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
|
||
SqlSession sqlSession = factory.openSession();
|
||
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
|
||
|
||
// 5. 调用方法
|
||
List<Student> students = mapper.showStudent();
|
||
System.out.println(students);
|
||
|
||
/**
|
||
* 可以这样遍历输出每一个元素
|
||
* for (Student stu:students) {
|
||
* System.out.println(stu);
|
||
* }
|
||
*/
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
测试:
|
||
|
||
添加成功
|
||
|
||
![image-20220118172524278](张博凯的Java学习笔记.assets/image-20220118172524278.png)
|
||
|
||
![image-20220118172654675](张博凯的Java学习笔记.assets/image-20220118172654675.png)
|
||
|
||
查询成功
|
||
|
||
![image-20220118182635014](张博凯的Java学习笔记.assets/image-20220118182635014.png)
|
||
|
||
查询所有成功
|
||
|
||
![image-20220118190921546](张博凯的Java学习笔记.assets/image-20220118190921546.png)
|
||
|
||
> [输出]
|
||
>
|
||
> ```java
|
||
> [Student(stuId=1, stuName=admin, stuGender=男, stuAge=18), Student(stuId=2, stuName=tom, stuGender=男, stuAge=19), Student(stuId=3, stuName=bb, stuGender=男, stuAge=18)]
|
||
> ```
|
||
|
||
|
||
|
||
#### MyBatis文件
|
||
|
||
##### MyBatis核心配置文件
|
||
|
||
> mybatis-config.xml是MyBatis框架的主配置文件,只要用于配置MyBatis的数据源及属性信息
|
||
|
||
>mybatis-config.xml中 `<configuration>` 中的标签配置顺序如下:
|
||
>
|
||
>properties,settings,typeAliases,typeHandlers,objectFactory,objectWrapperFactory,plugins,environments,mappers
|
||
|
||
###### properties标签
|
||
|
||
<span id='使用db.properties文件进行数据库配置'></span>
|
||
|
||
> 用于设置键值对,或者加载属性文件
|
||
|
||
在resources目录下创建db.properties文件,配置键值对
|
||
|
||
src/main/resources/db.properties
|
||
|
||
```properties
|
||
driver=com.mysql.jdbc.Driver
|
||
url=jdbc:mysql://localhost:3306/mydb
|
||
username=root
|
||
password=111111
|
||
```
|
||
|
||
在mybatis-config.xml的 `<configuration></configuration>` 标签中添加 `<properties resource="db.properties"></properties>` ,然后后面的数据库配置信息可以直接使用 `${配置项名称}` 取得
|
||
|
||
> [注意] `<properties>` 标签要写在 `<environments>` 之前
|
||
|
||
```xml
|
||
<configuration>
|
||
<properties resource="db.properties"></properties>
|
||
|
||
<environments default="mysql">
|
||
<environment id="mysql">
|
||
<transactionManager type="JDBC"></transactionManager>
|
||
<dataSource type="POOLED">
|
||
<property name="driver" value="${driver}"/>
|
||
<property name="url" value="${url}"/>
|
||
<property name="username" value="${username}"/>
|
||
<property name="password" value="${password}"/>
|
||
</dataSource>
|
||
</environment>
|
||
</environments>
|
||
</configuration>
|
||
```
|
||
|
||
|
||
|
||
###### typeAliases标签
|
||
|
||
> typeAliases用于给实体类取别名,在映射文件中可以直接使用别名来替换实体类的全限定名
|
||
|
||
```xml
|
||
<configuration>
|
||
<typeAliases>
|
||
<typeAlias type="org.example.entity.Student" alias="student"></typeAlias>
|
||
</typeAliases>
|
||
</configuration>
|
||
```
|
||
|
||
配置后,可以如下修改
|
||
|
||
```
|
||
<mapper namespace="...">
|
||
<!-- 配置前 -->
|
||
<insert id="..." parameterType="org.example.entity.Student">
|
||
...
|
||
</insert>
|
||
|
||
<!-- 配置后 -->
|
||
<insert id="..." parameterType="student">
|
||
...
|
||
</insert>
|
||
</mapper>
|
||
```
|
||
|
||
|
||
|
||
###### plugin插件
|
||
|
||
> 插件标签:向分页插件的使用
|
||
|
||
```xml
|
||
<configuration>
|
||
<plugins>
|
||
<plugin interceptor=""></plugin>
|
||
</plugins>
|
||
</configuration>
|
||
```
|
||
|
||
|
||
|
||
###### mappers标签
|
||
|
||
> 加载映射文件配置
|
||
|
||
```xml
|
||
<configuration>
|
||
<mappers>
|
||
<mapper resource="mappers/StudentMapper.xml"></mapper>
|
||
</mappers>
|
||
</configuration>
|
||
```
|
||
|
||
|
||
|
||
##### 映射文件
|
||
|
||
###### mapper标签
|
||
|
||
> mapper文件相当于Dao接口的实现类,namespace属性指定实现Dao接口的全限定名
|
||
|
||
|
||
|
||
###### insert、update、delete、select标签
|
||
|
||
> 增加、修改、删除、查询
|
||
|
||
|
||
|
||
#### 分页插件
|
||
|
||
> Mybaits提供了一个PageHelper
|
||
>
|
||
> PageHelper官网:https://pagehelper.github.io/
|
||
>
|
||
> GitHub:https://github.com/pagehelper/Mybatis-PageHelper
|
||
>
|
||
> 中文使用方法:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
|
||
|
||
##### 分页SQL语句
|
||
|
||
> 分页 关键字 limit
|
||
>
|
||
> 需要提供:当前页 一页显示的数据(页面容量)
|
||
>
|
||
> 查询表中共有多少条数据:`select count(*) from student;`
|
||
>
|
||
> 分页:`select count(*) from student limit (pageNum-1)*pageSize, pageSize;`
|
||
|
||
|
||
|
||
##### 导入PageHelper依赖
|
||
|
||
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper/5.3.0
|
||
|
||
```xml
|
||
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
|
||
<dependency>
|
||
<groupId>com.github.pagehelper</groupId>
|
||
<artifactId>pagehelper</artifactId>
|
||
<version>5.3.0</version>
|
||
</dependency>
|
||
```
|
||
|
||
|
||
|
||
##### 配置PageHelper拦截器
|
||
|
||
在mybatis-config.xml中配置
|
||
|
||
```xml
|
||
<!--
|
||
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
|
||
properties?, settings?,
|
||
typeAliases?, typeHandlers?,
|
||
objectFactory?,objectWrapperFactory?,
|
||
plugins?,
|
||
environments?, databaseIdProvider?, mappers?
|
||
-->
|
||
<plugins>
|
||
<!-- com.github.pagehelper为PageHelper类所在包名 -->
|
||
<plugin interceptor="com.github.pagehelper.PageInterceptor">
|
||
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
|
||
<property name="param1" value="value1"/>
|
||
</plugin>
|
||
</plugins>
|
||
```
|
||
|
||
|
||
|
||
##### 插件使用
|
||
|
||
```
|
||
// 分页前
|
||
List<Student> students = mapper.showStudent();
|
||
System.out.println(students);
|
||
|
||
// 分页后
|
||
PageHelper.startPage(1, 2);
|
||
List<Student> students = mapper.showStudent();
|
||
PageInfo<Student> pageInfo = new PageInfo<Student>(students);
|
||
System.out.println(pageInfo);
|
||
|
||
// 输出信息
|
||
System.out.println("当前页:" + pageInfo.getPageNum());
|
||
System.out.println("页面容量:" + pageInfo.getSize());
|
||
System.out.println("总条数:" + pageInfo.getTotal());
|
||
System.out.println("总页数:" + pageInfo.getPages());
|
||
System.out.println("显示的数据:");
|
||
for (Student student:pageInfo.getList() ) {
|
||
System.out.println(
|
||
student.getStuId() + "\t" +
|
||
student.getStuName() + "\t" +
|
||
student.getStuGender() + "\t" +
|
||
student.getStuAge()
|
||
);
|
||
}
|
||
```
|
||
|
||
查询结果
|
||
|
||
![image-20220118232516699](张博凯的Java学习笔记.assets/image-20220118232516699.png)
|
||
|
||
|
||
|
||
#### log4j日志打印
|
||
|
||
> 查看日志信息
|
||
|
||
##### log4j依赖
|
||
|
||
https://mvnrepository.com/artifact/log4j/log4j/1.2.17
|
||
|
||
```xml
|
||
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
|
||
<dependency>
|
||
<groupId>log4j</groupId>
|
||
<artifactId>log4j</artifactId>
|
||
<version>1.2.17</version>
|
||
</dependency>
|
||
```
|
||
|
||
|
||
|
||
##### 导入log4j.propertites文件
|
||
|
||
src/main/resources/log4j.properties
|
||
|
||
```
|
||
# Set root category priority to INFO and its only appender to CONSOLE.
|
||
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
|
||
log4j.rootCategory=debug, CONSOLE, LOGFILE
|
||
|
||
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
|
||
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
|
||
|
||
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
|
||
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
|
||
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
|
||
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
|
||
|
||
# LOGFILE is set to be a File appender using a PatternLayout.
|
||
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
|
||
log4j.appender.LOGFILE.File=D:\axis.log
|
||
log4j.appender.LOGFILE.Append=true
|
||
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
|
||
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
|
||
```
|
||
|
||
|
||
|
||
#### 数据表关联映射
|
||
|
||
> 至少两张表,表与表之间有关系
|
||
>
|
||
> 映射关系:一对一,一对多,多对一,多对多
|
||
|
||
##### 一对一关系
|
||
|
||
> 实例:用户表—用户详情表
|
||
>
|
||
> **查询用户的姓名、密码、年龄、地址、电话号码**
|
||
>
|
||
> ```sql
|
||
> # 两种方式:
|
||
> select * from user u,details d where u.user_id=d.user_id;
|
||
> select * from user u inner join details d on u.user_id=d.user_id;
|
||
> ```
|
||
|
||
###### 数据库设计
|
||
|
||
用户信息表
|
||
|
||
```sql
|
||
CREATE TABLE `user` (
|
||
`user_id` int(11) NOT NULL AUTO_INCREMENT,
|
||
`user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`user_pwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`user_realname` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`user_age` int(11) DEFAULT NULL,
|
||
PRIMARY KEY (`user_id`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
|
||
```
|
||
|
||
用户详情表
|
||
|
||
```sql
|
||
CREATE TABLE `details` (
|
||
`detail_id` int(11) NOT NULL AUTO_INCREMENT,
|
||
`user_address` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`user_desc` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`user_phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`user_id` int(11) NOT NULL,
|
||
PRIMARY KEY (`detail_id`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
|
||
```
|
||
|
||
###### 创建实体类
|
||
|
||
用户信息类
|
||
|
||
src/main/java/org/example/entity/User.java
|
||
|
||
```java
|
||
package org.example.entity;
|
||
|
||
import lombok.Data;
|
||
import lombok.NoArgsConstructor;
|
||
import lombok.AllArgsConstructor;
|
||
|
||
/**
|
||
* 对应表 用户信息表
|
||
* 类要封装: 1. 私有化 2.生成get和set方法
|
||
*/
|
||
@Data
|
||
@NoArgsConstructor
|
||
@AllArgsConstructor
|
||
public class User {
|
||
private int userId;
|
||
private String userName;
|
||
private String userPwd;
|
||
private String userRealname;
|
||
private int userAge;
|
||
private Details details; // 一对一关系
|
||
}
|
||
```
|
||
|
||
用户详细信息类
|
||
|
||
src/main/java/org/example/entity/Details.java
|
||
|
||
```java
|
||
package org.example.entity;
|
||
|
||
import lombok.Data;
|
||
import lombok.NoArgsConstructor
|
||
import lombok.AllArgsConstructor;
|
||
|
||
/**
|
||
* 用户详情表
|
||
*/
|
||
@Data
|
||
@NoArgsConstructor
|
||
@AllArgsConstructor
|
||
public class Details {
|
||
private int detailId;
|
||
private String userAddress;
|
||
private String userDesc;
|
||
private String userPhone;
|
||
private int userId;
|
||
}
|
||
```
|
||
|
||
###### mapper接口
|
||
|
||
src/main/java/org/example/dao/UserMapper.java
|
||
|
||
```java
|
||
package org.example.dao;
|
||
|
||
import org.example.entity.User;
|
||
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 接口
|
||
*/
|
||
public interface UserMapper {
|
||
/**
|
||
* 查询信息
|
||
* @return
|
||
*/
|
||
public List<User> showUser();
|
||
}
|
||
```
|
||
|
||
###### mapper.xml文件
|
||
|
||
src/main/resources/mappers/UserMapper.xml
|
||
|
||
```xml
|
||
<mapper namespace="org.example.dao.UserMapper">
|
||
<resultMap id="u" type="user">
|
||
<!--
|
||
id标签:主键id
|
||
result标签: 属性
|
||
property:类中的属性名
|
||
column:对应表的字段名
|
||
-->
|
||
<id property="userId" column="user_id"></id>
|
||
<result property="userName" column="user_name"></result>
|
||
<result property="userPwd" column="user_pwd"></result>
|
||
<result property="userRealname" column="user_realname"></result>
|
||
<result property="userAge" column="user_age"></result>
|
||
|
||
<!-- 一对一关系 -->
|
||
<association property="details" javaType="org.example.entity.Details">
|
||
<result property="detailId" column="detail_id"></result>
|
||
<result property="userAddress" column="user_address"></result>
|
||
<result property="userDesc" column="user_desc"></result>
|
||
<result property="userPhone" column="user_phone"></result>
|
||
<result property="userId" column="user_id"></result>
|
||
</association>
|
||
</resultMap>
|
||
|
||
<select id="showUser" resultMap="u">
|
||
select * from user u inner join details d on u.user_id=d.user_id
|
||
</select>
|
||
</mapper>
|
||
```
|
||
|
||
> 记得在mybatis-config.xml中配置 `<mappers>`
|
||
>
|
||
> ```xml
|
||
> <configuration>
|
||
> <mappers>
|
||
> <mapper resource="mappers/UserMapper.xml"></mapper>
|
||
> </mappers>
|
||
> </configuration>
|
||
> ```
|
||
|
||
###### 测试类
|
||
|
||
src/test/java/org/example/test/UserTest.java
|
||
|
||
```java
|
||
...
|
||
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
|
||
List<User> users = mapper.showUser();
|
||
for(User user:users) {
|
||
System.out.println(
|
||
user.getUserName() + "\t" +
|
||
user.getUserPwd() + "\t" +
|
||
user.getUserAge() + "\t" +
|
||
user.getDetails().getUserAddress() + "\t" +
|
||
user.getDetails().getUserPhone()
|
||
);
|
||
}
|
||
...
|
||
```
|
||
|
||
|
||
|
||
##### 一对多关系
|
||
|
||
>案例:班级(1)—学生(n)
|
||
>
|
||
>**通过班级id查询该班级的信息及班级下的所有学生的信息**
|
||
>
|
||
>```sql
|
||
>select * from classes c inner join student s on c.cid=s.scid and c.cid=1;
|
||
>select * from classes c,student s where c.cid=s.scid and c.cid=1;
|
||
>```
|
||
|
||
###### 创建数据库表
|
||
|
||
<span id="一对多关系创建数据表"></span>
|
||
|
||
班级表
|
||
|
||
```sql
|
||
CREATE TABLE `classes` (
|
||
`cid` int(11) NOT NULL AUTO_INCREMENT,
|
||
`cname` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
|
||
`cdesc` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
|
||
PRIMARY KEY (`cid`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
|
||
```
|
||
|
||
学生表
|
||
|
||
```sql
|
||
CREATE TABLE `student` (
|
||
`sid` int(11) NOT NULL AUTO_INCREMENT,
|
||
`sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`sage` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`scid` int(11) NOT NULL,
|
||
PRIMARY KEY (`sid`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
|
||
```
|
||
|
||
###### 创建实体类
|
||
|
||
班级类
|
||
|
||
src/main/java/org/example/entity/Classes.java
|
||
|
||
```java
|
||
package org.example.entity;
|
||
|
||
import lombok.Data;
|
||
import lombok.NoArgsConstructor;
|
||
import lombok.AllArgsConstructor;
|
||
|
||
import java.util.List;
|
||
|
||
@Data
|
||
@NoArgsConstructor
|
||
@AllArgsConstructor
|
||
public class Classes {
|
||
private int cid;
|
||
private String cname;
|
||
private String cdesc;
|
||
private List<Student> stu; // 一对多关系
|
||
}
|
||
```
|
||
|
||
学生类
|
||
|
||
src/main/java/org/example/entity/Student.java
|
||
|
||
```java
|
||
package org.example.entity;
|
||
|
||
import lombok.AllArgsConstructor;
|
||
import lombok.Data;
|
||
import lombok.NoArgsConstructor;
|
||
|
||
@Data
|
||
@AllArgsConstructor
|
||
@NoArgsConstructor
|
||
public class Student {
|
||
private int sid;
|
||
private String sname;
|
||
private int sage;
|
||
private int scid;
|
||
}
|
||
```
|
||
|
||
###### mapper接口
|
||
|
||
src/main/java/org/example/dao/ClassesMapper.java
|
||
|
||
```java
|
||
package org.example.dao;
|
||
|
||
import org.example.entity.Classes;
|
||
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 接口
|
||
*/
|
||
public interface ClassesMapper {
|
||
/**
|
||
* 通过班级id查询信息
|
||
* @return
|
||
*/
|
||
public Classes showClasses(int cid);
|
||
}
|
||
```
|
||
|
||
###### mapper.xml文件
|
||
|
||
src/main/resources/mappers/ClassesMapper.xml
|
||
|
||
```xml
|
||
<mapper namespace="org.example.dao.ClassesMapper">
|
||
<resultMap id="a" type="org.example.entity.Classes">
|
||
<id property="cid" column="cid"></id>
|
||
<result property="cname" column="cname"></result>
|
||
<result property="cdesc" column="cdesc"></result>
|
||
<!-- 一对多关系 -->
|
||
<collection property="stu" ofType="org.example.entity.Student">
|
||
<result property="sid" column="sid"></result>
|
||
<result property="sname" column="sname"></result>
|
||
<result property="sage" column="sage"></result>
|
||
<result property="scid" column="scid"></result>
|
||
</collection>
|
||
</resultMap>
|
||
|
||
<select id="showClasses" resultMap="a">
|
||
select * from classes c inner join student s on c.cid=s.scid and c.cid=#{cid}
|
||
</select>
|
||
</mapper>
|
||
```
|
||
|
||
> 记得在mybatis-config.xml中配置 `<mappers>`
|
||
>
|
||
> ```xml
|
||
> <configuration>
|
||
> <mappers>
|
||
> <mapper resource="mappers/StudentMapper.xml"></mapper>
|
||
> <mapper resource="mappers/ClassesMapper.xml"></mapper>
|
||
> </mappers>
|
||
> </configuration>
|
||
> ```
|
||
|
||
###### 测试类
|
||
|
||
src/test/java/org/example/test/ClassesTest.java
|
||
|
||
```java
|
||
...
|
||
ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);
|
||
Classes classes = mapper.showClasses(1);
|
||
System.out.println(classes);
|
||
...
|
||
```
|
||
|
||
输出结果
|
||
|
||
![image-20220119152029499](张博凯的Java学习笔记.assets/image-20220119152029499.png)
|
||
|
||
|
||
|
||
##### 多对一关系
|
||
|
||
> 案例:学生(n): 班级(1)
|
||
>
|
||
> **通过学生id查询该学生所在班级的信息**
|
||
>
|
||
> ```sql
|
||
> select * from students s inner join classes c on s.scid=c.cid and s.sid=4;
|
||
> select * from student s,classes c where s.scid=c.cid and s.scid=1;
|
||
> ```
|
||
|
||
###### 创建数据库表
|
||
|
||
[同上](#一对多关系创建数据表)
|
||
|
||
###### 创建实体类
|
||
|
||
学生类
|
||
|
||
src/main/java/org/example/entity/Student.java
|
||
|
||
```java
|
||
package org.example.entity;
|
||
|
||
import lombok.Data;
|
||
import lombok.AllArgsConstructor;
|
||
import lombok.NoArgsConstructor;
|
||
|
||
@Data
|
||
@AllArgsConstructor
|
||
@NoArgsConstructor
|
||
public class Student {
|
||
private int sid;
|
||
private String sname;
|
||
private int sage;
|
||
private int scid;
|
||
private Classes classes; // 学生所在的班级
|
||
}
|
||
```
|
||
|
||
班级类
|
||
|
||
src/main/java/org/example/entity/Classes.java
|
||
|
||
```java
|
||
package org.example.entity;
|
||
|
||
import lombok.Data;
|
||
import lombok.AllArgsConstructor;
|
||
import lombok.NoArgsConstructor;
|
||
|
||
import java.util.List;
|
||
|
||
@Data
|
||
@AllArgsConstructor
|
||
@NoArgsConstructor
|
||
public class Classes {
|
||
private int cid;
|
||
private String cname;
|
||
private String cdesc;
|
||
}
|
||
```
|
||
|
||
###### mapper接口
|
||
|
||
src/main/java/org/example/dao/ClassesMapper.java
|
||
|
||
```java
|
||
package org.example.dao;
|
||
|
||
import org.example.entity.Student;
|
||
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 接口
|
||
*/
|
||
public interface StudentMapper {
|
||
/**
|
||
* 查询学生对应的班级
|
||
* @return
|
||
*/
|
||
public List<Student> showStu();
|
||
}
|
||
```
|
||
|
||
###### mapper.xml文件
|
||
|
||
src/main/resources/mappers/StudentMapper.xml
|
||
|
||
```xml
|
||
<mapper namespace="org.example.dao.StudentMapper">
|
||
<resultMap id="b" type="org.example.entity.Student">
|
||
<id property="sid" column="sid"></id>
|
||
<result property="sname" column="sname"></result>
|
||
<result property="sage" column="sage"></result>
|
||
<result property="scid" column="scid"></result>
|
||
|
||
<!-- 班级是一 -->
|
||
<association property="classes" javaType="org.example.entity.Classes">
|
||
<result property="cid" column="cid"></result>
|
||
<result property="cname" column="cname"></result>
|
||
<result property="cdesc" column="cdesc"></result>
|
||
</association>
|
||
</resultMap>
|
||
|
||
<select id="showStu" resultMap="b">
|
||
select * from student s inner join classes c on s.scidc.cid
|
||
</select>
|
||
</mapper>
|
||
```
|
||
|
||
> 记得在mybatis-config.xml中配置 `<mappers>`
|
||
>
|
||
> ```xml
|
||
> <configuration>
|
||
> <mappers>
|
||
> <mapper resource="mappers/StudentMapper.xml"></mapper>
|
||
> <mapper resource="mappers/ClassesMapper.xml"></mapper>
|
||
> </mappers>
|
||
> </configuration>
|
||
> ```
|
||
|
||
###### 测试类
|
||
|
||
src/test/java/org/example/test/StudentTest.java
|
||
|
||
```java
|
||
...
|
||
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
|
||
List<Student> students = mapper.showStu();
|
||
for(Student student:students) {
|
||
System.out.println(student.getSName() + "\t" + "对应班级:" + student.getClasses());
|
||
}
|
||
...
|
||
```
|
||
|
||
|
||
|
||
##### 多对多关系
|
||
|
||
> 案例:学生(m)----课程(n)
|
||
>
|
||
> 注意:一般对于多对多的关系都会有一个中间表,对另外两张表进行关系。
|
||
>
|
||
> ```sql
|
||
> # 查询学生时,同时查询该学生选择的课程
|
||
> # 三张表联合查询 inner join on
|
||
>
|
||
> select * from student s inner join grades g inner join course c on
|
||
> s.sid=g.sid and g.course_id=c.course_id
|
||
>
|
||
> select * from student s,grades g,course c where s.sid=g.sid and g.course_id=c.course_id
|
||
> ```
|
||
|
||
###### 数据库表设计
|
||
|
||
课程表
|
||
|
||
```sql
|
||
CREATE TABLE `course` (
|
||
`course_id` int(11) NOT NULL AUTO_INCREMENT,
|
||
`course_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
PRIMARY KEY (`course_id`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||
```
|
||
|
||
学生表
|
||
|
||
```sql
|
||
CREATE TABLE `student` (
|
||
`sid` int(11) NOT NULL AUTO_INCREMENT,
|
||
`sname` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||
`sage` int(255) DEFAULT NULL,
|
||
PRIMARY KEY (`sid`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||
```
|
||
|
||
中间表
|
||
|
||
```sql
|
||
CREATE TABLE `grades` (
|
||
`gradeId` int(11) NOT NULL AUTO_INCREMENT,
|
||
`sid` int(11) NOT NULL,
|
||
`course_id` int(11) NOT NULL,
|
||
PRIMARY KEY (`gradeId`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||
```
|
||
|
||
|
||
|
||
|
||
|
||
#### 动态SQL
|
||
|
||
|
||
|
||
### Spring框架
|
||
|
||
> 胶水框架(主要跟其他框架结合)
|
||
>
|
||
> Spring全家桶(SpringMVC,Springboot,Spring Cloud)
|
||
|
||
|
||
|
||
### SpringMVC框架
|
||
|
||
> Web框架,Servlet角色
|
||
|
||
|
||
|
||
#### Thymeleaf
|
||
|
||
> 模板引擎
|
||
|
||
|
||
|
||
## 提升
|
||
|
||
### 前后端分离
|
||
|
||
Springboot + Vue
|
||
|
||
### 微服务(架构师)
|
||
|
||
Spring Cloud + ES + Redis
|
||
|