# SpringBootExample **Repository Path**: company-test_0/SpringBootExample ## Basic Information - **Project Name**: SpringBootExample - **Description**: SpringBoot - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2021-09-23 - **Last Updated**: 2024-08-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SpringBootExample ## 分支 * [java](java.md) * [jvm](jvm.md) * [spring](spring.md) * [Mybatis](Mybatis.md) ## 参考链接 * [IDEA 2024.1 激活教程](https://www.jihuo.live/archives/1981) * [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) * [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.5.4/maven-plugin/reference/html/) * [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.5.4/maven-plugin/reference/html/#build-image) * [SpringBoot整合Swagger](https://www.cnblogs.com/byuan/p/14988295.html) * [Swagger2.9.2的NumberFormatException](https://www.jianshu.com/p/4c0f886f4468) * [Spring boot连接MYSQL–详细(小白向)](https://blog.csdn.net/qq_44127187/article/details/111246064) * [MybatisPlus方法详细使用,实现无SQL式开发](https://juejin.cn/post/7127899373245038600) * [四十五图,一万五千字!一文让你走出迷雾玩转Maven!](https://juejin.cn/post/7238823745828405308) * [SpringBoot整合Swagger2](https://blog.csdn.net/m0_62019369/article/details/128585423) * [【SpringBoot整合JWT】](https://blog.csdn.net/weixin_47314924/article/details/130756999) * [万字SpringBoot学习笔记|菜鸟版](https://juejin.cn/post/7254384256280035388) * [阿里藏经阁](https://developer.aliyun.com/ebook/index/__0_1_0_1) ## 目录 * [jenv管理java多版本](#jenv管理java多版本) * [java优化](#java优化) * [swagger](#swagger) * [发送请求unirest](#发送请求unirest) * [序列化gson](#序列化gson) --- ## jenv管理java多版本 - [windows中使用jenv管理java多版本](https://www.cnblogs.com/java-six/p/17937124) - [JEnv-for-Windows](https://github.com/FelixSelter/JEnv-for-Windows) - [oracle](https://www.oracle.com/cn/java/technologies/downloads/#jdk22-windows) 1. clone下载github JEnv-for-Windows项目 2. 设置环境变量path,并在path最前面 3. 安装jdk 4. jenv add {name} {jdkpath} 5. jenv use {name} 6. java -version验证 ```txt C:\Users\Administrator\Desktop>jenv -h "jenv list" List all registered Java-Envs. "jenv add " Adds a new Java-Version to JEnv which can be refferenced by the given name "jenv remove " Removes the specified Java-Version from JEnv "jenv change " Applys the given Java-Version globaly for all restarted shells and this one "jenv use " Applys the given Java-Version locally for the current shell "jenv local " Will use the given Java-Version whenever in this folder. Will set the Java-version for all subfolders as well "jenv link " Creates shortcuts for executables inside JAVA_HOME. For example "javac" "jenv uninstall " Deletes JEnv and restores the specified java version to the system. You may keep your config file "jenv autoscan [--yes|-y] ??" Will scan the given path for java installations and ask to add them to JEnv. Path is optional and "--yes|-y" accepts defaults. ``` ## java优化 - [这 35 个 Java 代码优化细节,你用了吗?](https://blog.csdn.net/2401_83330354/article/details/137486494) - [Java性能优化的48条+七个案例](https://blog.csdn.net/2301_82242251/article/details/137030356) - [揭秘Java单例模式:从入门到精通,一文搞定!](https://zhuanlan.zhihu.com/p/683211583) 1. Array & ArrayList & LinkedList array 数组效率最高,但容量固定,无法动态改变,ArrayList容量可以动态增长,但牺牲了效率。 随机查询尽量使用ArrayList,ArrayList优于LinkedList,LinkedList还要移动指针 添加删除的操作LinkedList优于ArrayList,ArrayList还要移动数据 ```java // Array示例 // 冒泡排序 int [] arr = {22, 44, 33, 55, 11}; for (int i = 0; i < arr.length -1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } // ArrayList示例 ArrayList arrayList = new ArrayList<>(); System.out.println("创建的ArrayList集合初始的大小是"+arrayList.size()); arrayList.add("张三"); arrayList.add("李四"); arrayList.add("王五"); int arrayLength = arrayList.size(); System.out.println("创建的ArrayList集合,并添加数据之后的大小是"+arrayLength); for(int i = 0;i linkedList = new LinkedList<>(); linkedList.add(1); linkedList.add(2); linkedList.add(3); linkedList.addFirst(4); linkedList.addFirst(5); linkedList.addLast(6); System.out.println(linkedList); System.out.println("是否出现过元素1:"+linkedList.contains(1)); //是否出现过元素1:true System.out.println("是否出现过元素4:"+linkedList.contains(7)); //是否出现过元素7:false linkedList.set(1,9); System.out.println("更新过的链表:"+linkedList); ``` 2. String、StringBuffer、Stringbuilder String:不可变字符序列,效率低,但是复用率高。 StringBuffer:可变字符序列、效率较高(增删)、线程安全 StringBuilder:可变字符序列、效率最高、线程不安全 ```java String str = "Hello"; str = str.concat(", World!"); System.out.println(str); // 输出:Hello, World! StringBuffer buffer = new StringBuffer("Hello"); buffer.append(", World!"); System.out.println(buffer.toString()); // 输出:Hello, World! StringBuilder builder = new StringBuilder("Hello"); builder.append(", World!"); System.out.println(builder.toString()); // 输出:Hello, World! ``` 3. 使用同步代码块替代同步方法 除非能确定一整个方法都是需要进行同步的,否则尽量使用同步代码块,避免对那些不需要进行同步的代码也进行了同步,影响了代码执行效率。 ```java import java.util.concurrent.locks.ReentrantLock; public class SynchronizedBlockExample { private final ReentrantLock lock = new ReentrantLock(); // 同步方法的替代:使用同步代码块 public void nonSynchronizedMethod() { // 在方法的一部分使用同步 lock.lock(); try { // 敏感区域代码 System.out.println("Thread " + Thread.currentThread().getName() + " is executing a critical section."); // 更多的代码... } finally { // 确保释放锁 lock.unlock(); } // 其他非敏感区域的代码 } public static void main(String[] args) { SynchronizedBlockExample example = new SynchronizedBlockExample(); // 启动多个线程来调用该方法 for (int i = 0; i < 5; i++) { new Thread(example::nonSynchronizedMethod, "Thread " + i).start(); } } } ``` ```java public class SynchronizedExample { // 同步方法 public synchronized void synchronizedMethod() { System.out.println("Synchronized method is running."); // 这里的代码会在同一时刻只有一个线程执行 } // 同步代码块 public void synchronizedBlock() { synchronized (this) { System.out.println("Synchronized block is running."); // 这里的代码会在同一时刻只有一个线程执行 } } public static void main(String[] args) { SynchronizedExample example = new SynchronizedExample(); // 同步方法调用 example.synchronizedMethod(); // 同步代码块调用 example.synchronizedBlock(); } } ``` 4. 程序运行过程中避免使用反射 反射是Java提供给用户一个很强大的功能,功能强大往往意味着效率不高。不建议在程序运行过程中使用尤其是频繁使用反射机制,特别是Method的invoke方法,如果确实有必要,一种建议性的做法是将那些需要通过反射加载的类在项目启动的时候通过反射实例化出一个对象并放入内存—-用户只关心和对端交互的时候获取最快的响应速度,并不关心对端的项目启动花多久时间。 ```java import java.lang.reflect.Method; public class ReflectionExample { public static void main(String[] args) { try { // 使用Class.forName加载类 Class clazz = Class.forName("com.example.MyClass"); // 创建实例 Object myClassInstance = clazz.newInstance(); // 通过方法名和参数类型获取方法 Method myMethod = clazz.getMethod("myMethodName", String.class); // 调用方法 Object returnValue = myMethod.invoke(myClassInstance, "参数值"); // 输出结果 System.out.println("方法返回值: " + returnValue); } catch (Exception e) { e.printStackTrace(); } } } ``` 5. 使用带缓冲的输入输出流进行IO操作 带缓冲的输入输出流,即BufferedReader、BufferedWriter、BufferedInputStream、BufferedOutputStream,这可以极大地提升IO效率。 ```java import java.util.*; import java.io.*; import java.math.*; public class Main{ public static void main(String[] args) throws IOException { String filename = "D:/hern.txt"; String[] str = {"这","是","H","e","r","n","!"}; File file = new File(filename); FileWriter writer = new FileWriter(file); BufferedWriter bufferwriter = new BufferedWriter(writer); for(int i = 0; i < str.length; i++) { bufferwriter.write(str[i]); bufferwriter.newLine(); } bufferwriter.close(); writer.close(); FileReader reader = new FileReader(file); BufferedReader bufferreader = new BufferedReader(reader); String s = null; int i = 0; while( (s = bufferreader.readLine()) != null ) { i++; System.out.print(s+" "); } bufferreader.close(); reader.close(); /*运行结果是: 这是 Hern 的! */ } } ``` 6. 在合适的场合使用单例 使用单例可以减轻加载的负担、缩短加载的时间、提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: * 控制资源的使用,通过线程同步来控制资源的并发访问 * 控制实例的产生,以达到节约资源的目的 * 控制数据的共享,在不建立直接关联的条件下,让多个不相关的进程或线程之间实现通信 ```java //1. 懒汉式(线程不安全) public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } //2. 饿汉式(线程安全) public class Singleton { private static final Singleton instance = new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } } //3. 双重检查锁定(线程安全) public class Singleton { private volatile static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } ``` ## swagger * [已解决:SpringBoot使用Swagger2提示Unable to infer base url错误](https://www.4spaces.org/586.html) http://localhost:8888/swagger-ui.html#/ ## 发送请求unirest * [Unirest-Java](http://kong.github.io/unirest-java/#requests) * [使用Unirest发送POST请求](https://www.jianshu.com/p/e66aa7677a1c) * [http框架--unirest介绍及使用示例](https://blog.csdn.net/penriver/article/details/118360994) ## 序列化gson * [最全Gson使用介绍,通俗易懂](https://zhuanlan.zhihu.com/p/451745696)