Spring boot advanced tutorial spring boot connecting to MySQL database

Spring boot can greatly simplify the persistence task without writing SQL statements. In the previous chapter "spring boot construction framework", we created a new spring boot application. This chapter establishes a connection with the database in the original project.

Spring boot has two methods to establish a connection with the database. One is to use jdbctemplate and the other is to integrate mybatis. The following describes how to integrate and use these two methods.

1. Use jdbctemplate

Add application. In the resource folder Properties configuration file and enter database parameters as follows:

Create a new controller class to test the database connection. The examples are as follows:

Run app to input address and output database data.

2. Integrate mybatis

Add mybatis dependency in POM The following is added to the XML file:

Add application. In the resource folder Properties configuration file and enter database parameters as follows:

Add mapper's interface class and XML file in turn. The classes are as follows: appmessagemapper java

AppMessageMapper. xml

AppMessage. java

AppMessageService. java

APPMessageController. java

Problem description?

The springboot scan package prompts that the mapper cannot be found. Exception information: consumer defining a bean of type in your configuration

Analyze the cause

The default rule for bean assembly of spring boot project is to scan from top to bottom according to the package location of application class. "Application class" refers to the entry class of spring boot project. If the package of the application class is: com yoodb. Blog, only com.com will be scanned yoodb. Blog package and all its sub packages, if the package of service or Dao is not in com yoodb. Blog and its sub packages will not be scanned.

resolvent

Method 1: use the annotation @ componentscan (value = "com. Yoodb. Blog"), where com yoodb. Blog is the package path. Method 2: put the startup class application in the upper package. Note that the application startup class must be in the root directory of the package.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>