Today is the 132nd day for Liu Xiaoai to learn Java by herself.

Thank you for watching. Thank you.


The learning content is as follows:

  • More powerful configuration file yamL file learning, as well as the implementation principle of the startup class briefly understand.
  • SpringBoot integrates the SSM framework to build a project environment.

Mybatis has a plug-in called general mapper, single table query is very convenient to use.

I was going to write a simple explanation, but the more I wrote, the more I wrote, the more I wrote, the more I wrote, the more I wrote, the more I wrote.

Yml configuration file

In addition to properties, configuration files can also use types with a.yml or.yaml suffix, such as application.yml files.

Note: If both profiles are present, the configuration information is merged. If there are duplicate properties, use Properties.

Let’s write a YML file:


① YML configuration file

If the attribute name turns blue, it’s normal. If it doesn’t, something’s wrong. Be sure to keep in mind:

  • Attribute names have a space after the colon, don’t forget.
  • In the case of sets, each attribute is preceded by -.

② Attribute reading class

To test this, we create the User inner class and introduce the User object, which has the property girls as a collection.

Once the configuration is complete, do a test:


If the jdbcProperties property is injected, you can check whether the jdbcProperties property is successfully injected.

You can see from the console that the properties correspond one to one with the properties in the configuration file.

That’s it for the YML configuration file, and let’s briefly look at how the startup class works.

With SpringBoot, all those tedious configurations disappear. How does this work?

@ SpringBootApplication source code:


(1) @ SpringBootConfiguration

If you look at the source code for this annotation, you can see that it has an annotation @Configuration.

That is, it is essentially a configuration class, just a SpringBoot configuration class, and there can only be one in the project.

(2) @ EnableAutoConfiguration

Enable Enable automatic configuration.

A large number of libraries are configured by default within SpringBoot. How do I make these Settings automatically take effect?

Automatically configure the corresponding libraries based on imported dependencies.

(3) @ ComponentScan

ComponentScan, which is similar to the TAB that spring used to enable annotation scanning.

The scanned packages are the packages of the class and its subpackages. So typically the startup classes are placed in a package directory before the comparison.

Note: yesterday I started by putting the startup class in one package and the Controller class in another, and the startup was having problems because of this.

Second, integrate SpringMVC

This is the Controller layer related integration.

1 Configure ports and mapping paths


① Modifying a Port

As we all know, the port for Tomcat is 8080, which has always been used by default.

If you want to change it, you have to go to Tomcat to change it, which is very troublesome, but now it is easy to change port 80 in yML file, and you can access it directly in the browser.

Note: In HTTP, if you do not enter a port, the browser defaults to port 80.

2 Modify the mapping path

You can change the mapping path using servlet.path. In general, we should just use the default mapping path, but be aware that this can be changed.

If we change it, the mapping path in the Controller class must be the same as configured, or it will not be accessible.

For example, in the preceding figure, only files ending in. Do can be accessed. Therefore, configure Hello.

Note: Unlike the XML configuration, the mapping path cannot automatically remove the suffix and must be consistent.

2 Static resource access

We used to put static resources into webApp. Now we use SpringBoot without webApp. What should WE do?


Using the ResourceProperties class, we know where static resources are stored by default.

Create a static package and store it as public.

3 Configure interceptors

In either case, you need to define a custom interceptor that implements the parent HandlerInterceptor, which we learned in detail in the previous day’s notes.

So how do you configure it in SpringBoot?

The interceptor is not a common property, but a class, so this is where the Java configuration comes in, just to recap:


(1) the XML configuration

The XML configuration used in springMVC is done using the < MVC :interceptors> tag.

(2) the Java configuration

Use @Configuration to specify the Java Configuration class, implement the WebMvcConfigurer interface, and override methods.

Call the addInterceptor() method with the registry parameter and then call the addPathPatterns() method.

Use the registry to add interceptors, and then add the mapping path to intercept.

Integrate the data persistence layer

Mapper layer, also known as Dao layer related integration.

1 Connection pool configuration


① Introduce dependencies

Spring-boot-starter-jdbc, which automatically carries a connection pool called “light chaser”, so we don’t need to configure pool-related dependencies.

Of course SpringBoot doesn’t know what our database is, so we need to configure mysql dependencies.

② Configure the database information

Database four king Kong configuration, needless to say.

2 Transaction Processing

This is much more convenient than before, continue to compare:


(1) the XML configuration

Previously, transactions were configured using XML, consisting of three steps: configuring the platform-corresponding transaction manager, configuring transaction notification, and configuring AOP. All in all, it’s complicated.

② Annotation configuration

Now with SpringBoot, you can simply use @Transactional on the methods that need to start transactions.

Transaction related properties, such as read-only, timeout… Etc., can be set in annotations.

Mybatis 3 integration


(1) the XML configuration

Enable hump matching and alias. What do these two functions do? Let’s do a review:

Hump matching is used to match the aliases of tables in the database to property names in Java classes.

Aliasing means you can change the name of a class to a fully qualified name, so if you need a fully qualified name, just write the class name.

② YAML configuration file

Camel d. Aliases indicates an alias.

Remember these two words and follow the development tools to find the corresponding properties quickly.

This completes the integration of the SSM framework with SpringBoot, which is much simpler than the previous configuration.

The last

I wrote an article about the general purpose Mapper because it’s so easy to use.

This is explained in detail in another article today.

Thanks for watching.

If you can, please give it a thumbs up. Thank you.