Top Related Projects
MyBatis SQL mapper framework for Java
Simplifies the development of creating a JPA-based data access layer.
jOOQ is the best way to write SQL in Java
Unified Queries for Java
Hibernate's core Object/Relational Mapping functionality
Quick Overview
MyBatis-Plus is an powerful enhancement tool for MyBatis framework. It simplifies development, reduces code quantity, and provides many plugins to meet various project requirements. MyBatis-Plus aims to be the best partner for MyBatis, making development more efficient and enjoyable.
Pros
- Simplifies CRUD operations with minimal configuration
- Provides powerful code generation tools
- Offers flexible and customizable query methods
- Supports multiple database types and automatic pagination
Cons
- Learning curve for developers new to MyBatis ecosystem
- May introduce unnecessary complexity for simple projects
- Some advanced features might require additional configuration
- Documentation is primarily in Chinese, which may be challenging for non-Chinese speakers
Code Examples
- Simple CRUD operations:
// Insert a new user
User user = new User();
user.setName("John");
user.setAge(28);
userMapper.insert(user);
// Query user by ID
User found = userMapper.selectById(1);
// Update user
found.setAge(29);
userMapper.updateById(found);
// Delete user
userMapper.deleteById(1);
- Conditional query using QueryWrapper:
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "John")
.ge("age", 18)
.orderByDesc("create_time");
List<User> userList = userMapper.selectList(queryWrapper);
- Pagination:
Page<User> page = new Page<>(1, 10);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("age", 18);
IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
Getting Started
- Add MyBatis-Plus dependency to your project:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
- Configure DataSource in your application.yml:
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database
username: your_username
password: your_password
- Create an entity and mapper:
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
public interface UserMapper extends BaseMapper<User> {
}
- Use the mapper in your service:
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getAllUsers() {
return userMapper.selectList(null);
}
}
Competitor Comparisons
MyBatis SQL mapper framework for Java
Pros of MyBatis
- More flexible and customizable for complex SQL queries
- Better suited for legacy databases or complex schemas
- Provides finer control over SQL execution and mapping
Cons of MyBatis
- Requires more manual configuration and boilerplate code
- Steeper learning curve for developers new to the framework
- Less automation for common CRUD operations
Code Comparison
MyBatis:
<select id="selectUser" resultType="User">
SELECT * FROM users WHERE id = #{id}
</select>
MyBatis-Plus:
@Select("SELECT * FROM users WHERE id = #{id}")
User selectUser(Long id);
MyBatis-Plus simplifies the process by allowing annotations directly on Java interfaces, reducing XML configuration. It also provides additional features like:
- Automatic CRUD methods
- Pagination support
- Code generation tools
- Dynamic table name mapping
While MyBatis offers more control and flexibility, MyBatis-Plus aims to simplify development and increase productivity, especially for standard CRUD operations and simpler database schemas. The choice between the two depends on project requirements, existing database complexity, and developer preferences.
Simplifies the development of creating a JPA-based data access layer.
Pros of Spring Data JPA
- More comprehensive ORM solution with full JPA support
- Seamless integration with other Spring ecosystem projects
- Powerful query derivation from method names
Cons of Spring Data JPA
- Steeper learning curve, especially for complex queries
- Can be slower for large-scale operations due to JPA overhead
Code Comparison
Spring Data JPA:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByLastNameAndAge(String lastName, int age);
}
MyBatis-Plus:
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT * FROM user WHERE last_name = #{lastName} AND age = #{age}")
List<User> findByLastNameAndAge(String lastName, int age);
}
Spring Data JPA allows for more declarative query definitions, while MyBatis-Plus offers more control over SQL queries. Spring Data JPA's approach is more concise and requires less boilerplate, but MyBatis-Plus provides finer-grained control over the generated SQL.
Both frameworks aim to simplify database operations, but they cater to different preferences and use cases. Spring Data JPA is ideal for developers who prefer working with object-oriented paradigms and want to leverage the full power of JPA. MyBatis-Plus is better suited for those who want more control over SQL queries and prefer a lighter-weight solution with less abstraction.
jOOQ is the best way to write SQL in Java
Pros of jOOQ
- Strong type safety and compile-time SQL syntax checking
- Supports a wide range of databases with database-specific features
- Powerful code generation for database schema
Cons of jOOQ
- Steeper learning curve compared to MyBatis-Plus
- Commercial licensing required for some databases and features
- More verbose code for simple CRUD operations
Code Comparison
MyBatis-Plus:
@Mapper
public interface UserMapper extends BaseMapper<User> {
// CRUD methods are automatically available
}
User user = userMapper.selectById(1);
jOOQ:
Result<Record> result = create.select()
.from(USER)
.where(USER.ID.eq(1))
.fetch();
User user = result.into(User.class).get(0);
Key Differences
- MyBatis-Plus focuses on simplifying CRUD operations and reducing boilerplate code
- jOOQ provides a more SQL-like approach with strong type safety
- MyBatis-Plus is easier to set up and use for simple projects
- jOOQ offers more advanced querying capabilities and better support for complex SQL operations
Both libraries have their strengths, and the choice depends on project requirements, database complexity, and developer preferences.
Unified Queries for Java
Pros of Querydsl
- Supports multiple backends (JPA, MongoDB, SQL, etc.)
- Provides a more flexible and type-safe query construction
- Better integration with Java IDEs for code completion and refactoring
Cons of Querydsl
- Steeper learning curve compared to MyBatis-Plus
- Requires more setup and configuration
- Less focus on CRUD operations and code generation
Code Comparison
MyBatis-Plus:
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "John").gt("age", 18);
List<User> users = userMapper.selectList(queryWrapper);
Querydsl:
QUser user = QUser.user;
List<User> users = queryFactory.selectFrom(user)
.where(user.name.like("John").and(user.age.gt(18)))
.fetch();
Summary
MyBatis-Plus is a powerful extension of MyBatis, focusing on simplifying CRUD operations and providing code generation tools. It's easier to set up and use for basic database operations.
Querydsl, on the other hand, offers a more flexible and type-safe approach to query construction across multiple backends. It provides better IDE integration but requires more initial setup and has a steeper learning curve.
Choose MyBatis-Plus for rapid development with MyBatis and simpler CRUD operations. Opt for Querydsl if you need more complex queries, type safety, and support for multiple backends.
Hibernate's core Object/Relational Mapping functionality
Pros of Hibernate ORM
- More comprehensive ORM solution with full JPA implementation
- Powerful object-relational mapping capabilities, including complex relationships
- Extensive caching mechanisms for improved performance
Cons of Hibernate ORM
- Steeper learning curve due to its complexity
- Can be slower for simple CRUD operations compared to MyBatis-Plus
- Potential for performance issues with large datasets if not properly optimized
Code Comparison
Hibernate ORM:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
MyBatis-Plus:
@TableName("users")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
}
Both frameworks provide annotations for mapping entities to database tables, but Hibernate ORM uses JPA annotations, while MyBatis-Plus uses its own set of annotations. Hibernate ORM generally requires more configuration and offers more advanced mapping capabilities, whereas MyBatis-Plus aims for simplicity and ease of use.
MyBatis-Plus is better suited for projects that prioritize simplicity and performance for basic CRUD operations, while Hibernate ORM is more appropriate for complex domain models and applications requiring advanced ORM features.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Born To Simplify Development
ä¼ä¸ç Mybatis-Mate é«çº§ç¹æ§
æ·»å 微信 wx153666
å¤æ³¨è¿ mp 群
ä¸å 许éæ³é¡¹ç®ä½¿ç¨ï¼åæèªè´
Special user
What is MyBatis-Plus?
MyBatis-Plus is an powerful enhanced toolkit of MyBatis for simplify development. This toolkit provides some efficient, useful, out-of-the-box features for MyBatis, use it can effectively save your development time.
Links
Features
- Fully compatible with MyBatis
- Auto configuration on startup
- Out-of-the-box interfaces for operate database
- Powerful and flexible where condition wrapper
- Multiple strategy to generate primary key
- Lambda-style API
- Almighty and highly customizable code generator
- Automatic paging operation
- SQL Inject defense
- Support active record
- Support pluggable custom interface
- Build-in many useful extensions
Getting started
-
Add MyBatis-Plus dependency
- Latest Version:
- Maven:
- SpringBoot2
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>Latest Version</version> </dependency>
- SpringBoot3
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>Latest Version</version> </dependency>
- Gradle
- SpringBoot2
compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: 'Latest Version'
- SpringBoot3
compile group: 'com.baomidou', name: 'mybatis-plus-spring-boot3-starter', version: 'Latest Version'
-
Modify mapper file extends BaseMapper interface
public interface UserMapper extends BaseMapper<User> { }
-
Use it
List<User> userList = userMapper.selectList( new QueryWrapper<User>() .lambda() .ge(User::getAge, 18) );
MyBatis-Plus will execute the following SQL
SELECT * FROM user WHERE age >= 18
This showcase is just a small part of MyBatis-Plus features. If you want to learn more, please refer to the documentation.
License
MyBatis-Plus is under the Apache 2.0 license. See the Apache License 2.0 file for details.
Top Related Projects
MyBatis SQL mapper framework for Java
Simplifies the development of creating a JPA-based data access layer.
jOOQ is the best way to write SQL in Java
Unified Queries for Java
Hibernate's core Object/Relational Mapping functionality
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot