The MyBatis-Enhance persistence framework is open source and will only be enhanced without modification.Crazy point to view details!!
Welcome to add group communication
Enhance communication group ① : 782540319 SpringBoot core technology ① : 373229384 SpringBoot core technology ② : 588351309
Maven repository address
What is MyBatis Enhance?
Enhance is an enhanced version of native MyBatis that does not affect any native use and completely replaces MyBatis – Core, MyBatis – Spring and MyBatis – spring-boot-Starter. SpringBoot configuration file can be used to configure the relevant content, as powerful as possible convenient and fast integration of MyBatis.
In addition, it also provides CRUD operation of single table basic data and part of batch data operation, you can no longer use MyBatis to provide automatic generation of data operations on a single data table, of course, if you want to use it is also possible.
Enhance also maps out a way to dynamically query across multiple tables, which lets you experience common data actions like association, aggregation, and multi-table query fields as you write SQL statements in Java code (not currently supported in 1.0.2.release).
Using the environment
SpringBoot has become so popular that, for ease of use, Enhance is currently only available in projects that integrate with the SpringBoot framework.
SpringBoot1.x
The above versionJDK 1.6
The above version
The installation
The installation is relatively simple. In a project that references Enhance, you also need to add the database drivers and data sources you use. Enhance does not limit this, since the jar associated with Enhance has already been uploaded to the Maven Center Repository, we just need to add the dependency. The development tool automatically downloads the related dependency packages.
- use
Maven
When building the tool, copy the following intopom.xml
In configuration file
<dependency> <groupId>com.gitee.hengboy</groupId> <artifactId>mybatis-enhance-spring-boot-starter</artifactId> < version > 1.0.2. RELEASE < / version > < / dependency >Copy the code
- If you are using
Gradle
Build tools, then copy the following into yourbuild.gradle
compile group: 'com.gitee.hengboy', name: 'mybatis-enhance-spring-boot-starter', version: '1.0.2. RELEASE'
Copy the code
How do you use it?
Entity creation
Enhance uses the Spring Data JPA to manage Entity classes, and already provides annotations, called entities, that correspond to tables in a database. Here’s a simple Entity code:
/ * * * * * user data entity @ author: in the yu < br / > * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * Created with the IDEA. * Date: 2018/5/13 * Time: 8:53 * Jane books: http://www.jianshu.com/u/092df3f77bca * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / Data @ Table (name ="test_user_info") public class UserInfoEntity implements Serializable {/ user Id * * * * / @ Id (generatorType = KeyGeneratorTypeEnum. AUTO) @Column(name ="TUI_ID") private Integer userId; /** * user name */ @column (name ="TUI_NAME") private String userName; /** * age */ @column (name ="tui_age") private Integer age; /** * address */ @column (name ="tui_address")
private String address;
}
Copy the code
I used annotations named in the same way as the Spring Data JPA, so that you can quickly convert the use of annotations when you use Enhance.
The Mapper created
Create a Mapper in the same way as you did with native MyBatis, but with Enhance you don’t need the @mapper annotation. You create a Mapper that can be scanned by inherits EnhanceMapper
and can access CRUD methods provided internally! !!!!! As follows:
/ * * * * * @ author user basic information data interface: in the yu < br / > * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * Created with the IDEA. * Date: 2018/5/13 * Time: 9:00 * Simple book: http://www.jianshu.com/u/092df3f77bca * ================================ */ public interface UserInfoMapper extends EnhanceMapper<UserInfoEntity, Integer> { }Copy the code
EnhanceMapper requires two generic types, the first is the type of the entity class, and the second is the type of the primary key of the entity class, which is convenient for us to achieve unity when passing parameters or returning values. Otherwise, we also need to convert the Object type, which is not only troublesome but also increases the operation cost. Please read the usage document for details
Temporary built-in methods
/ / statistics Long countAll () throws EnhanceFrameworkException; / / to empty data void deleteAll () throws EnhanceFrameworkException; Void deleteArray(Id... ids) throws EnhanceFrameworkException; / / delete data according to the custom SQL void deleteBySql (String SQL, the Map < String, Object > params) throws EnhanceFrameworkException; / / Collection according to the primary key to delete the specified data void deleteCollection (Collection < Id > Collection) throws EnhanceFrameworkException; / / delete a data void deleteOne (Id Id) throws EnhanceFrameworkException; / / data void insert (T T) throws EnhanceFrameworkException; Void insertArray(T... array) throws EnhanceFrameworkException; / / save all data within a Collection void insertCollection (Collection < T > Collection) throws EnhanceFrameworkException; / / query all data List < T > selectAll () throws EnhanceFrameworkException; List<T> selectArray(Id... ids) throws EnhanceFrameworkException; / / paging query data List < T > selectByPageable (Pageable Pageable) throws EnhanceFrameworkException; / / custom SQL query data List < Map > selectBySql (String SQL, the Map < String, Object > params) throws EnhanceFrameworkException; / / Collection according to the primary key to the query data List < T > selectCollection (Collection < Id > ids) throws EnhanceFrameworkException; / / according to the primary key to query a single data T selectOne (Id Id) throws EnhanceFrameworkException; / / according to the primary key update data entity void update (T T) throws EnhanceFrameworkException; / / custom SQL update data void updateBySql (String SQL, the Map < String, Object > params) throws EnhanceFrameworkException;Copy the code
The above is a list of built-in methods provided by 1.0.2.RELEASE, which are commonly used in daily development to operate on single table data.
The use of method naming conventions
Method naming rule is a Data operation method provided in Spring Data JPA, which is mainly applicable to Data operations such as query, statistics and deletion. Its main principle is to automatically generate SQL based on method names and use regular expressions to match methods.
Method rule query
The following is a simple example for querying method rules:
public interface UserInfoMapper extends EnhanceMapper<UserInfoEntity, Integer> {/** * Query based on only one field ** @param name Value of the query condition * @return
*/
UserInfoEntity findByUserName(@Param("userName") String name); ** @param name specifies the first query value * @param age specifies the second query value * @return
*/
UserInfoEntity findByUserNameAndAge(@Param("userName") String name, @Param("age") Integer age);
}
Copy the code
Method rule statistics
The following is a simple example of method rule statistics:
public interface UserInfoMapper extends EnhanceMapper<UserInfoEntity, Integer> {/** * Statistics based on only one field ** @param name Value of the statistics condition * @return
*/
Long countByUserName(@Param("userName") String name); /** * Perform statistics based on multiple criteria ** @param name Value of the first criteria * @param age value of the second criteria * @return
*/
Long countByUserNameAndAge(@Param("userName") String name, @Param("age") Integer age);
}
Copy the code
Method rule deletion
The following is a simple example for deleting method rules:
public interface UserInfoMapper extends EnhanceMapper<UserInfoEntity, Integer > {/ * * * according to a field only delete * * @ param name query condition value * / void removeByUserName (@ param ("userName") String name); ** @param name = param id = param id = param id = param id = param id = param id = param id = param id = param id = param id = param id = param id removeByUserNameAndUserId(@Param("userName") String name, @Param("userId") String id);
}
Copy the code
Of course, method naming rules are not only convenient for the above point, please read the usage documentation for details
Stay tuned for Maven automated code generation plug-ins
As the current development environment, whether you are working on product projects or outsourcing projects, you should implement functions quickly. For MyBatis Enhance framework, I wrote a separate code generation tool. Automating the creation of entities, Mapper and other necessary classes, as well as customizing the creation of entity classes through the Freemarker template for real fast code development and improved coding efficiency!!
Stay tuned for DSL dynamic queries
Single-table data manipulation is supported internally in Enhance by default, but multi-table queries are the most common in our projects. I migrated some of the basic implementation to Enhance, taking advantage of the QueryDSL dynamic query framework. Greatly convenient multi-table joint query and dynamic return arbitrary data Entity (Entity), data mapping Entity (DTO), etc.