This article was originally a part of my paid column, but I found a lot of people paying to read it, so I decided to share it for free in the hope of spreading it to more developers who want to get started with Java quickly.
————- The following is the text —————-
In the last three articles, we’ve talked about how to get started quickly on Android, iOS, and Web front-end development. Now it’s time for the final end — the Java backend. The Java backend has a very large ecosystem and involves many complex issues, such as distributed transactions, distributed queries, microservices, high concurrency, disaster recovery, containerization, etc., involving more technology stacks and frameworks. However, we are just trying to get started quickly, and we will only learn what is necessary to get us to the actual project. So, the core knowledge we need to learn mainly includes Java, Servlet, JDBC, MySQL, Redis, Spring, MyBatis and so on. The Java basics were covered in the introductory Android development section, so I won’t repeat them here.
The development tools
I recommend using IntelliJ IDEA, which is widely regarded as one of the best Java development tools, and should be familiar to Android developers who are familiar with Android Studio. Its powerful functions and features can be said to beat Eclipse and MyEclipse, especially I like its intelligent code hint function. IDEA is thoroughly used after greatly improving the development efficiency, at present our company’s developers are also fully turned to IDEA. Here is a very detailed Chinese tutorial, which is open source on Github and updated frequently. Here is the address:
- IntelliJ-IDEA-Tutorial:github.com/judasn/Inte…
If English is better, it is recommended to directly read the official help document:
- The official documentation: www.jetbrains.com/help/idea/m…
Servlet
Servlets are the underlying technology of the Java Web. Although we don’t need to write servlets directly now because of the Spring framework, we still need to understand how they work as the underlying technology so that we can better understand the Spring framework and so on.
A Servlet is actually a Server Applet — a small program that runs on a Server and is a Web component based on Java technology. Writing a Servlet is actually writing a Java class according to the Servlet specification. It does not have a main function. It must be deployed to the Servlet container, which instantiates and calls the Servlet’s methods. Servlet containers, also known as Web containers, are mainly Tomcat and Jetty. Both have their advantages and disadvantages. Jetty is more suitable for distributed environment of public cloud, while Tomcat is more suitable for enterprise environment. Most of the time, we are still developing enterprise-level applications, so Tomcat is our first choice.
The Servlet specification has evolved to version 4.0, mainly with new SUPPORT for HTTP/2. In addition, the most important addition to version 3.0 is WebSocket support. Prior to version 3.0, servlets only supported HTTP requests. So, what do you need to learn about servlets today? This time, I will first list the core knowledge points, and then recommend some related learning resources.
- Environment configuration: it includes development environment and deployment environment. You should be familiar with the establishment of these two environments. The development environment is mainly the installation and configuration of JDK and IDEA, while the deployment environment is the installation and configuration of Web container Tomcat. Also, Java Web uses Maven widely as a build tool, so it’s important to know how to use Maven. Although Gradle is more advanced than Maven, most projects still use Maven in the Java backend.
- Life cycle: Understand the life cycle of servlets and how Servlet instances are managed by the Servlet container.
- API: Familiar with the common apis for servlets, Mainly servlets Interface, ServletConfig, ServletContext, RequestDispatcher, ServletRequest, ServletResponse, HttpServlet, HttpServletRequest, and HttpServ LetResponse, especially the last two classes, HttpServletRequest and HttpServletResponse, must be very familiar with, as they are frequently used in real development.
- Filters: Servlet filters can intercept requests and responses and do some processing. The filter is used to authenticate user requests, log records, filter or replace data, and decode or decrypt data. Multiple filters can form a filter chain.
- Listeners: Servlet listeners are used to listen for lifecycle events of ServletContext, HttpSession, and ServletRequest objects, as well as property-changing events of those objects.
- WebSocket: Note that some implementations that inherit WebSocketServlets on the web are Deprecated. There are two implementations shown in the JSR356 specification for Java WebSocket published by Oracle. One is to use the @ServerEndpoint annotation. The other is to inherit from the Javax.webSocket. Endpoint class, which is recommended. For an introduction to WebSocket development, see this article: Java WebSocket Development Getting Started with WebSocket
Support for HTTP/2 is not widely available yet, and there is very little information about it, so you can learn more about it later.
Next, I will recommend some learning resources. Head First’s First book ** Head First Servlets & JSP ** However, chapter 7/8/9 about JSP content can be skipped, after all, JSP has already passed when not used. The patterns section in Chapter 14 is well worth reading, and should be read by everyone, because when you understand what it says, you will be able to design a better system.
In addition, my Servlet entry book ** “Servlet/JSP in-depth Explanation — Web Development based on Tomcat” is also a good entry book, but too old has stopped printing, so in Amazon, JINGdong, Dangdang and other can not buy, to see can only find the electronic version of the Internet. A relatively new book called Servlet, JSP, and Spring MVC Learning Guide ** was published in late 2016, but I haven’t read it yet, so I’m not sure what to make of it.
Cainiao has a concise tutorial, in fact, is also a set of English tutorials translated from abroad, can also be a simple look, have a basic understanding. Here are the addresses:
- The Servlet tutorial: www.runoob.com/servlet/ser…
In terms of video resources, I found a special video on Servlet 3.0 features and share it with you:
- Baidu web disk link: pan.baidu.com/s/1qYn2ws0 Password: p85n
JDBC
JDBC = Java DataBase Connectivity is a set of standard Java apis for accessing databases. By calling this API, you can connect to the database, execute SQL statements, and process the data in the database. However, the JDBC API is just a set of standardized interfaces; the only thing that really connects to the database is the JDBC driver, and different databases use different JDBC drivers.
As with servlets, we don’t use the JDBC API directly for database programming because of a more convenient framework. However, The underlying framework of Hibernate and MyBatis is JDBC, so we still need to understand the basic principles of JDBC, in order to faster hand MyBatis and other frameworks. The good news is that there is so little to learn that it takes very little time to master it. Also found an online JDBC tutorial to share with you, the following is the address:
- JDBC tutorial: www.yiibai.com/jdbc/
There are only a few JDBC core interfaces and classes: Driver, DriverManager, Connection, Statement, ResultSet, and SQLException. To understand the use of these interfaces and classes, and understand the process of using JDBC, there are five simple steps:
- Register the JDBC driver;
- Establishing a connection to the database;
- Execute SQL statements;
- Obtain the resulting data;
- Clean up resources.
Also, understand transaction handling, including transaction commit and rollback. And how to batch process SQL. All of this is covered in the JDBC tutorial above, which I won’t go into. In addition, the book “Servlet/JSP In Depth: Web Development Based on Tomcat” that I introduced above also has a section devoted to JDBC, so take a look.
The database
As we know, the database is divided into relational database and non-relational database two categories. Relational databases are mainly MySQL, Oracle, DB2, PostgreSQL and so on. The standard data query language SQL is a language based on relational databases. Non-relational databases, also called Not only SQL (NoSQL), include Redis, MongoDB, and HBase. Many projects use more than one database these days, and Redis is a must-have. In addition, as the most popular relational database, MySQL is open source and easy to get started, so let’s get familiar with both databases first.
First introduce the relational database design tools, non-relational database as if there is no special design tools will not introduce. First of all, For Windows, PowerDesigner is undoubtedly the most powerful and popular. Unfortunately, it is not available for Mac. For Mac, I recommend Navicat, which is both a great relational database client and a great database modeling tool that can be exported as an SQL file.
For MySQL, at present we also do not need to study more in-depth things, such as storage engine, stored procedures, triggers, transaction isolation level, distributed transactions, locking mechanism and so on, the most basic main understanding of the following content:
- Installation configuration: Needless to say, very basic
- SQL: Familiarity with SQL is essential
- Data types: Know what data types MySQL has and what the differences are between them
- Constraints: Understand and be familiar with the use of various constraints, including primary key constraints, non-empty constraints, unique constraints, autoincrement constraints, default value constraints, etc
- Indexes: Indexes are important and must be understood, but how to use them well is also a subject of knowledge
- Functions: Know some commonly used functions, such as count, sum, avg, Max, min, now, and so on
There is no need to recommend any learning resources for these basic knowledge, just find a bunch of them on the Internet. However, for further study after mastering the basics, I recommend two books: High-performance MySQL and MySQL Technology Insider: InnoDB Storage Engine. In addition, the official document is also essential, but the content is more.
For Redis, the performance is very high because it is based on memory, and many projects use it as a cache. Although a key-value database, a value can be a String, a hash, a List, a set, a Sorted set, and so on. Recommend a novice concise tutorial, you can simply get started:
- Redis tutorial: www.runoob.com/redis/redis…
Books, recommended to see ** “Redis actual combat” **, is a very good and widely acclaimed entry-level books.
In addition, Redis has a Java library called Jedis, so you need to briefly understand how to use it.
Spring
Now, Spring has become a very large ecosystem. The Spring Framework we know is only a core project of the whole Spring ecosystem. In addition, there are many other projects. There are more than 20 projects including Spring Boot, Spring Cloud, Spring Data, Spring For Android and so on. You can see all the projects in the Official Website of Spring. However, we’ll start with just two projects: Spring Boot and the Spring Framework. The Spring Framework is the core Framework to learn, but it’s not the whole project to understand, just the basics that will sustain us until we get to the entry level.
So, let’s start with the Spring Framework, the latest version of which is Spring Framework 5.0. The Spring Framework contains more than 20 modules, Spring-core, Spring-AOP, Spring-Beans, Spring-context, Spring-JDBC, Spring-web, Spring-webMVC, spring-websocket, etc. For details, see Github projects of the Spring Framework. However, we generally do not need to introduce all modules into the project, just as needed.
To learn the Spring Framework, there are two core design ideas to master, namely Inversion of Control (IoC) and Aspect Oriented Programming (AOP). The concept of INVERSION of control is called Dependency Injection (DI). The difference is that INVERSION of control is a design idea, and DI is one of the ways to implement it. Another implementation is called a Dependency Lookup. However, most use dependency injection, which Spring uses. In addition, the Spring core also has an Inversion of Control (IoC) Container that creates the required implementation classes at run time through configuration files and using reflection. To understand these concepts, it is recommended to see a simple answer on Zhihu, very easy to understand:
- What are the benefits of Spring IoC? – Sevenvidia’s answer
AOP is also a design idea, mainly to separate some non-business logic code, such as logging, performance statistics, security control, transaction processing, exception handling, etc. Spring implements AOP internals through dynamic proxies at the bottom level, and supports AspectJ annotations at the top. There are several related concepts to understand in AOP: Aspect, Joint point, Pointcut, Advice, Weave, Introduction, Target Object, and AOP Proxy. So, to help you understand the concepts and principles of AOP, and how to use AOP in Spring, I’ve found two articles to share with you:
- AOP concept, principle, application introduction
- Evolution of AOP configuration in Spring from 1.0 to 5.0
Spring Boot makes it easier to build and develop Spring-based applications. Spring Cloud is based on Spring Boot. Spring Boot is basically a collection of libraries whose artifactId is usually prefixed with spring-boot-starter-. It is also very easy to build a project using Spring Boot. I will share two posts from The blog of Zhai Yongchao, author of the book Spring Cloud Microservices in Action:
- Spring Boot quick start
- Use Intellij’s Spring Initializr to quickly build A Spring Boot/Cloud project
For a relatively systematic introduction to Spring, my two best-reviewed books are Spring In Action (4th edition) and Mastering Spring 4.x: Enterprise Application Development in Action. Both books are based on Spring 4, and none are based on Spring 5 yet, but that doesn’t really matter for getting started. In addition, a chapter on Spring Boot is added to Mastering Spring 4.x: The Real World of Enterprise Application Development. Spring Framework can also see the official document, the content, after all, is the latest and most detailed, just too much content, it is said that more than 600 pages, so for a will is to see not over of, direct look at the official documentation, generally do not recommend beginners only advice was not very deep understanding of the specific points case can go over to have a look, to deepen understanding. The same goes for the official documentation for Spring Boot.
MyBatis
As we’ve seen before, the standard API for Java to access databases is JDBC. However, directly using the JDBC API to code the database is very tedious and extensible, so, To simplify development, Hibernate, MyBatis, SpringJDBC and other ORM frameworks are derived. So why choose MyBatis over Hibernate or SpringJDBC? Mainly because the most popular or MyBatis, in addition, MyBatis is also easy to use.
Relational Mapping: Relational Mapping, Relational Mapping, Relational Mapping, Relational Mapping, Relational Mapping, Relational Mapping, Relational Mapping, Relational Mapping Each class corresponds to a data table, each instance of the class corresponds to a record in the table, and each attribute of the class corresponds to each field in the table. Hibernate is a standard ORM framework, which establishes a complete mapping relationship between entity classes and databases. There is no need for developers to write SQL by themselves, and SQL statements are automatically generated internally. Therefore, it is difficult to optimize SQL statements for applications with a large amount of data. MyBatis is a semi-automatic ORM framework. Different from Hibernate, which provides a full set of mapping mechanism from entity classes to database tables, SQL is automatically generated. While MyBatis is only in the entity class and SQL to establish a mapping relationship, does not automatically generate SQL statements, therefore, developers need to write their own SQL. This makes it easier to optimize the SQL.
To understand the usage of MyBatis, directly read the official document can, not much content, and there is a Chinese version, the following is the link address:
- MyBatis official document: www.mybatis.org/mybatis-3/z…
Introduction to combat
In front of the content is very much, through the actual project to all the knowledge points together is really difficult, mainly is now no project will directly write Servlet and JDBC, and these two pieces of Java Web is the underlying technology, must understand thoroughly. So, we can only learn the Servlet and JDBC native parts, it is recommended to follow the book Demo to practice, the main purpose is to code those core knowledge points to form a deep understanding. Spring, MyBatis, database, can be learned through some open source projects, I found a good learning project based on Spring Boot, recommend to you:
- springboot-learning-example
Each small project is very simple, it is recommended to focus on springboot-MyBatis – Redis this small project, the most fitting we need to learn the content. Because the project is too simple, it is recommended to expand it. For example, add an extra table by yourself and add, delete, modify and check it by adding a new REST interface. Or add the user registration and login function, and use filters for unified authentication of users; Or add WebSocket support. It would be better to extend it to a relatively complete Java Web project, such as a simplified e-commerce project that doesn’t need an interface, just provide one. Once you’ve completed the complete Java Web project, you’ll be ready to get started.
Thinking and Practice
How do I configure Gradle if I use it as a build tool? How is the configuration different from Android? How to use SpringJDBC? How does it compare with MyBatis? Through the actual project to feel the difference between these knowledge points.
Scan the qr code below to follow the subscription number.