亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot整合Mybatis注解開發(fā)的實現代碼

 更新時間:2020年11月25日 10:36:38   作者:Breeze_4379  
這篇文章主要介紹了SpringBoot整合Mybatis注解開發(fā)的實現代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

官方文檔:

https://mybatis.org/mybatis-3/zh/getting-started.html

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?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注解能省去以前復雜的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);
}

然后直接調用即可

這里有個問題,使用注解開發(fā)的話sql語句全寫在注解里,那么如果要實現批量更新插入要怎么寫呢,目前還沒解決,找到辦法再更

到此這篇關于SpringBoot整合Mybatis注解開發(fā)的實現代碼的文章就介紹到這了,更多相關SpringBoot整合Mybatis注解開發(fā)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot啟動yaml報錯的解決

    SpringBoot啟動yaml報錯的解決

    這篇文章主要介紹了SpringBoot啟動yaml報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • JAVA泛型的繼承和實現、擦除原理解析

    JAVA泛型的繼承和實現、擦除原理解析

    這篇文章主要介紹了JAVA泛型的繼承和實現、擦除原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • springSecurity用戶認證和授權的實現

    springSecurity用戶認證和授權的實現

    Spring?Security?是一個開源的安全框架,提供了基于權限的訪問控制、身份認證的功能,本文主要介紹了springSecurity用戶認證和授權,具有一定參考價值,感興趣的可以了解一下
    2024-04-04
  • Mybatis攔截器實現數據權限詳解

    Mybatis攔截器實現數據權限詳解

    這篇文章主要介紹了Mybatis攔截器實現數據權限詳解, 通過Mybatis攔截器我們可以攔截某些方法的調用,我們可以選擇在這些被攔截的方法執(zhí)行前后加上某些邏輯,需要的朋友可以參考下
    2023-11-11
  • 深度deepin安裝以及jdk、tomcat、Nginx安裝教程

    深度deepin安裝以及jdk、tomcat、Nginx安裝教程

    這篇文章主要給大家介紹了關于深度deepin安裝以及jdk、tomcat、Nginx安裝的相關資料,按照文中介紹的方法可以輕松的實現安裝,對大家的工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-01-01
  • Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)

    Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)

    這篇文章主要介紹了Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Java中Base64加密解密舉例詳解

    Java中Base64加密解密舉例詳解

    Base64編碼是我們程序開發(fā)中經常使用到的編碼方法,它是一種基于用64個可打印字符來表示二進制數據的表示方法,這篇文章主要給大家介紹了關于Java中Base64加密解密的相關資料,需要的朋友可以參考下
    2024-05-05
  • 教你怎么在IDEA中創(chuàng)建java多模塊項目

    教你怎么在IDEA中創(chuàng)建java多模塊項目

    這篇文章主要介紹了教你怎么在IDEA中創(chuàng)建java多模塊項目,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java WeakHashMap案例詳解

    Java WeakHashMap案例詳解

    這篇文章主要介紹了Java WeakHashMap案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • RocketMQ4.5.X 實現修改生產者消費者日志保存路徑

    RocketMQ4.5.X 實現修改生產者消費者日志保存路徑

    這篇文章主要介紹了RocketMQ4.5.X 實現修改生產者消費者日志保存路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論