Spring Boot integrates MyBatis, once a few small puzzles and trampling the pits.
First, the structure of MyBatis
MyBatis and spring boot integration, countless tutorials on the web, are teaching you step-by-step integration, do not have a problem, but do it to make me this half bucket of water some know it does not know why the feeling. To summarize:
- Structure: The entity layer (Pojo Class) + The data Access Layer (DAO interface) + Service layer (services Class), and then use service in the action Layer (Controller Class).
- Data binding: There are two ways for the DAO layer to concatenate SQL with methods, namely XML or annotations, each with good.
- DAO layer Callout: The DAO layer needs to be scanned by the framework. You can use the @mapper annotation directly in the DAO class, or you can use the @mapperscan ("Xxx.dao") in the Startup Class (Xxxapplication) to label labels uniformly, and each has a good
- Service layer: Some people like to use Interface+class implements way, first define an interface, and then use the inheritance interface way to implement service. You can also simply define the service class directly. Also each has each good, the individual thinks the simple project does not have the whole interface + inherits the way. Either way, however, you need to use the @service annotation in the service class to label it, otherwise it cannot be scanned by the framework.
- Reference Links: 77249029, https://www.cnblogs.com/goloving/p/9126187.html. Recommend the latter, the logic is relatively clear.
Second, MySQL's Self-increment field
There are serial numbers in Oracle to ensure that the ID field is unique, self-growing, and can be obtained in code. MySQL can also, when the table is built first define the primary key, self-increment: CREATE TABLE log (id int (8) NOT null primary key auto_increment,...);
Defining the self-increment class in the MyBatis insert configuration: usegeneratedkeys= "true" keyproperty= "id"
When used, note that the Insert method returned is not the ID value of the new record, and the ID value of the new record needs to be obtained from the ID attribute in the entity class.
Third, the MySQL JDBC error
MySQL server and client JDBC need to match, there are several main issues:
- More than 6 JDBC, you need to change com.mysql.jdbc.Driver to Com.mysql.cj.jdbc.Driver
- More than 5.5 of the servers require that an SSL connection be established by default, at which point the SSL connection is explicitly disabled by setting Usessl=false: Jdbc:mysql://localhost:3306/dev?usessl=false
- 8 Server If you report Caching-sha2-password errors with a low-version client connection, you need to modify the password rules for the logged-on user name: ALTER USER ' root ' @ ' localhost ' identified with mysql_native_ Password by ' password '; FLUSH privileges;
- Possible error when server version and client version are inconsistent: Unknown system variable ' query_cache_size ', you need to set the appropriate JDBC version in MAVEN based on the server
Spring Boot+mybatis+mysql