mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-06-08 10:47:13 +00:00
feat: restructure docs into "chapters" (#12)
* feat(docker, k8s): create containers folder and kubernetes notes
This commit is contained in:
parent
b1cb858508
commit
2725e3cb70
92 changed files with 777 additions and 367 deletions
115
docs/languages/java/dao.md
Normal file
115
docs/languages/java/dao.md
Normal file
|
@ -0,0 +1,115 @@
|
|||
# Database Access Object
|
||||
|
||||
## DB
|
||||
|
||||
Connection to the DB.
|
||||
|
||||
```java
|
||||
package dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DB {
|
||||
|
||||
private Connection conn; // db connection obj
|
||||
|
||||
private final String URL = "jdbc:<dbms>:<db_url>/<database>";
|
||||
private final String USER = "";
|
||||
private final String PWD = "";
|
||||
|
||||
public DB() {
|
||||
this.conn = null;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
try {
|
||||
this.conn = DriverManager.getConnection(URL, USER, PWD);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
if(conn != null) {
|
||||
try {
|
||||
this.conn.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConn() {
|
||||
return conn;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## `I<Type>DAO`
|
||||
|
||||
Interface for CRUD methods on a database.
|
||||
|
||||
```java
|
||||
package dao;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public interface I<Type>DAO {
|
||||
|
||||
String RAW_QUERY = "SELECT * ..."
|
||||
public Type Query() throws SQLException;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## `<Type>DAO`
|
||||
|
||||
Class implementing `I<Type>DAO` and handling the communication with the db.
|
||||
|
||||
```java
|
||||
package dao;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class <Type>DAO implements I<Type>DAO {
|
||||
|
||||
private <Type> results;
|
||||
|
||||
private Statement statement; // SQL instruction container
|
||||
private ResultSet rs; // Query results container
|
||||
private DB db;
|
||||
|
||||
public <Type>DAO() {
|
||||
db = new DB();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type Query() throws SQLException {
|
||||
// 1. connection
|
||||
db.connect();
|
||||
|
||||
// 2. instruction
|
||||
statement = db.getConn().createStatement(); // statement creation
|
||||
|
||||
// 3. query
|
||||
rs = statement.executeQuery(RAW_QUERY); // set the query
|
||||
|
||||
// 4. results
|
||||
while(rs.next()) {
|
||||
// create and valorize Obj
|
||||
// add to results
|
||||
}
|
||||
|
||||
// 5. disconnection
|
||||
db.disconnect()
|
||||
|
||||
// 6. return results
|
||||
return results;
|
||||
}
|
||||
}
|
||||
```
|
122
docs/languages/java/java-collection-framework.md
Normal file
122
docs/languages/java/java-collection-framework.md
Normal file
|
@ -0,0 +1,122 @@
|
|||
# Java Collection Framework - JCF
|
||||
|
||||
All classes that permit the handling of groups of objects constitute the Java Collection Framework.
|
||||
|
||||
A Collection is a *container* in which several objects are grouped in a *single entity*.
|
||||
|
||||
The **Java Collection Framework** is constituted by:
|
||||
|
||||
- **Interfaces** the define the operations of a generic collection. They can be split into two categories:
|
||||
- **Collection**: used to optimize operations of insertion, modification and deletion of elements in a group of objects.
|
||||
- **Map**: optimized for look-up operations.
|
||||
- **Classes** that implement the interfaces using different data structures.
|
||||
- **Algorithms** consisting in methods to operate over a collection.
|
||||
|
||||

|
||||
|
||||
## java.util.Collections
|
||||
|
||||
### Collection Functions
|
||||
|
||||
```java
|
||||
boolean add (Object o) e.g., <x>.add (<y>) //append to collection, false if fails
|
||||
boolean add (int index, Object o) //insertion at given index
|
||||
boolean addAll (Collection c) //appends a collection to another
|
||||
void clear() //remove items from container
|
||||
boolean contains (Object o) //true if object is in collection
|
||||
boolean containsAll (Collection c) //true if all items of collection are in another
|
||||
boolean isEmpty (Object o) e.g., if (<x>.isEmpty()) ... //true if collection is empty
|
||||
boolean remove (Object o) //remove object from collection
|
||||
Object remove (int index) //remove object at given index
|
||||
void removeAll (Collection c) //remove all items form collection
|
||||
int size () //number og items in collection
|
||||
Object [] toArray() //transform collection in array
|
||||
Iterator iterator() //returns iterator to iterate over the collection
|
||||
```
|
||||
|
||||
### Collections Methods
|
||||
|
||||
```java
|
||||
Collection<E>.forEach(Consumer<? super T> action);
|
||||
```
|
||||
|
||||
### Iterator
|
||||
|
||||
Abstracts the problem of iterating over all the elements of a collection;
|
||||
|
||||
- `public Iterator (Collection c)` creates the Iterator
|
||||
- `public boolean hasNext()` checks if there is a successive element
|
||||
- `public Object next()` extracts the successive element
|
||||
|
||||
### ArrayList
|
||||
|
||||
**Note**: ArrayLists can't contain *primitive* values. *Use wrapper classes* instead.
|
||||
|
||||
```java
|
||||
import java.util.ArrayList;
|
||||
ArrayList<Type> ArrayListName = new ArrayList<Type>(starting_dim); //resizable array
|
||||
ArrayList<Type> ArrayListName = new ArrayList<Type>(); //resizable array
|
||||
ArrayList<Type> ArrayListName = new ArrayList<>(); //resizable array (JAVA 1.8+)
|
||||
|
||||
|
||||
ArrayListName.add(item); //append item to collection
|
||||
ArrayListName.add(index, item); // add item at position index, shift all item from index and successive towards the end af the ArrayList
|
||||
ArrayListName.set(index, item); // substitute EXISTING item
|
||||
ArrayListName.get(index); //access to collection item
|
||||
ArrayListName.remove(item) //remove first occurrence of item from collection
|
||||
ArrayListName.remove(index) //remove item at position index
|
||||
ArrayListName.clear() //empties the ArrayList
|
||||
ArrayListName.contains(object); // check if object is in the ArrayList
|
||||
ArrayListName.IndexOf(object); // returns the index of the object
|
||||
ArrayListName.isEmpty(); // check wether the list is empty
|
||||
|
||||
ArrayListName.size(); //dimension of the ArrayList
|
||||
ArrayListName.tirmToSize(); // reduce ArrayList size to minimum needed
|
||||
// ArrayList size doubles when a resize is needed.
|
||||
|
||||
//run through to the collection with functional programming (JAVA 1.8+)
|
||||
ArrayListName.forEach(item -> function(v));
|
||||
```
|
||||
|
||||
### Collection Sorting
|
||||
|
||||
To sort a collection it's items must implement `Comparable<T>`:
|
||||
|
||||
```java
|
||||
class ClassName implements Comparable<ClassName> {
|
||||
|
||||
@override
|
||||
public int compareTo(Classname other){
|
||||
//compare logic
|
||||
return <int>;
|
||||
}
|
||||
}
|
||||
|
||||
List<ClassName> list;
|
||||
//valorize List
|
||||
Collections.sort(list); //"natural" sorting uses compareTo()
|
||||
```
|
||||
|
||||
Otherwise a `Comparator()` must be implemented:
|
||||
|
||||
```java
|
||||
class Classname {
|
||||
//code here
|
||||
}
|
||||
|
||||
// Interface object (!) implements directly a method
|
||||
Comparator<ClassName> comparator = new Comparator<ClassName>() {
|
||||
|
||||
@Override
|
||||
public int compare(ClassName o1, Classname o2) {
|
||||
//compare logic
|
||||
return <int>;
|
||||
}
|
||||
};
|
||||
|
||||
List<ClassName> list;
|
||||
//valorize List
|
||||
Collections.sort(list, comparator); //"natural" sorting uses compareTo()
|
||||
```
|
||||
|
||||
`Comparator<T>` and `Comparable<T>` are functional interfaces
|
1229
docs/languages/java/java.md
Normal file
1229
docs/languages/java/java.md
Normal file
File diff suppressed because it is too large
Load diff
45
docs/languages/java/spring/pom.xml.md
Normal file
45
docs/languages/java/spring/pom.xml.md
Normal file
|
@ -0,0 +1,45 @@
|
|||
# pom.xml
|
||||
|
||||
File specifing project dependencies.
|
||||
|
||||
```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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>major.minor.patch</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>major.minor.patch.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<groupId>base_package</groupId>
|
||||
<artifactId>Project_Name</artifactId>
|
||||
<version>major.minor.patch</version>
|
||||
<name>Project_Name</name>
|
||||
<description>...</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
```
|
203
docs/languages/java/spring/spring-project.md
Normal file
203
docs/languages/java/spring/spring-project.md
Normal file
|
@ -0,0 +1,203 @@
|
|||
# Spring Project
|
||||
|
||||
## Libs
|
||||
|
||||
- MySql Driver
|
||||
- Spring Data JPA (data persistance)
|
||||
- Spring Boot Dev Tools
|
||||
- Jersey (Serializzazione)
|
||||
- Spring Web
|
||||
|
||||
## application.properties
|
||||
|
||||
```ini
|
||||
spring.datasource.url=DB_url
|
||||
spring.datasource.username=user
|
||||
spring.datasource.password=password
|
||||
|
||||
spring.jpa.show-sql=true
|
||||
|
||||
server.port=server_port
|
||||
```
|
||||
|
||||
## Package `entities`
|
||||
|
||||
Model of a table of the DB
|
||||
|
||||
```java
|
||||
package <base_package>.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity // set as DB Entity (DB record Java implementation)
|
||||
public class Entity {
|
||||
|
||||
@Id // set as Primary Key
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) // id is autoincremented by the DB
|
||||
private int id;
|
||||
|
||||
// no constructor (Spring requirement)
|
||||
|
||||
// table columns attributes
|
||||
|
||||
// getters & setters
|
||||
|
||||
// toString()
|
||||
}
|
||||
```
|
||||
|
||||
## Package `dal`
|
||||
|
||||
Spring Interface for DB connection and CRUD operations.
|
||||
|
||||
```java
|
||||
package <base_package>.dal // or .repository
|
||||
|
||||
import org.springframework.data.repository.JpaRepository;
|
||||
import org.springframework.data.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
// interface for spring Hibernate JPA
|
||||
// CrudRepository<Entity, PK_Type>
|
||||
public interface IEntityDAO extends JpaRepository<Entity, Integer> {
|
||||
|
||||
// custom query
|
||||
@Query("FROM <Entity> WHERE param = :param")
|
||||
Type query(@Param("param") Type param);
|
||||
}
|
||||
```
|
||||
|
||||
## Package `services`
|
||||
|
||||
Interfaces and method to access the Data Access Layer (DAL).
|
||||
|
||||
In `IEntityService.java`:
|
||||
|
||||
```java
|
||||
package <base_package>.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import <base_package>.Entity;
|
||||
import <base_package>.IEntityRepository;
|
||||
|
||||
// CRUD method implemented on Entity
|
||||
public interface IEntityService {
|
||||
|
||||
String FIND_ALL = "SELECT * FROM <table>"
|
||||
String FIND_ONE = "SELECT * FROM <table> WHERE id = ?"
|
||||
|
||||
List<Entity> findAll();
|
||||
Entity findOne(int id);
|
||||
void addEntity(Entity e);
|
||||
void updateEntity(int id, Entity e);
|
||||
void deleteEntity(Entity e);
|
||||
void deleteEntity(int id);
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
In `EntityService.java`:
|
||||
|
||||
```java
|
||||
package <base_package>.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import <base_package>.entities.Entity;
|
||||
import <base_package>.repos.ILibriRepository;
|
||||
|
||||
// implementation of IEntityService
|
||||
@Service
|
||||
public class EntityService implements IEntityService {
|
||||
|
||||
@Autowired // connection to db (obj created by spring as needed: Inversion Of Control)
|
||||
private IEntityDAO repo;
|
||||
|
||||
@Override
|
||||
public List<Entity> findAll() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity findOne(int id) {
|
||||
return repo.findById(id).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addEntity(Entity e) {
|
||||
repo.save(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateEntity(int id, Entity e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteEntity(Entity e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteEntity(int id) {
|
||||
}
|
||||
|
||||
// custom query
|
||||
Type query(Type param) {
|
||||
return repo.query(param);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Package `integration`
|
||||
|
||||
REST API routes & endpoints to supply data as JSON.
|
||||
|
||||
```java
|
||||
package <base_package>.integration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import <base_package>.entities.Entity;
|
||||
import <base_package>.services.IEntityService;
|
||||
|
||||
@RestController // returns data as JSON
|
||||
@RequestMapping("/api") // controller route
|
||||
public class EntityRestCtrl {
|
||||
|
||||
@Autowired // connection to service (obj created by spring as needed: Inversion Of Control)
|
||||
private IEntityService service;
|
||||
|
||||
@GetMapping("entities") // site route
|
||||
public List<Entity> findAll(){
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/entities/{id}")
|
||||
public Entity findOne(@PathVariable("id") int id) { // use route variable
|
||||
return service.findOne(id);
|
||||
}
|
||||
|
||||
@PostMapping("/entities")
|
||||
public void addEntity(@RequestBody Entity e) { // added entity is in the request body
|
||||
return service.addEntity(e)
|
||||
}
|
||||
|
||||
// PUT / PATCH -> updateEntity(id, e)
|
||||
|
||||
// DELETE -> deleteEntity(id)
|
||||
}
|
||||
```
|
51
docs/languages/java/web/servlet.md
Normal file
51
docs/languages/java/web/servlet.md
Normal file
|
@ -0,0 +1,51 @@
|
|||
# Servlet
|
||||
|
||||
A Java servlet is a Java software component that extends the capabilities of a server.
|
||||
Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web applications on web servers and thus qualify as a server-side servlet web API.
|
||||
|
||||
## Basic Structure
|
||||
|
||||
```java
|
||||
package <package>;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebServlet("/route")
|
||||
public class <className> extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** handle get request */
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
// GET REQUEST: load and display page (forward to JSP)
|
||||
// OPTIONAL: add data to request (setAttribute())
|
||||
|
||||
}
|
||||
|
||||
/** handle post request */
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
// POST REQUEST: add stuff to DB, page, ...
|
||||
|
||||
doGet(request, response); // return same page with new content added (default case)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Servlet Instructions
|
||||
|
||||
```java
|
||||
request.getParameter() // read request parameter
|
||||
|
||||
response.setContentType("text/html"); // to return HTML in the response
|
||||
|
||||
response.getWriter().append(""); //append content to the http response
|
||||
|
||||
request.setAttribute(attributeName, value); // set http attribute of the request
|
||||
request.getRequestDispatcher("page.jsp").forward(request, response); // redirect the request to another page
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue