java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot整合Mybatis注解开发

SpringBoot整合Mybatis注解开发的实现代码

作者:Breeze_4379

这篇文章主要介绍了SpringBoot整合Mybatis注解开发的实现代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

官方文档:

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);
}

然后直接调用即可

这里有个问题,使用注解开发的话sql语句全写在注解里,那么如果要实现批量更新插入要怎么写呢,目前还没解决,找到办法再更

到此这篇关于SpringBoot整合Mybatis注解开发的实现代码的文章就介绍到这了,更多相关SpringBoot整合Mybatis注解开发内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文