spring boot之整合Mybatis操作数据库

Spring Boot 框架的核心特性包括简化配置并快速开发,在这个机制下开发项目,当开发者们需要整合某一个功能时,只需要引入其特定的场景启动器 ( starter ) 即可,比如 Web 模块整合、JDBC 模块整合、Thymeleaf 模块整合,开发者们在编码前只需要在 pom.xml 文件中引入对应的 starter 依赖即可。

MyBatis-Spring-Boot-Starter 可以帮助开发者快速创建基于 Spring Boot 的 MyBatis 应用程序,那么使用 MyBatis-Spring-Boot-Starter 可以做什么呢?

  • 构建独立的 MyBatis 应用程序
  • 零模板
  • 更少的 XML 配置代码甚至无 XML 配置

Spring Boot 整合 MyBatis 进行数据库的增、删、改、查

1.在开发项目之前需要在 MySQL 中先创建一张表

2.新建实体类和 Mapper 接口

3.新建 entity 包并在 entity 包下新建 User 类

4.新建 dao 包并在 dao 包中新建 UserDao 接口,并定义增删改查四个方法

5.创建 Mapper 接口的映射文件

在 resources 目录下新建 mapper 目录,并在 mapper 目录下新建 Mapper 接口的映射文件 UserMapper.xml,之后进行映射文件的编写。

5.1.首先,定义映射文件与 Mapper 接口的对应关系,比如该示例中,需要将 UserMapper.xml 文件与对应的 UserDao 接口类之间的关系定义出来:

<mapper namespace="ltd.newbee.mall.dao.UserDao">

5.2.之后,配置表结构和实体类的对应关系:

<resultMap type="ltd.newbee.mall.entity.User" id="UserResult">
  <result property="id" column="id"/>
  <result property="name" column="name"/>
  <result property="password" column="password"/>
</resultMap>

5.3.最后,按照对应的接口方法,编写增删改查方法具体的 SQL 语句,最终的 UserMapper.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ltd.newbee.mall.dao.UserDao">
  <resultMap type="ltd.newbee.mall.entity.User" id="UserResult">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="password" column="password"/>
  </resultMap>


  <select id="findAllUsers" resultMap="UserResult">
    select id,name,password from tb_user
    order by id desc
  </select>


  <insert id="insertUser" parameterType="ltd.newbee.mall.entity.User">
    insert into tb_user(name,password)
    values(#{name},#{password})
  </insert>


  <update id="updUser" parameterType="ltd.newbee.mall.entity.User">
    update tb_user
    set
    name=#{name},password=#{password}
    where id=#{id}
  </update>


  <delete id="delUser" parameterType="int">
    delete from tb_user where id=#{id}
  </delete>
</mapper>

6.新建 MyBatisController

为了对 MyBatis 进行功能测试,在 controller 包下新建 MyBatisController 类,并新增 4 个方法分别接收对于 tb_user 表的增删改查请求,代码如下:

package ltd.newbee.mall.controller;


import ltd.newbee.mall.dao.UserDao;
import ltd.newbee.mall.entity.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.List;


@RestController
public class MyBatisController {


    @Resource
    UserDao userDao;


    // 查询所有记录
    @GetMapping("/users/mybatis/queryAll")
    public List<User> queryAll() {
        return userDao.findAllUsers();
    }


    // 新增一条记录
    @GetMapping("/users/mybatis/insert")
    public Boolean insert(String name, String password) {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
            return false;
        }
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        return userDao.insertUser(user) > 0;
    }


    // 修改一条记录
    @GetMapping("/users/mybatis/update")
    public Boolean insert(Integer id, String name, String password) {
        if (id == null || id < 1 || StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
            return false;
        }
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setPassword(password);
        return userDao.updUser(user) > 0;
    }


    // 删除一条记录
    @GetMapping("/users/mybatis/delete")
    public Boolean insert(Integer id) {
        if (id == null || id < 1) {
            return false;
        }
        return userDao.delUser(id) > 0;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值