Spring boot and mybatis configuration methods
1. First, all the dependencies required for configuring mybatis by spring boot are as follows:
Here we mainly talk about the database driver package above. Oracle and MySQL introduce different dependencies. Don't forget. If you forget to import, an exception will be reported that the driver class cannot be found.
2. Then, in application Add the following contents to the properties configuration file:
Spring There are actually many contents at the beginning of datasource. For details, see application Enter in the properties file and see what you can point out.
by default, springboot will set spring The information at the beginning of datasource is stored to configure the datasource object, and then the sqlsessionfactory and other related database connections. Therefore, many other people will say what configuration classes to write, which I don't think is necessary. (configure Druid with a @ bean, which will be discussed in the next article)
Of course, if it is particularly detailed, in application Properties file through spring The datasource can't point out the relevant configuration, so you may need to configure classes.
mybatis. The location pointed by mapper locations starts from Src / main / resource. You need to add classpath before it, which points to your mapper The location where the XML file is placed.
3. I'm mybatis. I'm the entity class and mapper generated through reverse engineering of database tables. First, post the specific placement structure of the project as follows:
The explanation of the above figure is as follows:
The startup class must be placed at the top of the project relative to other classes. As mentioned in the previous article, spring boot does not have the < bean > tag configured by the traditional spring project. It scans beans from the startup class down into the default built-in Tomcat container.
The service in the above figure is placed higher than its implementation class. If there are two services a and B, and there are aimpl and bimpl in the implementation class, if aimpl calls B, it may report "a field named 'B' not found" in the startup container. It seems that this error indicates that the container scanning sequence is wrong.
The best solution to this situation is to put the service high and scan the service before scanning the impl when the container is started, so that the service can be found when the class scanning is implemented, and this error will not occur.
Another solution is to add an additional annotation @ lazy when introducing a service
In short, if there is a call and called relationship, you must remember that the callee must first be scanned into the spring boot built-in container, that is, the callee's package position should be higher (at least the position should be flat).
3. A @ service annotation needs to be added to the service implementation class. This may not be necessary. I haven't tried it.
4. In addition to these requirements, spring boot + mybatis also needs to add an annotation @ mapperscan at the startup class, as follows:
Here is my mapper Namespace value in XML
This completes the configuration of mybatis. The next article will talk about spring boot + mybatis transaction configuration, Druid configuration and paging plug-in configuration.
The above is the spring boot and mybatis configuration method introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!