SpringBoot整合MyBatis实现增删改查案例完整版(附源代码)

2021-04-26 04:27

阅读:432

标签:location   deb   def   enable   mtd   path   figure   engine   epo   

1.Spring Boot是一个简化Spring开发的框架。用来监护spring应用开发,约定大于配置,去繁就简,just run 就能创建一个独立的,产品级的应用。

我们在使用Spring Boot时只需要配置相应的Spring Boot就可以用所有的Spring组件,简单的说,spring boot就是整合了很多优秀的框架,不用我们自己手动的去写一堆xml配置然后进行配置。从本质上来说,Spring Boot就是Spring,它做了那些没有它你也会去做的Spring Bean配置。
闲话少说,上图文:

2.开始

  • 新建一个项目
    技术图片
  • 选择Spring Initializr项目
    技术图片
  • 创建项目的文件结构以及jdk的版本,本次以demo为例
    技术图片
  • 选择项目所需要的依赖
    技术图片
  • 修改项目名,finish完成,本次选择为修改

来看一眼pom文件

4.0.0org.springframework.boot
        spring-boot-starter-parent
        2.2.5.RELEASEcom.example
    demo
    0.0.1-SNAPSHOTdemoDemo project for Spring Boot1.8org.springframework.boot
            spring-boot-starter-jdbc
        org.springframework.boot
            spring-boot-starter-web
        org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1mysql
            mysql-connector-java
            runtimeorg.springframework.boot
            spring-boot-starter-test
            testorg.junit.vintage
                    junit-vintage-engine
                org.springframework.boot
                spring-boot-maven-plugin
            
  • 修改配置文件本,文不使用application.properties文件 而使用更加简洁的application.yml文件。
    技术图片
    修改为application.yml
    内容:
server:
  port: 8080

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

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.example.entity.demo

logging:
  level:
    com:
      example:
        mapper : debug

创建表:

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

目录结构:
技术图片
UserController.java

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

/**
 * @author yn2333
 * @Date 2020/03/03
 */

@RestController
@RequestMapping("/test")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/selectUserByid", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public String GetUser(User user){
        return userService.Sel(user).toString();
    }

    @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String Add(User user){
           return userService.Add(user);
    }

    @RequestMapping(value = "/update", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String Update(User user){
        return userService.Update(user);
    }

    @RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String Delete(User user){
        return userService.Delete(user);
    }
}

User.java

package com.example.demo.entity;

/**
 * @author yn2333
 * @Date 2020/03/03
 */
public class User {
    private Integer id;
    private String username;
    private String password;
    private String address;

    public User(Integer id, String username, String password, String address) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getAddress() {
        return address;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username=‘" + username + ‘\‘‘ +
                ", password=‘" + password + ‘\‘‘ +
                ", address=‘" + address + ‘\‘‘ +
                ‘}‘;
    }
}

UserMapper.java

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

/**
 * @author yn2333
 * @Date 2020/03/03
 */
@Repository
public interface UserMapper {

    User Sel(@Param("user")User user);

    int Add(@Param("user")User user);

    int Update(@Param("user")User user);

    int Delete(@Param("user")User user);
}

UserService.java

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yn2333
 * @Date 2020/03/03
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public User Sel(User user) {
        return userMapper.Sel(user);
    }

    public String Add(User user) {
        int a = userMapper.Add(user);
        if (a == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    public String Update(User user) {
        int a = userMapper.Update(user);
        if (a == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    public String Delete(User user) {
        int a = userMapper.Delete(user);
        if (a == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

}

DemoApplication.java

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * @author yn2333
 * @Date 2020/03/03
 */

@MapperScan("com.example.demo.mapper") //扫描的mapper
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

UserMapper.xml


        INSERT INTO user
        
                username,
            
                password,
            
                address,
            
                #{user.username,jdbcType=VARCHAR},
            
                #{user.password,jdbcType=VARCHAR},
            
                #{user.address,jdbcType=VARCHAR},
            
        UPDATE user
        
                username = #{user.username},
            
                password = #{user.password},
            
                address = #{user.address},
            
        WHERE
        id=#{user.id}
    
        DELETE FROM user WHERE id = #{user.id}
    

3.测试

  • 启动
    技术图片
  • 添加:
    127.0.0.1:8080/test/add?username=无极&password=123456&address=上海市
    技术图片
  • 查询:
    127.0.0.1:8080/test/selectUserByid?id=1
    技术图片
  • 修改
    127.0.0.1:8080/test/update?id=1&username=码云&password=654321&address=北京
    技术图片
  • 删除
    127.0.0.1:8080/test/delete?id=1
    技术图片
    增、删、改、查基本测试已经通过,Navicat刷新 之后就能看到效果。
    源代码以及sql文件已打包
    下载方式一:https://download.csdn.net/download/weixin_44395707/12223942
    下载方式二:
    扫下方二维码关注公众号回复:code001即可获取
    技术图片]]>

SpringBoot整合MyBatis实现增删改查案例完整版(附源代码)

标签:location   deb   def   enable   mtd   path   figure   engine   epo   

原文地址:https://www.cnblogs.com/cxyxy/p/13254532.html


评论


亲,登录后才可以留言!