mybatis-plus integrated spring boot using

mybatis-plus is to simplify the use and development of domestic mybatis open source project in which the generic mapper interface is very easy to use with a single list of all of the CRUD 

Only its own entity classes mapper interface inherits this interface and then write the generic can be operated

This is the official website mybatis-plus documents written in pretty good   https://mp.baomidou.com/guide/

SSM can be used in the project can also be integrated into spring boot I was here in the spring boot 

The first is dependent maven

<dependency> 
<groupId> com.baomidou </ groupId>
<artifactId> the mybatis-PLUS-the Boot-Starter </ artifactId>
<Version> 3.2.0 </ Version>
</ dependency>

This is the latest version of dependency, import after this do not need to rely on the import of mybatis because mybatis-plus which contains the mybatis

then is to scan the specified class in the spring boot on startup add this comment @MapperScan ( "package name of your mapper interface resides") this comment package name mapper interfaces all
the notes actually use mybatis have to add, after scanning your mapper interface to a proxy classes to perform sql statement like you to be generated at runtime

a method to pay more then in the spring boot startup class, because now certainly have to use paging query function, and mybatis itself only memory paging is to record all meet the conditions found and so the use of paging mybatis-plus comes with pagination plug-in
that is mybatis-plus comes with plug injected into the page spring ioc container management
1   / * 
2       * plug tab
 . 3       * / 
. 4      @Bean
 . 5      public PaginationInterceptor paginationInterceptor () {
 . 6          PaginationInterceptor paginationInterceptor = new new PaginationInterceptor ();
 . 7          // paginationInterceptor.setLimit (the largest single amount of your page limit, default 500, is less than The unrestricted 0 -1); 
. 8          return paginationInterceptor;
 . 9      }

 

Since it is ORM framework is certainly database operation that is the main entity classes and dao interfaces (called mapper interface is also used to call the line a lot, it is called dao)

Said first entity class mybatis-plus type of database configured to provide a connection information table to automatically generate the input entity classes and interfaces dao

As for class and attribute names will have based on your database table or column names will replace the underlined uppercase letter and it was just the next do not have to configure the mapping between the

But if you do not want to want to specify the name of the mapping relationships among the tables are also available annotations

First, a first annotation applied on the entity class and the class corresponding to the specified database table

   End specify the table name also need to specify the primary key, notes There is little need to pay attention to your default settings annotation type is ID_WORKER if you want to insert the entity class object primary key attribute is empty 

He will give you automatically create a unique ID for a dozen long if you are not the primary key attribute of type long int is what will be an error so we wanted to change long into the corresponding database also bigint

If you do not want to use it to the primary key is the database type is set to AUTO increment he gives you, he will not be processed to generate  

 

 End specify the primary key and the rest are common attributes

 

 Entity class about right and the rest is directly own dao class defines an interface and then inherit BaseMapper <entity class you want to operate> can this interface when you've got all CRUD entire class methods

These CRUD methods to see about a dozen single method name and parameters to be passed can also understand it is quite simple 

 

 I am here to talk about the individual paging query

The first argument if the paging query selectPage This method takes two parameters are paging class to a class that implements this interface IPage fact, this is Page mybatis-plus offer

Constructor can be passed to query the first few pages, and each page a few queries if you do not pass the default is the query page 1 and page query 10

The second parameter is to implement Wrapper class interface here is the query so QueryWrapper pass this class also has a UpdateWrapper transfer conditions due to modification

QueryWrapper this class there are many other methods such eq like Between parameters are generally database column names and values ​​and returns the value of each method is also the chain can be programmed QueryWrapper

In fact, this is the essence of our passing column names and values ​​are spliced ​​into the conditions string 

This last method returns a IPage interfaces, there are several ways to get to get the current page total pages the total number of records, etc. may result sets into strong so more convenient powerful Page

 

If multi-table joint inquiry may choose to write in a way dao interface and then use @select notes written inside sql statement to execute the return value write List <Map <String, Object >> 

If only the rear end of the front and rear end of the separation project in response to the front-end json file you do not need treatment directly back to the front after a json format spring MVC and you return entity class format no difference

But if you still want to convert into a single class used to define a class attribute name written on the inside of this time to use the column name sql statement @select notes inside the class and will attribute the same name  

The return value can be written as List <entity name just defined class> Since the column name and the attribute name corresponding to the mybatis-plus can help us to automatically encapsulated object value

 

  You can then configure in spring boot configuration file at print sql statement so it is quite easy to debug and the like

# mybatis-plus print console execute SQL statements 
the mybatis-PLUS:
the Configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
I have to use this configuration file format is yml

basic use of these and many other plug-ins and features I do not have to come back later used to use it to update

 

Guess you like

Origin www.cnblogs.com/java888/p/11567596.html