SpringBoot is a scaffolding for rapid development based on the Spring framework. It can help our developers quickly build a project environment, and provides some suggested configuration methods, reducing the difficulty of developing and deploying developers' programs.
2 About MyBatis
2.1 Overview of MyBatis
MyBatis is a persistence framework for Java, the purpose is to make the operation of the database more convenient, flexible and efficient. Java objects and SQL statements can be mapped through Java annotations and XML files, providing a very flexible way of writing SQL and creating dynamic SQL statements, and can be used in conjunction with the Spring framework.
2.2 MyBatis core idea
Separate Java objects from database operations, map to database fields through annotations and XML mapping files, and provide corresponding APIs to operate databases. It can automatically convert SQL statements into JDBC codes, and generate corresponding results according to the specified return value type for developers to use.
2.3 MyBatis usage process
Configure the MyBatis environment Add related dependencies in pom.xml: MyBatis Framework and MySQL Driver
The connection information of the configuration database in the configuration file(application.properties)
Define the pojo class and make a mapping relationship with the data table
Write the Dao layer interface, define the method of operating the database in the interface, and implement the specific SQL statement in the annotation or XML file.
@MapperpublicinterfaceUserMapper{
@Insert("INSERT INTO user VALUES(NULL,#{username})")intinsert(User user);}
The annotation-based method saves the writing of configuration XML files, and can easily complete some simple CRUD operations, but it will be redundant for some complex SQL statements.
3.1.2 Use process
(maven project) Add MyBatis and MySQL related dependencies in pom.xml
Configure the connection information of the database in the configuration file (application.properties)
Create an entity class and make a corresponding relationship with the table in the database
Configure the Mapper interface, add the @Mapper annotation to the interface, tell the bottom layer to create an implementation class for this interface, define the logic of data access in the implementation class, and execute the session with the database
Define the method in the interface, and use annotations to mark the type of SQL statement on the method. The annotations that can be used are: @Insert, @Dlelete, @Update, @Select If multiple parameters are involved in the SQL statement, you can use the @Param annotation to give Name each parameter.
Better readability XML files have good structure and semantics, which can completely separate JAVA codes and SQL statements, which is easy to manage and optimize. Note that configuring SQL statements may make JAVA code verbose.
Better reusability Write SQL statements into XML files, extract and quote repeated SQL through and tags, thus improving the reusability of SQL statements.
Better support for dynamic SQL Dynamic SQL statements are SQL generated according to runtime parameters, which are more complex and more convenient to use XML files.
3.2.2 Use process
Add dependencies: pom.xml
Configure data source: application.propertis
Configure the scanning path of the xml file: application.properties
Create an entity class and make a correspondence with the data table
Define the interface, add the @Mapper annotation, and define the methods in the interface
sql label and include label: for the reuse of SQL statements
<!--1.重复SQL抽取--><sqlid="selectSql">
SELECT * FROM xxx
</sql><selectid="xxx"resultType="xxx"><includerefid="selectSql"></include></select><selectid="yyy"resultType="yyy"><includerefid="selectSql"></include> WHERE id>5
</select>