前言
通过MyBatis与MyBatis plus分别实现简单的CRUD,更加直观的区分它们之间的不同的编写风格。
1. MyBatis通过继承mapper实现CRUD
MyBatis是一个开源的持久化框架,通过继承Mapper接口可以实现CRUD操作。下面是一个使用MyBatis通过继承Mapper实现CRUD的案例:
- 创建数据库表
首先,创建一个名为user
的数据库表,包含以下字段:id
, name
, age
。
- 创建实体类
创建一个名为User
的Java类,包含以下字段:id
, name
, age
。并提供对应的getter和setter方法。
public class User {
private Integer id;
private String name;
private Integer age;
// getter and setter methods
}
- 创建Mapper接口
创建一个名为UserMapper
的Mapper接口,继承Mapper
接口,并定义CRUD操作的方法。
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends Mapper<User> {
}
- 配置MyBatis
在application.properties
文件中配置MyBatis相关的属性,包括数据库连接信息以及Mapper接口的扫描路径。
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=username
spring.datasource.password=password
mybatis.mapper-locations=classpath:mapper/*.xml
- 创建Mapper映射文件
在resources/mapper
目录下创建一个名为UserMapper.xml
的文件,并配置对应的SQL语句。
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.model.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<sql id="Base_Column_List">
id, name, age
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select
<include refid="Base_Column_List" />
from user
where id = #{id}
</select>
<insert id="insert" parameterType="com.example.model.User">
insert into user (id, name, age)
values (#{id}, #{name}, #{age})
</insert>
<update id="updateByPrimaryKey" parameterType="com.example.model.User">
update user
set name = #{name},
age = #{age}
where id = #{id}
</update>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from user
where id = #{id}
</delete>
</mapper>
- 使用Mapper接口进行CRUD操作
使用UserMapper
接口进行CRUD操作。
import com.example.mapper.UserMapper;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
public void addUser(User user) {
userMapper.insert(user);
}
public void updateUser(User user) {
userMapper.updateByPrimaryKey(user);
}
public void deleteUser(Integer id) {
userMapper.deleteByPrimaryKey(id);
}
}
以上就是使用MyBatis通过继承Mapper接口实现CRUD的案例。通过继承Mapper接口,可以直接调用MyBatis提供的通用方法来进行数据库操作,简化了开发过程。
2. MyBatis plus是如何实现CRUD的
Mybatis Plus是基于Mybatis的增强工具,它简化了Mybatis的开发流程,使得CRUD操作更加简单。下面是使用Mybatis Plus实现简单的CRUD操作的步骤:
- 导入Mybatis Plus的依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
-
创建实体类,使用
@TableName
注解指定数据库表名,使用@TableField
注解指定实体类属性和数据库字段的对应关系。 -
创建Mapper接口,继承自
BaseMapper
接口,这样就可以使用Mybatis Plus提供的方法进行CRUD操作。
public interface UserMapper extends BaseMapper<User> {
}
- 创建Service层,可以使用
@Service
注解将该类注册为Spring Bean,通过依赖注入的方式使用Mapper。
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public void save(User user) {
userMapper.insert(user);
}
public void update(User user) {
userMapper.updateById(user);
}
public void delete(Long id) {
userMapper.deleteById(id);
}
public User getById(Long id) {
return userMapper.selectById(id);
}
public List<User> getAll() {
return userMapper.selectList(null);
}
}
- 在Spring Boot的配置文件中,配置Mybatis Plus的相关属性。
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
- 使用Mybatis Plus进行CRUD操作。
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable Long id) {
return userService.getById(id);
}
@GetMapping("/user")
public List<User> getAllUsers() {
return userService.getAll();
}
@PostMapping("/user")
public int saveUser(@RequestBody User user) {
return userService.save(user);
}
@PutMapping("/user")
public int updateUser(@RequestBody User user) {
return userService.update(user);
}
@DeleteMapping("/user/{id}")
public int deleteUserById(@PathVariable Long id) {
return userService.deleteById(id);
}
}
以上就是使用Mybatis Plus实现简单的CRUD操作的步骤。通过Mybatis Plus,我们可以省去很多重复的CRUD代码,提高开发效率。如果需要更复杂的查询,可以使用Mybatis的XML映射文件或者使用Mybatis Plus提供的查询构造器功能。
总结
MyBatis SQL语句
- SQL语句封装在配置文件中,便于统一管理与维护;提供了动态SQL标签,支持编写动态SQL。以及提供映射标签,支持对象与数据库的ORM字段关系映射。
- 访问数据库的 sql 语句存放于 mapper (或Dao) 包下的 xml 配置文件中。
MyBatis plus SQL语句
- 内置通用 Mapper、通用 Service,不需要再写 xml 了,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 。
- 内置 Sql 注入剥离器,有效预防Sql注入攻击 。
MyBatis mapper
- 对于一些简单的普通的增删改查,接口可继承通用Mapper进行实现;但对于一些复杂的增删改查功能可在接口方法上写明注解sql 开发实现。
MyBatis plus mapper
- 继承BaseMapper;BaseMapper 封装CRUD操作。