diff --git a/notes/Java_单例设计模式详解.md b/notes/Java_单例设计模式详解.md deleted file mode 100644 index ac6e770..0000000 --- a/notes/Java_单例设计模式详解.md +++ /dev/null @@ -1,193 +0,0 @@ -# Java 单例设计模式详解 - -参考自慕课网课程:[Java设计模式精讲](https://coding.imooc.com/class/270.html):star::star::star::star::star: - -#### 1、单例模式的饿汉模式(线程安全) - -缺点:在类初始化时候就会初始化对应的单例类,如果单例类没有被用到且单例类初始化复杂,就不应用这种模式 - -```java -public class HungrySingleton { - - private static final HungrySingleton hungrySingleton; - - private HungrySingleton(){ - // 如果不加上这个判断,就会被反射攻击 - if (hungrySingleton!=null){ - throw new RuntimeException("单例模式禁止反射调用!"); - } - } - - static { - hungrySingleton=new HungrySingleton(); - } - - public static HungrySingleton getInstance(){ - return hungrySingleton; - } -} -``` - -```java -// 反射攻击 -public class ReflectAttack { - - public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - // 反射攻击 - Class hungrySingletonClass = HungrySingleton.class; - Constructor constructor = hungrySingletonClass.getDeclaredConstructor(); - // 获取可操作权限 - constructor.setAccessible(true); - HungrySingleton hungrySingleton = constructor.newInstance(); - HungrySingleton instance = HungrySingleton.getInstance(); - System.out.println(hungrySingleton==instance); - } -} -``` - - - -#### 2.单例模式的懒汉模式(线程不安全) - -```java -public class LazySingleton { - - private static LazySingleton lazySingleton=null; - - private LazySingleton(){ - if (lazySingleton!=null){ - throw new RuntimeException("单例模式禁止反射调用!"); - } - } - - // 在多线程下是不安全的 - public static LazySingleton getInstance(){ - if (lazySingleton!=null){ - lazySingleton=new LazySingleton(); - } - return lazySingleton; - } -} -``` - - - -#### 3.单例模式的懒汉模式(采用双重检查锁,线程安全) - -```java -public class LazySingleton { - - // 必须要声明为 volatile 防止指令重排序 - private static volatile LazySingleton lazySingleton = null; - - private LazySingleton() { - if (lazySingleton != null) { - throw new RuntimeException("单例模式禁止反射调用!"); - } - } - - public static LazySingleton getInstance() { - if (lazySingleton == null) { - synchronized (LazySingleton.class) { - if (lazySingleton == null) { - /* - new对象过程: - 1.分配内存给这个对象 - 2.初始化对象 - 3.设置lazyDoubleCheckSingleton 指向刚分配的内存地址 - 上述三步会发生指令重排序 - */ - lazySingleton = new LazySingleton(); - } - } - } - return lazySingleton; - } -} -``` - - - -#### 4.使用枚举实现单例(推荐 JVM保证下的单例) - -**能够防止反射攻击和序列化对单例的破坏** - -```java -// 枚举单例模式 -public enum EnumInstance { - - INSTANCE; - - public static EnumInstance getInstance() { - return INSTANCE; - } -} -``` - -```java -// 测试 -public class Test { - - public static void main(String[] args) { - EnumInstance instance0 = EnumInstance.INSTANCE; - EnumInstance instance1 = EnumInstance.getInstance(); - EnumInstance instance2 = EnumInstance.getInstance(); - System.out.println(instance0 == instance1); // true - System.out.println(instance0 == instance2); // true - System.out.println(instance1 == instance2); // true - } -} - -``` - -```java -public enum EnumInstance { - - INSTANCE; - - private String data; - - // 可以增加对应的方法 - public void setData(String data) { - this.data = data; - } - - public String getData() { - return data; - } - - public static EnumInstance getInstance() { - return INSTANCE; - } -} -``` - - - -#### 5.容器式单例(不常用) - -```java -public class ContainerSingleton { - - private ContainerSingleton(){ - } - - private static Map singletonMap = new HashMap(); - - public static void putInstance(String key,Object instance){ - if(StringUtils.isNotBlank(key) && instance != null){ - if(!singletonMap.containsKey(key)){ - singletonMap.put(key,instance); - } - } - } - - public static Object getInstance(String key){ - return singletonMap.get(key); - } - - -} - -``` - diff --git a/notes/Java_反射.md b/notes/Java_反射与注解.md similarity index 100% rename from notes/Java_反射.md rename to notes/Java_反射与注解.md diff --git a/notes/Oracle和MySQL异同对比.md b/notes/Oracle_与_MySQL_异同对比.md similarity index 81% rename from notes/Oracle和MySQL异同对比.md rename to notes/Oracle_与_MySQL_异同对比.md index 106acc0..942835d 100644 --- a/notes/Oracle和MySQL异同对比.md +++ b/notes/Oracle_与_MySQL_异同对比.md @@ -1,112 +1,30 @@ -# oracle和mysql 知识点总结和异同对比 - -## 目录
-一、数据库管理
-    1.1 用户管理
-        1.1.1 mysql用户、权限管理
-        1.1.2 oracle 用户、角色、权限管理
-二、DQL 语句
-    2.1 基础查询
-        1.常量查询的区别:
-        2.字符串拼接
-        3.判断字段是否为空
-        4.查询非空字段
-    2.2 常见函数
-        1.字符函数
-        2.数学函数
-        3.日期函数
-        1. oracle 日期函数:
-            1.1 oracel常用的时间格式掩码
-            1.2 Oracle 获取当前年、月、日
-            1.3 计算两个时间差
-            1.4 日期和字符串转换
-            1.5 日期的加减计算
-        2.mysql日期函数
-            2.1 mysql 常用日期格式掩码
-            2.2 Oracle 获取当前年、月、日
-            2.3 计算两个时间之差
-            2.4 日期和字符串转换
-            2.5 日期的加减计算
-        4、流程控制函数
-            4.1 oracle decode函数
-            4.2 mysql流程控制函数
-        5.分页查询
-三、DML语句
-    1. 插入语句
-            1.1 mysql 多行插入
-            1.2 oracle 多行插入
-        2.多表更新
-            2.1 mysql 多表更新
-            2.2 oracle 多表更新
-        3. 级联删除
-            3.1 mysql 级联删除
-            3.2 oracle 级联删除
-四、DDL语句
-    4.1 数据库操作
-        4.1 mysql数据库的创建、修改和删除
-        4.2 oracle 数据库的创建和删除
-    4.2 表的管理
-        4.2.1 创建表
-            1. oracle 字段类型
-            2. mysql 字段类型
-        4.2.2 修改表
-        4.2.3 删除表
-        4.2.4 复制表
-    4.3 约束
-        4.3.1 常见的约束
-        4.3.2 创建表时添加约束
-        4.3.3 修改表时添加或删除约束
-        4.3.4 自增长列(mysql)
-        4.3.5 序列(oracle)
-        4.3.6 级联操作
-五、TCL语言
-    5.1 事务
-        5.1.1 事务隔离级别
-六、其他扩展
-    6.1 视图
-    6.2 触发器
-    6.3 存储过程
-        6.3.1 创建存储过程
-        6.3.2变量
-        6.3.3 存储过程的查询
-        6.3.4 存储过程的修改
-        6.3.5 存储过程的删除
-        6.3.6 条件语句
-            1.分支结构
-            2.循环结构
-    6.4 游标
-        6.4.1 游标简介
-        6.4.2 游标的特性
-        6.4.3 游标的操作
- -## 正文
+# Oracle 和 MySQL 异同对比 ## 一、数据库管理 -### 1.1 用户管理 +### 1.1 MySQL 用户、权限管理 -#### 1.1.1 mysql用户、权限管理 - -**1.创建、修改、删除用户** +#### 1. 创建、修改、删除用户 ```mysql -- 创建用户 CREATE USER '用户名' IDENTIFIED BY '密码'; -- 重命名 -RENAME USER '用户名' TO '新用户名'; +RENAME USER '用户名' TO '新用户名'; + +-- 修改自己的密码 +SET PASSWORD=Password('新密码') + +-- 其他用户的口令 +SET PASSWORD FOR 用户名 = Password('新密码') --- 修改用户口令 - -- 自己的口令 - SET PASSWORD=Password('新密码') - -- 其他用户的口令 - SET PASSWORD FOR 用户名 = Password('新密码') -- 删除用户 -DROP USER '用户名'; +DROP USER '用户名'; ``` -**2.用户权限管理** +#### 2. 用户权限管理 ```mysql -- 查看用户权限 @@ -119,19 +37,17 @@ GRANT privileges ON [权限范围] TO '用户名'; REVOKE privileges ON [权限范围] TO '用户名'; ``` -**注:修改完权限以后 一定要刷新服务,或者重启服务,刷新服务用:FLUSH PRIVILEGES** +注:权限修改完成后需要使用 FLUSH PRIVILEGES 命令刷新服务,或者重启服务。 +#### 3.权限级别范围 - -**3.权限级别范围** - -MySQL grant 权限,分别可以作用在多个层次上。 +MySQL 的权限授予可以作用在多个层次上: ```mysql -- 1. grant 作用在整个 MySQL 服务器上: -grant select on . to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。 +grant select on . to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表 grant all on . to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库 -- 2. grant 作用在单个数据库上: -grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。 +grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表 -- 3. grant 作用在单个数据表上: grant select, insert, update, delete on testdb.orders to dba@localhost; -- 4. grant 作用在表中的列上: @@ -142,7 +58,7 @@ grant execute on function testdb.fn_add to ’dba’@’localhost’ ``` -**4.可以赋予的权限(privileges)列表:** +**4.可以赋予的权限 (privileges) 列表 :** - ALTER: 修改表和索引。 - CREATE: 创建数据库和表。 @@ -162,7 +78,7 @@ grant execute on function testdb.fn_add to ’dba’@’localhost’ -#### 1.1.2 oracle 用户、角色、权限管理 +### 1.2 Oracle 用户、角色、权限管理 **1、创建用户**