溫馨提示×

溫馨提示×

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

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

使用SpringBoot對Mybatis進(jìn)行整合實現(xiàn)注解開發(fā)

發(fā)布時間:2020-11-25 14:13:28 來源:億速云 閱讀:237 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)使用SpringBoot對Mybatis進(jìn)行整合實現(xiàn)注解開發(fā),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

SpringBoot整合Mybatis 引入maven依賴

(IDEA建項目的時候直接選就可以了)

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

配置application.yml文件

server:
 port: 8144
spring:
 #redis
 redis:
  host: 127.0.0.1
  port: 6379
  password: 123456
  timeout: 3000
 datasource:
  username: root
  password: root
  driver-class-name: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://localhost:3306/hellomybatis&#63;serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8

建實體類、Controller類、Service類 實體類

package com.example.mybatisDemo.entity;

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

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

Controller類

package com.example.mybatisDemo.controller;

import com.example.mybatisDemo.entity.User;
import com.example.mybatisDemo.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("api/user")
public class UserController {
  private final UserService userService;

  public UserController(UserService userService) {
    this.userService = userService;
  }

  @GetMapping()
  public List<User> getAllUsers(){
    return userService.getUsers();
  }

  @DeleteMapping("{id}")
  public int deleteUser(@PathVariable long id){
    return userService.deleteUser(id);
  }

  @PostMapping
  public int saveUser(@RequestBody User user){
    return userService.insertUser(user);
  }

  @PutMapping
  public int updateUser(@RequestBody User user){
    return userService.updateUser(user);
  }

  @GetMapping("{id}")
  public User getUser(@PathVariable long id){
    return userService.getUser(id);
  }
}

Service類

package com.example.mybatisDemo.service;

import com.example.mybatisDemo.entity.User;
import com.example.mybatisDemo.repository.UserRepository;
import org.springframework.stereotype.Service;
import java.util.*;


@Service
public class UserService {
  private final UserRepository userRepository;

  public UserService(UserRepository userRepository) {
    this.userRepository = userRepository;
  }

  public List<User> getUsers(){
    return userRepository.getUsers();
  }

  public int deleteUser(long id){
    return userRepository.deleteUser(id);
  }

  public User getUser(long id){
    return userRepository.getUser(id);
  }

  public int updateUser(User user){
    return userRepository.updateUser(user);
  }

  public int insertUser(User user){
    return userRepository.addUser(user);
  }
}

建Repository類

package com.example.mybatisDemo.repository;

import com.example.mybatisDemo.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * Mapper注解能省去以前復(fù)雜的xml配置,直接用注解寫sql語句
 * 不添加Repository注解依賴注入會報錯(不影響運行),強迫癥還是加上吧
 */
@Mapper
@Repository
public interface UserRepository {
  @Select("select * from user")
  List<User> getUsers();

  @Delete("delete from user where id = #{id}")
  int deleteUser(long id);

  @Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
  int addUser(User user);

  @Update("update user set name=#{name},pwd=#{pwd} where id = #{id}")
  int updateUser(User user);

  @Select("select * from user where id = #{id}")
  User getUser(long id);
}

看完上述內(nèi)容,你們對使用SpringBoot對Mybatis進(jìn)行整合實現(xiàn)注解開發(fā)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

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

AI