溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Springboot中使用mybatis

發(fā)布時間:2021-04-16 15:51:56 來源:億速云 閱讀:208 作者:Leah 欄目:開發(fā)技術

今天就跟大家聊聊有關怎么在Springboot中使用mybatis,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

前期工作

1.導入mybatis整合依賴

<!--   mybatis整合     -->
        <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

2.連接數據庫

3.連接完數據庫就去applicaton.yml配置一下數據庫

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

開始整合

1.編寫與數據庫對應的實體類

package com.example.pojo;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String pwd;

}

為了偷懶,導入了lombok

<!-- lombok     -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

2.編寫mapper

package com.example.mapper;

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

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    //查詢用戶的全部信息
    List<User> getUserList();

    //select 找出id=1的用戶
    User getUserById(int id);

    //insert 增加一個用戶
    int insertUser(User user);

    //delete 刪除id=4的用戶
    int deleteUser(int id);

    //update 將id=2的用戶名字改為小龍
    int updateUser(User user);

}

3.編寫mapper.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="com.example.mapper.UserMapper">
    <select id="getUserList" resultType="user">
        select *
        from mybatis.user
    </select>

    <select id="getUserById" resultType="user" parameterType="int">
        select *
        from mybatis.user
        where id = #{id};
    </select>

    <insert id="insertUser" parameterType="user">
        insert into mybatis.user (id, name, pwd)
        values (#{id}, #{name}, #{pwd});
    </insert>

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

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

</mapper>

這里我們用了別名而且我們把這個mapper.xml文件放在了resources目錄下,所以我們要去application.yml配置一下

mybatis:
  type-aliases-package: com.example.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml

mapper.xml文件編寫的位置:

怎么在Springboot中使用mybatis

4.編寫controller

package com.example.controller;

import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

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

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") int id){
        return userMapper.getUserById(id);
    }

    @GetMapping("/insertUser")
    public String insertUser(){
        userMapper.insertUser(new User(5,"xiaoming","111"));
        return "ok";
    }

    @GetMapping("/deleteUser")
    public String deleteUser(){
        userMapper.deleteUser(5);
        return "ok";
    }

    @GetMapping("/updateUser")
    public String updateUser(){
        userMapper.updateUser(new User(5,"xx","111"));
        return "ok";
    }

}

看完上述內容,你們對怎么在Springboot中使用mybatis有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI