[JAVA] [For internal use] For those assigned to the Spring Boot project (under construction)
This agenda is used to explain to employees who have been assigned to Spring Boot for the first time.
Prerequisite knowledge
- Java 8 basic syntax
- HTML5, CSS3, JavaScript(ES5)
- Servlet3.1
- HTTP GET, POST
Development environment
- Eclipse Pleiades 4.7
- Java8
- Spring Boot 1.5.10
- PostgreSQL 9.6
- superagent.js
Target
Aim to be able to:
- Add screen
- Addition of client-server processing (addition of Restful API)
- Access DB (MyBatis)
- Understanding each file
- Understanding Controller, Service, etc. corresponding to MVC
- Create and deploy jar with Maven
- Add library from Maven
- Addition of access authority processing (Spring Security)
- Understanding the idea of convention over configuration
Library description
Spring Boot
- A collection of many Spring-related libraries
- Hello World can be displayed with the minimum configuration
--Necessary settings such as log settings have been made in advance.
- Is it becoming popular?
MyBatis
- Upgraded version of O / R Mapper. IBatis
- SQL can be written not only in XML but also in annotations
Thmeleaf
- Template engine. Something like JSP.
- While JSP specifies a tag dedicated to JSP, Thymeleaf specifies it with an attribute dedicated to Thmeleaf.
--Since the browser just ignores the unrecognized attributes, does the layout break?
--Division of labor between designers and programmers
- Spring Boot recommends Thymeleaf instead of JSP
What is Maven
[For super beginners] Maven super introduction
- Build management tool
- Set to
pom.xml
- External libraries can be used with
<dependency>
.
Specific explanation
1. Explanation of folder structure
Spring and MVC
- Controller
- Service
- Model
- View
1. Add screen
procedure
- Place the HTML in the
src / main / static / template
folder
- Create a method in Controller class that returns the created HTML
SampleController.java
public ModelAndView index(ModelAndView mav) {
mav.setViewName("sample");
return mav;
}
point
- The VIEW name passed to
setViewName
does not require an extension (it does not matter if it has an extension). It seems to be able to use both JSP and Thymeleaf.
2. Add Restful API
POST API
- superagent uses
post
, send
methods
POST API (file upload)
- superagent uses
post
, field
, ... methods
Restful API
https://qiita.com/TakahiRoyte/items/949f4e88caecb02119aa
- API naming convention
- Use GET and POST properly
3. Access DB with MyBatis
MyBatis Official Website
XML
- mybatis-config.xml
src/main/resources/mybatis/mapper/*.xml
- Tags can be used to dynamically generate SQL
--
<if>
, <foreach>
, <where>
, etc.
- You probably don't need the
parameterType
attribute as it guesses the information MyBatis will receive.
DAO class
- Add
@Component
- Call methods such as
selectOne
and selectList
of sqlSession
- Spring Security
Spring Security Reference
Thymeleaf + Spring Security integration basics
SecurityConfig.java
Set the following items, etc.
- URLs that can be accessed without logging in
- URLs that cannot be accessed without the role
Thymeleaf
- You can write something like
sec: authorize =" hasRole ('ROLE_USER') "
5. How to use Maven
Settings on existing system
pom.xml
(spring-boot-starter-parent) for Spring Boot is specified by<parent>
--The version of the external library to be used is also defined.
--If you want to specify the version, specify it with <properties>
.
- Use of own library
- JavaDoc generation
Maven update in eclipse
After modifying pom.xml
, update it with" Right-click project> Maven> Update project ".
Generate jar file (for deployment)
- When you execute the
mvn install
command, a jar file will be created in the project / target
folder.
- If you want to skip the execution of the test code, run
mvn install -Dmaven.test.skip = true
.
6. Spring Boot related
application.properties
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
- File that defines external settings for Spring Boot
- If you open it in Eclipse's "Spring Property Editor", you can complete the input for the key.
- Can also be specified as an argument of the java command.
java -jar sample.jar --server.port = 8081
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
Profile
https://area-b.com/blog/2015/01/30/2316/
http://endok.hatenablog.com/entry/2016/06/12/181900
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
- A mechanism to switch the setting values for each execution environment.
- You can switch to Java class with
@Profile ("test ")
etc.
- Thymeleaf uses
@environment
to switch
https://blog.tagbangers.co.jp/ja/2015/01/18/thymeleaf-environment-properties
Settings on existing system
-
Define 3 types of profiles in the existing system
--local: Start in local environment
--test: Start on the server of the development environment
--release: Start on production server
-
Specify a profile like --spring.profiles.active = local
in the argument of java command
-
ʻApplication.properties is prepared for each profile. --ʻApplication.properties
: Default setting value.
--ʻApplication-test.properties: Settings for the test environment. --ʻApplication-release.properties
: Settings for production environment. Disable tools such as Swagger, enable cache
--ʻApplication-local.properties`: Personal settings. Assuming not committing.
Deployment method
- Run
mvn install -Dmaven.test.skip = true
with the command to generate the jar file
- Generated in the
project / target
folder
- Start the server with the java command
- There is a batch file for starting (start.bat)
Log settings for existing projects
- Adopted a combination of SLF4J (interface) and Logback (implementation)
--On older systems, there are many combinations of commons-logging (interface) and log4j (implementation).
src/main/resources/logbakc.xml
--Add individual settings to the log format prepared from the beginning
- Generate field
log
with @ Slf4j
of Lombok
- Log4jdbc was adopted to output the executed SQL (prepared value is also output) to the log.
- https://qiita.com/YAKINIKU/items/5141c7205f3c67691c92
- Output log before and after Cntroller (AOP)
JUnit
Spring @autowired
Exercise
Prerequisites
- Development environment is in place
- Add functionality to existing projects using Spring Boot
- Ajax communication uses superaget.js
Database
create_table.sql
CREATE table user (
SERIAL user_id NOT NULL,
TEXT user_name NOT NULL,
TEXT section NOT NULL,
PRIMARY KEY (user_id)
);
Add Controller class
- Check Request / Response in the browser development tool
Creating an HTML file
Create sample.html
and display the contents of sample.html
with URL /sample.html
- Refer to the value in the message file (
message.properties
)
Create GET API (1 parameter)
- Receive one number from the client with
@RequestParam
.
- On the server side, calculate the square of the received number and return it to the client
Create GET API (pass one piece of information)
- Receive one number from the client with
@RequestParam
.
- On the server side, calculate the square of the received number and return it to the client
Create GET API (pass multiple information)
- Create a bean class and receive multiple pieces of information in that class.
- On the server side, perform some processing on the received information and return it to the client
Create POST API
- Create a POST API.
--Receive multiple information
--Perform some processing on the received information and return it to the client
- Call API created in JavaScript
--Display the contents of the response on the screen
Create POST API (file upload)
- Create a POST API
--Receives one file and one string
--Save the received file
- Call API created in JavaScript
Access to database
- Add method to DAO class
- Call a method of DAO class from a method in Controller class
SELECT with ʻuser_id`
- Create a method in DAO class
--Method to get the result narrowed down by ʻuser_id`
SELECT by user name or department
- Write a WHERE clause using
<if>
, <where>
- If the information passed to SQL is null, do not narrow down
ʻUser` INSERT into table
- Write a WHERE clause using
<if>
, <where>
- If the information passed to SQL is null, do not narrow down
Store information in a hierarchical class with a single SELECT statement
- Write SQL statements using
<collection>
Maven
Modify pom.xml
to use external library
- Check Maven local repository
- Check in Eclipse project properties
Generate a jar file and start the server with the java command
- Generate jar file with mvn command without using Eclipse
- Specify the port number in the argument of the java command
- Change the profile and check that the setting values etc. are switched
Check the log
- Change the log level in logback.xml and check if the log is output / not output
Sentence problem
HTTP
- Explain the difference between HTTP GET and POST.
-Request / response header and body
Database
- Explain the features of O / R Mapper based on the advantages and disadvantages.
- You can use
#
or $
to receive information from Java. Explain the difference.
HelloWorld
Addition of Restful API
@RequestBody, @RequestParam, etc.
file upload
Introduction of MyBatis
Spring Boot Thorough Introduction Chapter 14 Sample Web System
Servlet issues
[Supplement] Comparison with Terasoluna