0%

配置项

  • 项目部署到linux的tomcat中,验证码在页面中刷新不了,需要修改catalina.sh,添加JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"配置

设置虚拟路径的两种方法

  • 使用tomcat自身设置虚拟路径
  1. 在tomcat的server.xml配置文件中,配置context标签

    Context标签中的相关属性:

    • path: 指定访问该web应用的URL入口,即虚拟文件目录
    • docBase:为访问的真实路径
    • reloadable:如果这个属性设为true,tomcat服务器在运行状态下会监视在WEB-INF/classes和WEB-INF/lib目录下class文件的改动, 如果监测到有class文件被更新的,服务器会自动重新加载Web应用。
  2. 如果这事使用idea启动项目,需要将Deploy applications configured in Tomcat instance勾上

  • 单独设置IDEA进行设置虚拟路径

    1. 上图的中Deploy applications configured in Tomcat instance不要勾上

    2. 选中edit configurations,点击加号按钮,选择实际路径,并在Appication context中填写虚拟路径。

设置URL请求参数长度

  • 问题描述

    post请求,本身请求参数无大小限制,但是一般的服务器像tomcat都有默认大小,如果传输的参数超过了默认大小,则会报错http post request header is too large

  • 解决办法

    在server.xml中<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> ,添加设置项maxHttpHeaderSize ="102400"

  • 四个核心概念:

    入口 entry

    输出 output

    loader

    插件 plugins

知识点

传JSON字符串+@RequestBody接收

  • 前端
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    function icheckDelete(url){
    var parms = {
    list : array //这是个数组
    };

    $.ajax({
    dataType: "JSON",
    contentType:'application/json;charset=UTF-8',//关键是要加上这行
    traditional:true,//这使json格式的字符不会被转码
    data: JSON.stringify(parms),
    type: "DELETE",
    timeout: 20000,
    url: url,
    success : function () {
    alert("删除成功!");
    },
    error : function (data){
    alert(data.responseText);
    }
    });

    }
  • 后端
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Controller:

    @RequestMapping(value = "deleteList", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@RequestBody DeleteListRequest request) throws WebTransException{

    /* 逻辑代码 */

    return "success";
    }

  • Stack is used only for execution purpose. Heap is used for storage purpose.

    Non-Static Members And Their Memory Management In Java

  • Non-Static variables and Non-Static methods are non-static components of a class. These are also called instance components of a class. Non-static components are stored inside the object memory. Each object will have their own copy of non-static components. But, static components are common to all objects of that class.

    IIB-Instance Initialization Block

  • IIB is used to initialize instance variables or non-static variables.

  • IIB blocks will not be called from the constructor in which this() statement is written as a first statement.

    super and this keywords in java

  • super class constructor is called by super() calling statement. You can’t use super() calling statement outside the constructor. By default, super() calling statement is the first statement in any constructor.

  • this() is the calling statement to same class constructor. It must be used within constructor only. If it is used, it must be the first statement in the constructor.

  • You can’t use super and this keywords in a static method and in a static initialization block even though you are referring static members.

    10 Points Every Java Programmer Should Know About final keyword in java

  • Any class or any method can be either abstract or final but not both. abstract and final are totally opposite. Because, abstract class or abstract method must be implemented or modified in the sub classes but final does not allow this. This creates an ambiguity.

  • The global variable which is declared as final and static remains unchanged for the whole execution. Because, Static members are stored in the class memory and they are loaded only once in the whole execution. They are common to all objects of the class. If you declare static variables as final, any of the objects can’t change their value as it is final. Therefore, variables declared as final and static are sometimes referred to as Constants. All fields of interfaces are referred as constants, because they are final and static by default.

    Garbage Collection And finalize() method In Java

  • Whenever you run a java program, JVM creates three threads. 1) main thread 2) Thread Scheduler 3) Garbage Collector Thread. In these three threads, main thread is a user thread and remaining two are daemon threads which run in background.

    Access Modifiers In Java

  • Class can not be a private except inner classes. Inner classes are nothing but again members of outer class. So members of a class (field, method, constructor and inner class) can be private but not the class itself.

    What Are Access And Non-Access Modifiers In Java?

    Type Casting In Java

    Please note that by using casting, data can not be modified but only type of data can be modified.

  • Primitive Casting.

    • memory size byte < short < int < long < float < double.
      • Auto Widening
      • Explicit Narrowing
  • Derived Casting

    • Auto-up Casting
      • Explicit Down Casting

        Abstraction In Java

  • Abstract classes contain abstract methods (you can refer them as ideas) so that they can be implemented in sub classes according to their requirements. They are also called as incomplete classes as they have some unimplemented abstract methods(ideas).

    Interfaces In Java

  • Interfaces in java are very much similar to abstract classes but interfaces contain only abstract methods (you can refer to them as only ideas). Abstract classes may contain both abstract methods as well as concrete methods. But interfaces must contain only abstract methods. Concrete methods are not allowed in interfaces. Therefore, Interfaces show 100% abstractness.

    Auto-Widening Vs Auto-Boxing Vs Auto-UpCasting In Java

    Exception Handing

    java.lang.Throwable is the super class of all errors and exceptions in java.

  • java.lang.Error class represents the errors which are mainly caused by the environment in which application is running. For example, OutOfMemoryError occurs when JVM runs out of memory or StackOverflowError occurs when stack overflows.

  • Where as java.lang.Exception class represents the exceptions which are mainly caused by the application itself. For example, NullPointerException occurs when an application tries to access null object or ClassCastException occurs when an application tries to cast incompatible class types.

    Arrays

  • Array can hold the references to any type of objects. It is important to note that array can contain only references to the objects, not the objects itself.

    Threads

  • Processes and threads can be diagrammatically represented as this

  • User threads are threads which are created by the application or user.

  • Daemon threads are threads which are mostly created by the JVM.

  • There are three constant fields in java.lang.Thread class related to priority of a thread. They are,

    1
    2
    3
     	MIN_PRIORITY   —> It defines the lowest priority that a thread can have and It’s value is 1.
    NORM_PRIORITY —> It defines the normal priority that a thread can have and it’s value is 5.
    MAX_PRIORITY —> It defines the highest priority that a thread can have and it’s value is 10.

    The priority of a main thread, if explicitly not set, is always 5 i.e NORM_PRIORITY.

  • avoid thread interference

    1
    2
    3
    4
    5
    6
     	By declaring the method as synchronized.
    By declaring the variables as final.
    By declaring the variable as volatile.
    By creating the immutable objects.
    By using Atomic operations.
    By restricting the access to same object by multiple threads.
  • The Logic Behind The Synchronization In Java :

    1
    2
    3
    4
    5
    6
      The synchronization in java is built around an entity called object lock or monitor. Here is the brief description about lock or monitor.
    - Whenever an object is created to any class, an object lock is created and is stored inside the object.
    - One object will have only one object lock associated with it.
    - Any thread wants to enter into synchronized methods or blocks of any object, they must acquire object lock associated with that object and release the lock after they are done with the execution.
    - The other threads which wants to enter into synchronized methods of that object have to wait until the currently executing thread releases the object lock.
    - To enter into static synchronized methods or blocks, threads have to acquire class lock associated with that class as static members are stored inside the class memory.
  • mutex

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class Shared
    {
    static void staticMethod()
    {
    synchronized (Shared.class)
    {
    //static synchronized block
    }
    }
    void NonStaticMethod()
    {
    synchronized (this)
    {
    //Non-static synchronized block
    }
    }
    void anotherNonStaticMethod()
    {
    synchronized (new Shared())
    {
    //Non-static synchronized block
    }
    }
    }
  • 10 Points-To-Remember About Synchronization In Java :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    1. You can use synchronized keyword only with methods but not with variables, constructors, static initializer and instance initializers.
    2. Constructors, Static initializer and instance initializer can’t be declared with synchronized keyword, but they can contain synchronized blocks.
    3. Both static and non-static methods can use synchronized keyword. For static methods, thread need class level lock and for non-static methods, thread need object level lock.
    4. It is possible that both static synchronized and non-static synchronized methods can run simultaneously. Because, static methods need class level lock and non-static methods need object level lock.
    5. A method can contain any number of synchronized blocks. This is like synchronizing multiple parts of a method.
    6. Synchronization blocks can be nested.
    7. Lock acquired by the thread before executing a synchronized method or block must be released after the completion of execution, no matter whether execution is completed normally or abnormally (due to exceptions).
    8. Synchronization in java is Re-entrant in nature. A thread can not acquire a lock that is owned by another thread. But, a thread can acquire a lock that it already owns. That means if a synchronized method gives a call to another synchronized method which needs same lock, then currently executing thread can directly enter into that method or block without acquiring the lock.
    9. synchronized method or block is very slow. They decrease the performance of an application. So, special care need to be taken while using synchronization. Use synchronization only when you needed it the most.
    10. Use synchronized blocks instead of synchronized methods. Because, synchronizing some part of a method improves the performance than synchronizing the whole method.
  • wait(), notify() and notifyAll()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    These methods are final methods of java.lang.Object class. That means every class in java will have these methods. Below is the method signatures of these methods.
    1. public final void wait() throws InterruptedException
    This method tells the currently executing thread to release the lock of this object and wait until some other thread acquires the same lock and notify it using either notify() or notifyAll() methods. This method throws InterruptedException if waiting thread is interrupted.
    2. public final void notify()
    This method wakes up one thread randomly that called wait() method on this object.
    3. public final void notifyAll()
    This method wakes up all the threads that called wait() method on this object. But, only one thread will acquire lock of this object depending upon the priority.

    Important Note : These three methods must be called within synchronized method or block. Any thread which calls these methods must have lock of that object.
  • Some Things-To-Remember About wait(), notify() and notifyAll() :

    • If a thread calls notify() method and more than one threads are waiting for the object lock, then only one thread will be notified randomly.
    • When a thread calls notifyAll() method on an object, it notifies all the threads which are waiting for this object lock. But, only one thread will acquire this object lock depending upon priority.
    • When you call sleep() method on a thread, thread goes to sleep with holding the object lock with it. But, if you call wait() method, thread releases the object lock and goes for sleep. This is the main difference between wait() and sleep() methods.
    • wait(), notify() and notifyAll() are final methods of java.lang.Object class not java.lang.Thread class.
    • wait(), notify() and notifyAll() – all these three methods throw IllegalMonitorStateException if the calling thread does not owns the object lock.
    • wait() method is overloaded in Object class. There are two more wait() methods available in Object class.
  • Thread Life Cycle OR Thread States In Java

    • NEW, A thread will be in this state before calling start() method.
      • RUNNABLE, A thread will be in this state after calling the start() method.
      • BLOCKED, A thread will be in this state when a thread is waiting for object lock to enter into synchronized method/block or a thread will be in this state if deadlock occurs.
      • WAITING, A thread will be in this state when wait() or join() method is called.
      • TIMED_WAITING, A thread will be in this state when thread is sleeping. i.e A thread will be in this state when sleep() or wait() with timeOut or join() with timeOut is called.
      • TERMINATED, A thread will be in this state once it finishes it’s execution.
  • 7 Things Every Java Programmer Should Know About Threads In Java

    • Runnable interface vs Thread class : I am of opinion that when multiple threads need to execute same task, then use Runnable interface. If multiple threads need to execute different tasks, then go for Thread class.
  • Difference Between wait() and sleep() Methods In Java

    • wait(): releases the lock or monitor

      • sleep(): not releases the lock or monitor

  • Extends Thread Vs Implements Runnable In Java

    From the above all findings, it is clear that “Implements Runnable” is the preferred method to create the threads in java.

  • How To Stop A Thread In Java

    • One is using boolean variable and second one is using interrupt() method. In this post, we will discuss both of these methods.
  • Difference Between notify And notifyAll In Java

    • wait(), notify() and notifyAll() must be called within synchronized method or synchronized block.
  • Difference Between BLOCKED Vs WAITING States In Java

    • There are six thread states in java. They are NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING and TERMINATED.

String

  • strings are treated as objects.
  • JVM allocates some memory specially meant for string literals. This part of the heap memory is called String Constant Pool.
    1
    2
    3
    4
    5
    6
    7
    8
    String s1 = "abc"; 
    String s2 = "xyz";
    String s3 = "123";
    String s4 = "A";
    String s5 = new String("abc");
    char[] c = {'J', 'A', 'V', 'A'};
    String s6 = new String(c);
    String s7 = new String(new StringBuffer());
  • String Constant Pool is allocated to an object depending upon it’s content. There will be no two objects in the pool having the same content.
  • ```
    “When you create a string object using string literal, JVM first checks the content of to be created object. If there exist an object in the pool with the same content, then it returns the reference of that object. It doesn’t create new object. If the content is different from the existing objects then only it creates new object.”
    1
    2
    ```
    In simple words, there can not be two string objects with same content in the string constant pool. But, there can be two string objects with the same content in the heap memory.
  • Immutability is the fundamental property of string objects. In whatever way you create the string objects, either using string literals or using new operator, they are immutable.

Generic

  • Defining Generic Class

    • Generics Work Only With Derived Types
  • Rules To Follow While Implementing Generic Interfaces

    • Only generic classes can implement generic interfaces.
      class GenericClass<T> implements GenericInterface<T>
      -A normal class can implement a generic interface if type parameter of generic interface is a wrapper class. For example, below implementation of GenericInterface is legal.
      1
      2
      3
      4
      5
      6
      interface GenericInterface<Integer>{
      //Generic interface with Integer as type parameter
      }
      class NormalClass implements GenericInterface<Integer>{
      //Normal class implementing generic interface
      }
    • Class implementing generic interface at least must have same number and same type of parameters and at most can have any number and any type of parameters.
      1
      2
      3
      class GenericClass2<T, V> implements GenericInterface<T>{
      //Class with two type parameters
      }
  • Define Methods And Constructors As Generic

    • Generic Methods:
      <type-Parameters> return_type method_name(parameter list)
    • Constructors As Generics,As we all know that constructors are like methods but without return types
  • Bounded Types

    • the syntax for declaring Bounded type parameters.
      <T extends SuperClass>
      1
      public static <T extends Number> void printNumbers(T[] t)
  • Wildcard arguments

    • The syntax for declaring this type of wildcard arguments is:GenericType<?>
    • To specify an upper bound for wildcards:GenericType<? extends SuperClass>
    • specify a lower bound for wildcard argument using super clause:GenericType<? super SubClass>
  • Type Erasure

Collection Framework

  • Collection Framework – Class Hierarchy

    • All classes and interfaces related to Collection Framework are placed in java.util package.
  • Collection Framework – Collection Interface

    • equals() and hashcode() methods in the Collection interface are not the methods of java.lang.Object class. Because, interfaces does not inherit from Object class. Only classes in java are inherited from Object class. Any classes implementing Collection interface must provide their own version of equals() and hashcode() methods or they can retain default version inherited from Object class.
  • Collection Framework – List Interface

    • the class diagram of List interface:
  • Collection Framework – The ArrayList Class

    • Default initial capacity of an ArrayList is 10.
      • the hierarchy diagram of ArrayList class.
      • Properties Of ArrayList :
        • Size of the ArrayList is not fixed. It can increase and decrease dynamically as we add or delete the elements.
          • ArrayList can have any number of null elements.
          • ArrayList can have duplicate elements.
          • As ArrayList implements RandomAccess, you can get, set, insert and remove elements of the ArrayList from any arbitrary position.
  • Differences Between Array Vs ArrayList In Java

    • Array is static in nature,ArrayList is dynamic in nature
      • when you try to add elements to ArrayList beyond its capacity, it creates the new array with increased size and copies the elements from old array to new array.
      • if adding an element requires resizing of an ArrayList, then it gets slightly slower as it involves creating a new array in the background and copying all elements from old array to new array.
      • Array can hold both primitive data types (int, float….) as well as objects. Where as ArrayList can hold only objects. If you try to insert primitive data into ArrayList, data is automatically boxed into corresponding wrapper class.
      • ArrayList to Array,toArray() method returns an array containing all elements of the ArrayList. Object[] array = list.toArray();
      • Array To ArrayList:
        • Arrays.asList()
          1
          2
          String[] array = new String[] {"ANDROID", "JSP", "JAVA", "STRUTS", "HADOOP", "JSF"};
          ArrayList<String> list = new ArrayList<String>(Arrays.asList(array));
          • Collection.addAll()
            1
            2
            3
            String[] array = new String[] {"ANDROID", "JSP", "JAVA", "STRUTS", "HADOOP", "JSF"};
            ArrayList<String> list = new ArrayList<String>();
            Collections.addAll(list, array);
          • ArrayList.addAll()
            1
            2
            3
            String[] array = new String[] {"ANDROID", "JSP", "JAVA", "STRUTS", "HADOOP", "JSF"};
            ArrayList<String> list = new ArrayList<String>();
            list.addAll(Arrays.asList(array));
          • Using streams from java 8
            1
            2
            String[] array = new String[] {"ANDROID", "JSP", "JAVA", "STRUTS", "HADOOP", "JSF"};
            List<Object> list = Arrays.stream(array).collect(Collectors.toList());
  • Remove Duplicate Elements From ArrayList

    • Using HashSet
      1
      2
      HashSet<String> set = new HashSet<String>(listWithDuplicateElements);
      ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
      • Using LinkedHashSet
        LinkedHashSet<String> set = new LinkedHashSet<String>(listWithDuplicateElements);
  • Collection Framework – The Vector Class

    • Vector class is synchronized.
      • All methods of Vector class are synchronized
      • capacity of the vector will be doubled whenever size exceeds capacity.
      • Why Not To Use Vector Class In Your Code
        • you can achieve thread safe ArrayList by using synchronizedList() method of Collections class.
  • Java Collection Framework – The LinkedList Class

    • In general terms, LinkedList is a data structure where each element consist of three things. First one is the reference to previous element, second one is the actual value of the element and last one is the reference to next element.
  • Collection Framework – The Queue Interface

    • First-In-First-Out.
      • You can’t add or get or set elements at an arbitrary position in the queues.
      • Properties Of Queue :
        • Null elements are not allowed in the queue.
          • Queue can have duplicate elements.
          • Unlike a normal list, queue is not random access. i.e you can’t set or insert or get elements at an arbitrary positions.
          • In the Queue Interface, there are two methods to obtain and remove the elements from the head of the queue. They are poll() and remove(). The difference between them is, poll() returns null if the queue is empty and remove() throws an exception if the queue is empty.
          • There are two methods in the Queue interface to obtain the elements but don’t remove. They are peek() and element(). peek() returns null if the queue is empty and element() throws an exception if the queue is empty.
  • Collection Framework – The Deque Interface

    • the hierarchy diagram of Deque interface:
    • The main advantage of Deque is that you can use it as both Queue (FIFO) as well as Stack (LIFO).
    • Deque is nothing but the double ended queue. That means, you can insert, retrieve and remove the elements from both the ends.
  • Collection Framework – The Set Interface

    • Order of elements in a set is implementation dependent. HashSet elements are ordered on hash code of elements. TreeSet elements are ordered according to supplied Comparator (If no Comparator is supplied, elements will be placed in ascending order) and LinkedHashSet maintains insertion order.
    • The HashSet internally uses HashMap to store the objects. The elements you insert in HashSet will be stored as keys of that HashMap object and their values will be a constant called PRESENT. This constant is defined as private static final Object PRESENT = new Object() in the source code of HashSet class.
    • HashSet can have maximum one null element.
    • LinkedHashSet internally uses LinkedHashMap to store it’s elements just like HashSet which internally uses HashMap to store it’s elements.
    • LinkedHashSet maintains insertion order. This is the main difference between LinkedHashSet and HashSet.
    • SortedSet elements are sorted according to supplied Comparator. If you don’t mention any Comparator while creating a SortedSet, elements will be placed in ascending order.
    • SortedSet Inserted elements must be of Comparable type and they must be mutually Comparable.
    • Elements inserted in the TreeSet must be of Comparable type and elements must be mutually comparable.
    • TreeSet internally uses TreeMap to store it’s elements just like HashSet and LinkedHashSet which use HashMap and LinkedHashMap respectively to store their elements.
  • Java Collection Framework – The Map Interface

    • Each key-value pairs of the map are stored as Map.Entry objects. Map.Entry is an inner interface of Map interface.
    • HashMap doesn’t maintain any order of elements. LinkedHashMap maintains insertion order of elements. Where as TreeMap places the elements according to supplied Comparator.
    • Default initial capacity of HashMap is 16.
    • Hashing is nothing but the function or algorithm or method which when applied on any object/variable returns an unique integer value representing that object/variable.
    • The capacity of an HashMap is the number of buckets in the hash table. The initial capacity is the capacity of an HashMap at the time of its creation. The default initial capacity of the HashMap is 24 i.e 16. The capacity of the HashMap is doubled each time it reaches the threshold. i.e the capacity is increased to 25=32, 26=64, 27=128….. when the threshold is reached.
    • HashMap internally uses an array of Entry<K, V> objects to store the data.
    • HashTable is a legacy class. It is almost considered as due for deprecation. Since JDK 1.5, ConcurrentHashMap is considered as better option than the HashTable.

JDBC

  • Java Database Connectivity API which is used by the java application to interact with the database.

    • JDBC API not directly interacts with the database. It uses JDBC driver of that database to interact with the database.
    • JDBC driver is a software component provided along with the database which is required by the JDBC API to interact with the database. Each database will have its own JDBC driver.

    In simple terms, JDBC drivers are nothing but the implementations of interfaces provided in the JDBC API (java.sql and javax.sql packages) with respect to a particular database. These implementations are bundled in a JAR file and supplied along with the database. These implementations are used by the JDBC API to interact with that database.

Big O Notations

  • Big O notations consist of O and an expression enclosed within ( ). Expression within ( ) represents the number of computations a particular algorithm has to perform to solve a given problem.
  • O(1) represents an algorithm which has to perform exact one computation to solve a problem. O(1) represents an algorithm which takes same time to execute for any size of input data. In other words, O(1) denotes an algorithm which performance is not affected by the size of the input data.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    boolean isFirstElementZero(int[] inputData)
    {
    if(inputData[0] == 0)
    {
    return true;
    }

    return false;
    }
  • O(N) represents an algorithm which has to perform ‘N’ computations to solve a problem. Where ‘N’ is the number of elements in input data. That means algorithm will take more time as number of elements in input data increases. In the other words, O(N) denotes an algorithm whose performance is directly proportional to size of the input data.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    boolean searchForElement(int[] inputData, int n)
    {
    for (int i = 0; i < inputData.length; i++)
    {
    if(inputData[i] == n)
    {
    return true;
    }
    }

    return false;
    }
  • O(N^2) denotes an algorithm which has to perform N^2 computations to solve a problem. Where N is the number of elements in input data. In the other words, O(N^2) represents an algorithm whose performance is directly proportional to square of the size of the input data. The algorithms which have nested iterations over the input data will give O(N^2) performance. Further deeper iterations over the input data will give O(N^3), O(N^4) and so on.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    boolean findDuplicates(int[] inputData)
    {
    for (int i = 0; i < inputData.length; i++)
    {
    for (int j = 0; j < inputData.length; j++)
    {
    if(inputData[i] == inputData[j] && i!=j)
    {
    return true;
    }
    }
    }

    return false;
    }
  • O(log N) represents an algorithm whose number of computations grows linearly as input data grows exponentially. i.e O(log N) represents the algorithms whose execution time grows linearly as input data grows exponentially. That means if an algorithm takes 1 second to compute 10 elements, then it will take 2 seconds to compute 100 elements, 3 seconds to compute 1000 elements and so on. The best example for O(log N) is binary search algorithm which uses divide and conquer rule to search for an element.
  • O(2N) represents an algorithm whose execution time is doubled for every extra element in the input data. For example, if an algorithm takes 4 seconds to compute 2 elements, then it will take 8 seconds to compute 3 elements, 16 seconds for 4 elements, 32 seconds for 5 elements and so on.
  • O(N!) represents an algorithm which has to perform N! computations to solve a problem. Where N is the number of elements in the input data. For example, if an algorithm takes 2 seconds to compute 2 elements, then it will take 6 seconds to compute 3 elements, 24 seconds to compute 4 elements and so on.

知识点

  • about date jdbcType

    1
    2
    #{beginDate,jdbcType=DATE} (no time of the day), or
    #{beginDate,jdbcType=TIMESTAMP} (if you need to include the time of the day).

And the Java parameter you want to apply should be of type:

1
2
3
java.sql.Date (date without time)
java.sql.Timestamp (timestamp)
java.util.Date (date and time).

  • the Greater Than/Equal Operator and the Less Than/Equal Operators:
    1
    2
    ROWNUM &gt;= 20
    ROWNUM &lt;= 20

问题解决

使用SpringMVC+Mybatis框架写项目的时候,在mapper里面的sql语句

1
2
3
<if test="updateTime != null">
and update_time &gt; #{updateTime,jdbcType=TIMESTAMP}
</if>

出错:There is no getter for property named 'updateTime' in 'class java.util.Date'

原因是:
如果将and语句中的#{属性}写在if语句中,mybatis会去对象中从它的get方法中取值,而我之前是直接传递的值,并没有封装对象。
解决办法:

 1. 把updateTime需改为date
2. 把updateTime代表的参数封装到一个对象中

问题随记

  1. Spring Boot通过Mybatis,使用mapper接口和xml配置sql,连接数据库
  • 首先在配置文件application.properties

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=***
    spring.datasource.password=***
    spring.datasource.driverClassName=com.mysql.jdbc.Driver

    #mybatis.typeAliasesPackage:为实体对象所在的包,跟数据库表一一对应
    #mybatis.mapperLocations:mapper文件的位置

    mybatis.typeAliasesPackage=com.xxx.model
    mybatis.mapperLocations=classpath:mybatis/mapper/*Mapper.xml
  • 在启动类Application.java中写上配置信息

    1
    2
    3
    4
    5
    6
    7
    8
    @SpringBootApplication
    @MapperScan("com.xxx.dao") // mybatis扫描路径,针对的是接口Mapper类
    public class DailyreportApplication{

    public static void main(String[] args) {
    SpringApplication.run(DailyreportApplication.class, args);
    }
    }
  1. mysql连接异常java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
  • 解决办法

    jdbc:mysql://yourserver:3306/yourdatabase?zeroDateTimeBehavior=convertToNull

Git修改.gitignore不生效

  • 在git中,如果想忽略某个文件,不让这个文件提交到版本库中,可以修改根目录中的.gitignore文件

    但有时候把某些目录或者文件加入忽略规则,发现并未生效

  • 未生效原因:.gitignore只能忽略那些原来没有被追踪(track)的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的

  • 解决方案:先把本地缓存删除(改变成未track状态),然后提交。

1
2
3
git rm -r --cached <要忽略的具体文件或者目录> 或者 git rm -r --cached . 
git add .
git commit -m "update .gitignore"

文件提交

  • 提交单个文件

    1
    2
    3
    git commit <file> -m "your comment" 
    git pull
    git push
  • 提交多个文件,但是需要排除指定的文件

    1
    2
    3
    git add -u
    git reset -- main/dontcheckmein.txt
    然后进行commit、pull等操作

Git fork后的分支,更新最新的源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sourcer为源项目代码
forker为fork之后的项目代码

1、找一个空的目录下签出 fork 后的代码
git clone https://github.com/sourcer/demo.git
查看 remote 信息
git remote -v

2、然后添加源项目地址(距离定义为 source)
git remote add source https://github.com/forker/demo.git
查看 remote 信息,可以看到新增两条信息
git remote -v

3、fetch 源项目
git fetch source

4、合并代码
git merge source/master

5、把合并最新的代码推送到你的fork项目上
git push origin master

比较文件,在不同版本的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# uncommited file to HEAD
git diff <path>

# uncommited file to before last commit
git diff HEAD^ -- <path>

#last commit to before last commit
git diff HEAD^ HEAD -- <path>

#difference between HEAD and n-th grandparent
git diff HEAD~n HEAD -- <path>

#Another cool feature is whatchanged command
git whatchanged -- <path>

回滚远程分支

1、本地代码回滚到上一版本

git reset –hard HEAD~1

(或者回滚到指定版本commitId)

git reset –hard commitId

2、加入-f参数,强制提交,远程端将强制跟新到reset版本

git push -f

拉取远程分支

First, fetch the remote branches:

git fetch origin

Next, checkout the branch you want. In this case, the branch we want is called “branchxyz”.

git checkout -b branchxyz origin/branchxyz

新建分支

  • To create a new branch from a branch you do NOT have checked out:

    git branch new_branch from_branch

  • To create a new branch from the branch you DO have checked out:

    git branch new_branch

  • To create and check out a new branch from the branch you DO have checked out:

    git checkout -b new_branch

  • To create and check out a new branch from a branch you do NOT have checked out:

    git checkout -b new_branch from_branch

  • To rename a branch

    git branch -m old_name new_name

删除分支

1
2
3
4
5
6
7
8
// delete branch locally
git branch -d localBranchName

// delete branch remotely
git push origin --delete remoteBranchName

//If someone else has already deleted the branch, you just do below
git fetch -p

合并策略

warning: 不建议在没有为偏离分支指定合并策略时执行pull操作。
您可以在执行下一次pull操作之前执行下面一条命令来抑制本消息:

git config pull.rebase false # 合并(缺省策略)
git config pull.rebase true # 变基
git config pull.ff only # 仅快进

您可以将 “git config” 替换为 “git config –global” 以便为所有仓库设置
缺省的配置项。您也可以在每次执行 pull 命令时添加 –rebase、–no-rebase,
或者 –ff-only 参数覆盖缺省设置。

类型

  • int变量自增 效率问题

    1
    i++;和i += 1;形成的指令数较少,且不需要操作数栈。相对而言,i = i+1;需要至少2个操作数栈深度,并且形成的指令数较多。故,i++;和i+= 1;的效率一样,并且二者的效率都优于i = i+1;

泛型

T,E,K,V,?是这样约定的:

  • ?表示不确定的 java 类型
  • T (type) 表示具体的一个java类型
  • K V (key value) 分别代表java键值中的Key Value
  • E (element) 代表Element

?和 T 的区别

img

?和 T 都表示不确定的类型,区别在于我们可以对 T 进行操作,但是对 ?不行,比如如下这种 :

1
2
3
4
5
// 可以
T t = operate();

// 不可以
?car = operate();

简单总结下:

T 是一个 确定的 类型,通常用于泛型类和泛型方法的定义,?是一个 不确定 的类型,通常用于泛型方法的调用代码和形参,不能用于定义类和泛型方法。

PECS(Producer Extends Consumer Super)原则:作为生产者提供数据(往外读取)时,适合用上界通配符(extends);作为消费者消费数据(往里写入)时,适合用下界通配符(super)。

时间

在 JDK8 以后,建议使用 DateTimeFormatter 代替 SimpleDateFormat ,因为 SimpleDateFormat 是线程不安全的,而 DateTimeFormatter 是线程安全的。当然,也可以采用第三方提供的线程安全日期格式化函数,比如 apache 的 DateFormatUtils 工具类。

**对于一个好的框架,从外部来看应该具有非常简单易于使用的 API,且 API 契约明确;从内部来看的话,其应该有一个可扩展的架构,即非常容易插入用户自定义实现,因为任何框架都不能满足所有需求。 **


  • Subject

    主体,代表了当前的用户

  • SecurityManager

    安全管理器,它管理着所有Subject,相当于springmvc中的DispatcherServlet(前端控制器)

  • Realm

    域,Shiro从Realm获取安全数据(如用户、角色、权限) ,是验证主体subject的数据源

流程如图:

流程说明

  1. 应用代码通过 Subject 来进行认证和授权,而 Subject 又委托给 SecurityManager;
  2. 我们需要给 Shiro 的 SecurityManager 注入 Realm,从而让 SecurityManager 能得到合法的用户及其权限进行判断。

从以上也可以看出,Shiro 不提供维护用户 / 权限,而是通过 Realm 让开发人员自己注入。


shiro的内部结构如图:


身份验证

principals:身份,即主体的标识属性,可以是任何东西,如用户名、邮箱等,唯一即可。

credentials:证明 / 凭证,即只有主体知道的安全值,如密码 / 数字证书等。

  • 身份认证流程


授权

授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等)。在授权中需了解的几个关键对象:主体(Subject)、资源(Resource)、权限(Permission)、角色(Role)。

  • 授权方式

    编程式、注解式、jsp/gsp标签式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1 	Α 	α 	alpha a:lf 阿尔法
2 Β β beta bet 贝塔
3 Γ γ gamma ga:m 伽马
4 Δ δ delta delt 德尔塔
5 Ε ε epsilon ep`silon 伊普西龙
6 Ζ ζ zeta zat 截塔
7 Η η eta eit 艾塔
8 Θ θ theta θit 西塔
9 Ι ι iota aiot 约塔
10 Κ κ kappa kap 卡帕
11 ∧ λ lambda lambd 兰布达
12 Μ μ mu mju 缪
13 Ν ν nu nju 纽
14 Ξ ξ xi ksi 克西
15 Ο ο omicron omik`ron 奥密克戎
16 ∏ π pi pai 派
17 Ρ ρ rho rou 肉
18 ∑ σ sigma `sigma 西格马
19 Τ τ tau tau 套
20 Υ υ upsilon ju:p`sailon 宇普西龙
21 Φ φ phi fai 佛爱
22 Χ χ chi phai 西
23 Ψ ψ psi psai 普西
24 Ω ω omega o`miga 欧米伽