Table of contents
- 1. Underlying framework:
- 2. Code differences:
- 3. Usage differences:
- 4. Case code analysis:
-
- 4.1 MyBatis
- 4.2 MyBatisPlus
MyBatis and MyBatisPlus are both MyBatis-based extension libraries used to simplify the development of MyBatis. MyBatisPlus is encapsulated on the basis of MyBatis and provides a set of common CRUD operation interfaces, while MyBatis is an underlying persistence layer framework.
1. Underlying framework:
MyBatis is a persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manual setting of parameters and obtaining result sets. MyBatis can use simple XML or annotations for configuration and native mapping.
MyBatisPlus is an extension library based on MyBatis. It provides a set of universal CRUD operation interfaces, automatically generates SQL statements, and provides rich query condition constructors and other functions. MyBatisPlus mainly uses annotations for configuration and mapping.
2. Code differences:
MyBatis code mainly involves the following parts:
- MyBatis configuration file (mybatis-config.xml), used to configure data sources, mapping files, etc.
- Mapping file (Mapper.xml), used to define SQL statements and result mapping.
- Mapper interface, used to define methods corresponding to mapping files.
- The DAO interface is used to encapsulate the Mapper interface and provide a unified CRUD method.
The code of MyBatisPlus mainly involves the following parts: - The configuration file of MyBatisPlus (mybatis-plus-config.xml) is used to configure data sources, paging plug-ins, etc.
- Entity class (Entity) is used to define the entity class corresponding to the database table.
- Mapper interface, used to define methods corresponding to database tables.
- Service interface is used to encapsulate the Mapper interface and provide unified CRUD methods.
- The ServiceImpl class implements the Service interface and calls the Mapper interface to perform database operations.
3. Usage differences:
MyBatis requires manual writing of SQL statements and result mapping, but provides a high degree of customization. The advantage of MyBatis is that it can flexibly write SQL statements according to actual needs, but the disadvantage is that it is cumbersome to write mapping files and manually set parameters.
MyBatisPlus provides a set of universal CRUD operation interfaces to automatically generate SQL statements. The advantage of MyBatisPlus is that it simplifies the development of MyBatis and improves development efficiency. However, the disadvantage is that it has poor customization capabilities and may not be able to meet the needs of complex SQL statements.
4. Case code analysis:
4.1 MyBatis
The following is a simple MyBatis case to demonstrate the addition, deletion, modification and query of user information:
<!-- Configuration file -->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
<!-- mapping file -->
<mapper namespace="com.example.mapper.UserMapper">
<insert id="insertUser" parameterType="com.example.entity.User">
INSERT INTO user (username, password, email) VALUES (#{username}, #{password}, #{email})
</insert>
<!-- other SQL statement -->
</mapper>
// Mapper interface
public interface UserMapper {
int insertUser(User user);
// Other methods
}
// DAO interface
public interface UserDao {
int insertUser(User user);
// Other methods
}
// business logic layer
@Service
public class UserService {
@Autowired
private UserDao userDao;
public int insertUser(User user) {
return userDao.insertUser(user);
}
// Other methods
}
4.2 MyBatisPlus
The following is a simple MyBatisPlus case to demonstrate the addition, deletion, modification and query of user information:
- First, create an entity class (Entity) User.java:
public class User {
private Long id;
private String username;
private String password;
private String email;
// omission getter and setter method
}
- Create a Mapper interface UserMapper.java:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper<User> {
}
- Create a Service interface UserService.java:
import com.baomidou.mybatisplus.extension.service.IService;
public interface UserService extends IService<User> {
}
- Create a ServiceImpl class UserServiceImpl.java and implement the Service interface:
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
- Create a Controller class UserController.java to handle HTTP requests:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public String createUser(@RequestBody User user) {
userService.save(user);
return "User created";
}
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.getById(id);
}
@GetMapping
public List<User> getUsers() {
return userService.list();
}
@PutMapping("/{id}")
public String updateUser(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
userService.updateById(user);
return "User updated";
}
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Long id) {
userService.removeById(id);
return "User deleted";
}
}
- Configure MyBatisPlus in the Spring Boot project:
<!-- Configuration file -->
<configuration>
<extensions>
<extension plugin-id="mybatis-plus" interceptor="com.baomidou.mybatisplus.extension.MybatisPlusInterceptor"/>
</extensions>
</configuration>
<!-- Configuration file -->
<mybatis-plus-config>
<mapper-locations>
<mapper-location resource="classpath:mapper/*Mapper.xml"/>
</mapper-locations>
<type-aliases>
<package name="com.example.entity"/>
</type-aliases>
<global-config>
<db-config>
<logic delete-flag="true"/>
<logic not-delete-flag="false"/>
</db-config>
<date-format>yyyy-MM-dd HH:mm:ss</date-format>
<null-type-handler>com.baomidou.mybatisplus.core.handlers.Empty string type handler</null-type-handler>
</global-config>
</mybatis-plus-config>
The above code demonstrates how to use MyBatisPlus to add, delete, modify and check user information. In this case, we used the annotations and plug-ins provided by MyBatisPlus to simplify the development of MyBatis.