SpringBoot ORM operates MySQL, REST interface architecture style, integrates Redis, integrates Dubbo and packages

1. Database operation

Object Relation MySQL

1.1 Steps to use

(1) Mybatis startup dependencies
Complete the automatic configuration of mybatis objects, and the objects are placed in the container.
(2) The pom.xml file specifies that the xml file in the src/main/java directory is included in the classpath
(3) Create an entity class
(4) Create a Dao interface
(5) Create a mapper file corresponding to the Dao interface, and write SQL statements in the xml file.
(6) Create a Service layer object, call the method of the dao layer, and complete the database operation.

2. Rest interface

2.1 Architecture style

How the API is organized.

A traditional style: http://localhost:8080/mytrans/addStudent?name=zhangsan&age=20
provides the access resource name addStudent on the address, and then uses the get method to pass parameters.

REST: Re presentational State Transfer , state transfer of the presentation layer .
It is an architectural style and design philosophy, not a standard.
Advantages: More concise and more layered.
Presentation layer state transfer: The presentation layer is the view layer, which is used to display resources. The results of operating resources are displayed on the page.
Status: A change in the resource.
Transfer: Resources can change.
Describe rest in one sentence: use url to represent resources, and use http actions to operate resources

GET: query
post: create
put: update
delete: delete resource

Note: In the rest interface, the request method + url must be unique. Otherwise, it is ambiguous, and the server will directly send a 500 error. Examples are as follows:

    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable(value = "stuId") Integer studentId) {
    
    
        return "student id is = " + studentId;
    }

    @GetMapping("/student/{age}")
    public String queryStudentByAge(@PathVariable(value = "age") Integer age) {
    
    
        return "student age is = " + age;
    }

2.2 RESTful annotations

2.2.1 @PathVariable

get data from url

    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable(value = "stuId") Integer studentId) {
    
    
        return "student id is = " + studentId;
    }

2.2.2 @GetMapping、@PostMapping、@PutMapping和@DeleteMapping

@GetMapping: Support the get request method, equivalent to @RequestMapping(method = RequestMethod.GET)
@PostMapping: support the post request method, equivalent to @RequestMapping(method = RequestMethod.POST)
@PutMapping: support the put request method, equivalent to @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping: support the delete request method, equivalent to @RequestMapping(method = RequestMethod.DELETE)

2.2.3 @RestController

Conforming to the annotation, it is a combination of @Controller and @ResponseBody.
With @RestController, all methods in the current class use ResponseBody

3. SpringBoot integrates Redis

RedisTemplate: Essentially used by lettucethe client.

3.1 StringRedisTemplate 和 RedisTemplate

StringRedisTemplate: kv are all String, using the serialization of String, good readability.
RedisTemplate: The kv is serialized and stored in redis. kv is serialized content and cannot be directly identified. The serialization mechanism of jdk used by default.

RedisTemplate can modify serializer

    public String addString(String k, String v) {
    
    
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        redisTemplate.opsForValue().set(k, v);
    }

4. SpringBoot integrates Dubbo

Dubbo is a framework based on the RPC protocol.

5. Spring Boot packaging

5.1 war package

(1) Modify the pom.xml file

  • Specify the name of the package
<build>
	<finalName>myboot</finalName>
</build>
  • Specify the jsp compilation directory
  • Specify the packaging type as war
<packaging>war</packaging>

(2) The main startup class inherits SpringBootServletInitializer
to expose the Application class, so that an independent tomcat server can be used

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    
    
        return builder.sources(Application.class);
    }
}

(3) Deployment
Put the war in the release directory of servers such as tomcat.

5.2 jar package

(1) Modify the pom.xml file

  • Specify the name of the package
<build>
	<finalName>myboot</finalName>
</build>
  • Specify the version of the springboot maven plugin
<plugins>
	<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<version>1.4.2-RELEASE</version>
	</plugin>
</plugins>

(2) execute mave package

  • Will generate myboot.jar package

5.3 The difference between war package and jar package

war: the ability to use real containers
jar: convenient, no need to use additional containers.

Guess you like

Origin blog.csdn.net/kaikai_sk/article/details/126704824