亲宝软件园·资讯

展开

详解SpringBoot整合MyBatis详细教程

Baret-H 人气:0

1. 导入依赖

首先新建一个springboot项目,勾选组件时勾选Spring WebJDBC APIMySQL Driver

然后导入以下整合依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.1.3</version>
</dependency>

2. 连接数据库

数据库代码:

-- 创建数据库
CREATE DATABASE springboot;

-- 使用springboot数据库
use springboot;

-- 创建user表
CREATE TABLE IF NOT EXISTS `user`(
	`id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '身份号',
	`name` VARCHAR(30) NOT NULL DEFAULT '匿名' COMMENT '姓名',
	`pwd` VARCHAR(30) NOT NULL DEFAULT '123456' COMMENT '密码',
	PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8

-- 给user表插入数据
INSERT INTO `user`(`id`,`name`,`pwd`) 
VALUES ('1','zsr',000204),('2','gcc',000421),('3','BaretH',200024);

然后IDEA连接数据库

image-20201008203845928

打开我们创建的数据库springboot

image-20201008203924600

对应的user

image-20201008204058928

3. 编写数据库配置信息

在springboot配置文件中配置数据库信息

spring.datasource.username=root
spring.datasource.password=200024
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

4. 编写pojo实体类

在主程序同级目录下新建pojo包,其中新建User实体类(使用了lombok)

package com.zsr.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
  int id;
  String name;
  String password;
}

5. 编写mapper接口

在主程序同级目录下新建mapper包,其中新建UserMapper接口

image-20201011220718188

package com.zsr.mapper;

import com.zsr.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper //表示这是Mybatis的mapper类
@Repository
public interface UserMapper {
  List<User> queryUserList();

  User queryUserByID(int id);

  int addUser(User user);

  int updateUser(User user);

  int deleteUser(int id);
}

6. 编写mapper.xml

在resources目录下新建mabatis包,其中新建mapper包,再在其中新建mapper.xml

image-20201011225616633

<?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="com.zsr.mapper.UserMapper">
  <select id="queryUserList" resultType="user">
    select * from user
  </select>

  <select id="queryUserByID" resultType="user">
    select * from user where id= #{id}
  </select>

  <insert id="addUser" parameterType="user">
    insert into user(id,name,pwd) values (#{id},#{name},#{pwd})
  </insert>

  <update id="updateUser" parameterType="user">
    update user set name=#{name},pwd=#{pwd} where id=#{id}
  </update>

  <delete id="deleteUser" parameterType="int">
    delete from user where id=#{id}
  </delete>
</mapper>

7. 编写controller

在主程序同级目录下新建controller包,在其中新建UserController

package com.zsr.controller;

import com.zsr.mapper.UserMapper;
import com.zsr.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
  @Autowired
  private UserMapper userMapper;

  @GetMapping("/queryUserList")
  public List<User> queryUserList() {
    List<User> users = userMapper.queryUserList();
    return users;
  }

  @GetMapping("/queryUserByID")
  public User queryUserByID() {
    User user = userMapper.queryUserByID(2);
    return user;
  }

  @GetMapping("/addUser")
  public String addUser() {
    userMapper.addUser(new User(4, "zml", "45632"));
    return "增加用户完毕";
  }

  @GetMapping("/updateUser")
  public String updateUser() {
    userMapper.updateUser(new User(4, "zml", "678910"));
    return "修改用户完毕";
  }

  @GetMapping("/deleteUser")
  public String deleteUser() {
    userMapper.deleteUser(4);
    return "删除用户完毕";
  }
}

8. 测试

测试访问http://localhost:8080/queryUserList

image-20201011232324236

测试访问http://localhost:8080/queryUserByID

image-20201011232404442

测试访问http://localhost:8080/addUser

image-20201011232855926

测试访问http://localhost:8080/updateUser

image-20201011232934604

image-20201011233014935

测试访问http://localhost:8080/deleteUser

image-20201011233046907

image-20201011233112576

加载全部内容

相关教程
猜你喜欢
用户评论