1. Core configuration files

There can be only one application.yml and application.properties in a project.

(a) application. The properties

Server.servlet. context-path = /springbootCopy the code

(2) application. Yml

YML file format is a file format written by YAML Aint Markup Language (YAML Aint Markup Language). YAML is an intuitive data serialization format that can be recognized by computers and easily read by humans. It is easy to interact with scripting languages. YML files are data-centric and much more concise than the traditional XML approach.

server:
	port: 8081
	servlet:
		context-path: 
Copy the code

Obtain the customized SpringBoot fields

(1) Read from the definition configuration field via @value annotation

Configure the application. The properties

Server.servlet. context-path = /springboot school.name = binSchool websit=http://bninecoding.comCopy the code

Using the @ Value (” ${school. The name} “)

@Value(${school.name})
private String schoolName;
Copy the code

(2) Map custom configurations to objects

Notes involved:

@component // Pass this class to the Spring container for management @ConfigurationProperties(prefix = "school") @autoWiredCopy the code

Configure the application. The properties

server.servlet.context-path = /springboot

school.name = binSchool
school.websit = binSchool

abc.name = binSchool
abc.websit = binSchool
Copy the code

Create school class:

@Component @ConfigurationProperties(prefix = "school") public class School { private String name; private String websit; public String getName() { retutrn name; } public void setname(String name) { this.name = name; }... }Copy the code

Since the @Component has already loaded the school class into the Spring container after doing so, it can be injected with @autowired:

public class IndexController { @Autowired private School school; @RequestMapping(value = "/say") public @responseBody String say() { return "school.name=" + school.getName() + "---school.websit=" + school.getWebsit; }}Copy the code

SpringBoot integrated JSP

(1) Environment integration

Configure the view resolver, application.properties:

spring.mvc.view.prefix = /
spring.mvc.view.suffix = .jsp
Copy the code

(two) use JSP

Note: I have asked the students to do the front and back end, now basically do not need JSP, JSP is the back-end to do the front end of the problem, now all over the front end to do, that is, moved to Vue.

1. Configure interfaces

@RequestMapping(value = "/say")
public ModelAndView say() {
	ModelAndView mv = new ModelAndView();
	mv.addObject("message","Hello,SpringBoot");
	mv.setViewName("say");
	return mv;
}
Copy the code

2. Create a page

Visit the newly created page:

4. Integrate Mybatis

MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis eliminates almost all of the JDBC code and the work of setting parameters and fetching result sets. MyBatis can configure and map primitive types, interfaces, and Java POJOs (Plain Old Java Objects) to records in the database via simple XML or annotations.

Add Mybatis dependency, MySQL driver (omitted)

(2) Mybatis Generator automatically generates code

In the Renren-fast project, renren-fast-generate can be used to automatically generate the front and back end code.

Dao and Mapper

There are usually two methods to develop Dao with MyBatis, namely the original Dao development method and the Mapper dynamic proxy development method.

DAO layer is called Data Access layer, fully known as Data Access Object, which is a relatively low-level and basic operation. Specifically, adding, deleting, modifying and querying a certain table is specific. That is to say, a DAO must correspond to a certain table in the database one by one, which encapsulates basic operations of adding, deleting, modifying and querying. Add, delete, change and check.

The Service layer is called the Service layer, which is called the Service layer. Roughly understood, it is the re-encapsulation of one or more DAOs into a Service, so it is no longer an atomic operation, requiring transaction control.

When developing daOs using the Mapper proxy approach, programmers need to do only two things more than they would with a raw DAO:

2. Write mapper interface (equivalent to DAO interface)

Five, SpringBoot common annotations

@RestController

That’s the equivalent of @Controller on your control layer class plus @ResponseBody on all of your methods.

Means that all methods in the current control-layer class return JSON objects

@RequestMapping

@RequestMapping = @GetMapping + @PostMapping

  • PostMapping: Add data usage
  • @deletemapping: Delete data used
  • @putMapping: Used to modify data
  • @getMapping: Query data usage

Does every interface have to be added, deleted, modified and checked separately?

There is no clear requirement in the work, you can use either, usually the most used is Post

Six, RESTful

REST is an architectural style, but is not a standard. It simply presents a set of architectural concepts and design principles for client-server interaction, based on which interfaces can be simpler and more hierarchical.

Any technology can implement this idea. If an architecture conforms to REST principles, it is called a RESTFul architecture, where path uses nouns rather than verbs.

For example we are going to visit a HTTP interface: http://localhost:8080/boot/order? id=1021&status=1

Using a RESTFul style is the HTTP address is: http://localhost:8080/boot/order/1021/1

(a) the original HTTP parsing parameters written

@RequestMapping(value = "/student")
public Object student(Integer id, Integer age) {
	Student stuent = new Student();
	student.setId(id);
	student.setAge(age);
	return student;
}
Copy the code

Then access the address: localhost:8080/student? id=1001&age=23

(2) RESTful

@pathvariable: PathVariable, get the value in the path

@RequestMapping(value = "/student/detail/{id}/{age}")
public Object student(@PathVariable("id") Integer id,
					@PathVariable("age") Integer age) {
	Map<String,Object> retMap = new HashMap<>();
	retMap.put("id",id);
	retMap.put("age",age);
	return retMap;
}
Copy the code

Access address: localhost: 8080 / student/detail / 1010/28

As you can see, RESTful is a little more secure because if you’re not the developer, the caller doesn’t know what the parameters mean.

But there are problems with RESTful:

@RequestMapping(value = "/student/detail/{id}/{age}")
public Object student(@PathVariable("id") Integer id,
					@PathVariable("age") Integer age) {
	Map<String,Object> retMap = new HashMap<>();
	retMap.put("id",id);
	retMap.put("age",age);
	return retMap;
}

@RequestMapping(value = "/student/detail/{id}/{status}")
public Object student(@PathVariable("id") Integer id,
					@PathVariable("status") Integer status) {
	Map<String,Object> retMap = new HashMap<>();
	retMap.put("id",id);
	retMap.put("status", status);
	return retMap;
}
Copy the code

The above code can appear student1 and student2 request path confused mistakes, such as the request path: localhost: 8080 / student/detail / 1010/28

Because the variable name is not explicitly specified, there is no way to know whether the request is directed to id-age or id-status.

In RESTful style, methods are usually separated by their add, delete, change, check requests, for example:

// @RequestMapping(value = "/student/detail/{id}/{age}") @GetMapping(value = "/student/detail/{id}/{age}") public Object  student(@PathVariable("id") Integer id, @PathVariable("age") Integer age) { Map<String,Object> retMap = new HashMap<>(); retMap.put("id",id); retMap.put("age",age); return retMap; } /// @RequestMapping(value = "/student/detail/{id}/{status}") @DeleteMapping(value = "/student/detail/{id}/{status}") public Object student(@PathVariable("id") Integer id, @PathVariable("status") Integer status) { Map<String,Object> retMap = new HashMap<>(); retMap.put("id",id); retMap.put("status", status); return retMap; }Copy the code

If both are GET requests, change the path path according to the meaning of the request.

RESTful often occurs the above path duplication problem, the solution is as follows:

  • Modifying the request Mode
  • Modifying a path

RESTful summary

It’s basically not up to you to decide whether a project is RESTful or not. If the project is RESTful, then you should use it, and if the project is not RESTful, you can’t use it.

Seven, Redis

(A) When do you need to use Redis?

  • Does not require real-time updates but is a huge drain on database data. For example, the ranking of product sales on the website can be counted once a day, and users do not care whether it is real-time or not.
  • Data that needs to be updated in real time, but not frequently. For example, if a user has an order list, he wants to see his orders in real time, but most users don’t place orders very often.
  • Data that is heavily visited and updated frequently at one point in time. A typical example of this kind of data is the second kill, at the moment of the second kill, there may be N times the normal flow in, the system pressure will be very high. However, the cache used for this data cannot be the same as the normal cache, and this cache must be guaranteed not to be lost, otherwise there will be a big problem.

In general, Redis can be used as a caching layer for MySQL. Why is it good for MySQL to have a cache layer?

Imagine this scenario: In a multiplayer online game, ranking, direct relationship between the data relationships, queue scenario, if the direct and MySQL positive meetings, a large number of data requests may bring MySQL exhausted, even excessive request breakdown database, will lead to the whole data service interruptions, database performance bottlenecks will grip the business development; Therefore, if Redis is used to do data caching, the pressure of querying data will be greatly reduced. In this shelf, when we have data query requirements in the business layer, we first query in the Redis cache. If not, we query in the MySQL database, and update the data to Redis at the same time. When we need to insert data in the business layer, we directly request MySQL and update the Redis cache at the same time.

(2) Configure REDIS

Application. The properties configuration:

Host = 192.168.154.128 spring.redis.port = 6379 spring.redis.password = 123456Copy the code

(3) Use Redis

@controller public class StudentController {@autoWired private StudentService StudentService; @RequestMapping(value = "/put") public @ResponseBody Object put(String key, string value) { studentService.put(key,value); } @requestMapping (value = "/get") public String get() {String count = studentService.get("count");  Return "count =" + count; } } @Service public class StudentServiceImpl implements StudentService { @Autowired private RedisTemplate<Object,Object>  redisTemplate; @Override public void put(String key, String value) { redisTemplate.opsForValue().set(key,value); } @Override public String get(String key) { String count = redisTemplate.opsForValue().get(key) return count; }}Copy the code