MyBatis plus与MyBatis分别实现简单的CRUD

前言

通过MyBatis与MyBatis plus分别实现简单的CRUD,更加直观的区分它们之间的不同的编写风格。

1. MyBatis通过继承mapper实现CRUD

MyBatis是一个开源的持久化框架,通过继承Mapper接口可以实现CRUD操作。下面是一个使用MyBatis通过继承Mapper实现CRUD的案例:

  1. 创建数据库表

首先,创建一个名为user的数据库表,包含以下字段:id, name, age

  1. 创建实体类

创建一个名为User的Java类,包含以下字段:id, name, age。并提供对应的getter和setter方法。

public class User {
    private Integer id;
    private String name;
    private Integer age;

    // getter and setter methods
}
  1. 创建Mapper接口

创建一个名为UserMapper的Mapper接口,继承Mapper接口,并定义CRUD操作的方法。

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends Mapper<User> {
}
  1. 配置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
  1. 创建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>
  1. 使用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操作的步骤:

  1. 导入Mybatis Plus的依赖:
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>最新版本</version>
</dependency>
  1. 创建实体类,使用@TableName注解指定数据库表名,使用@TableField注解指定实体类属性和数据库字段的对应关系。

  2. 创建Mapper接口,继承自BaseMapper接口,这样就可以使用Mybatis Plus提供的方法进行CRUD操作。

public interface UserMapper extends BaseMapper<User> {
}
  1. 创建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);
    }
}
  1. 在Spring Boot的配置文件中,配置Mybatis Plus的相关属性。
mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  1. 使用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语句

  1. SQL语句封装在配置文件中,便于统一管理与维护;提供了动态SQL标签,支持编写动态SQL。以及提供映射标签,支持对象与数据库的ORM字段关系映射。
  2. 访问数据库的 sql 语句存放于 mapper (或Dao) 包下的 xml 配置文件中。

MyBatis plus SQL语句

  1. 内置通用 Mapper、通用 Service,不需要再写 xml 了,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 。
  2. 内置 Sql 注入剥离器,有效预防Sql注入攻击 。

MyBatis mapper

  1. 对于一些简单的普通的增删改查,接口可继承通用Mapper进行实现;但对于一些复杂的增删改查功能可在接口方法上写明注解sql 开发实现。

MyBatis plus mapper

  1. 继承BaseMapper;BaseMapper 封装CRUD操作。
  • 66
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值