SpringBoot+Mybatis 增删查改(转)

本文章主要是结合SpringBoot+MyBatis+MySQL来实现数据库的CRUD操作(增删查改),Maven项目工程中主要包括以下6个文件:

User.java:实体类
UserMapper.java:mapper映射器类
UserController.java:控制类
Application.java:启动类
application.properties:配置文件,Spring项目启动自动加载
pom.xml:pom文件

在Eclipse6个文件的位置如下所示:
在这里插入图片描述

注:其中src/main/resources在创建Maven Project时并没有,需要右键->New->Source Folder来添加

首先在MySQL中的Springdb模式下创建user表,包括4个属性: 
id:int ,设置为[主键][6]且[自增][6] 
userName:varchar 
userAge:int 
userAddress:varchar

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.mybatis</groupId>
    <artifactId>springMybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springMybatis</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

User实体类

package com.spring.mybatis.springMybatis.entity;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class User {

    private int id;
    private String userName;
    private int userAge;
    private String userAddress;

}

以上也许有人会奇怪,@Setter和@Getter两个注解是什么意思。顾名思义应该会想到这是getter和setter方法。那么为什么这样做呢?

因为每个实体类都应该包含getter和setter方法才有意义。但是getter和setter方法的代码过于冗余,属于模板类型代码,影响代码阅读。@Setter和@Getter两个注解是属于[lombok][6]的,在pom.xml文件中添加依赖引入lombok:

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.14.4</version>
</dependency>

这样在实体类中添加@Setter和@Getter注解,就可以省略getter和setter方法而达到同样的效果

application.properties

这个文件是放在src/main/resources目录下,当项目启动时,该文件会自动加载,application.properties内容如下:

spring.datasource.url=jdbc:mysql://localhost:3306/springdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

UserMapper.java映射器类

该类主要是用于CRUD操作的,一般有两种方式实现与数据库实现CRUD:

  1. 注解: @Insert、@Select、@Update、@Delete
  2. xml配置

本文中选择的是基于注解的

package com.spring.mybatis.springMybatis.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.spring.mybatis.springMybatis.entity.User;

@Mapper
public interface UserMapper {

    @Select("select * from user where id = #{id}")
    public User selectUserById(int id);

    @Select("select * from user where userName = #{userName}")
    public List<User> selectUserByName(String userName);

    @Insert("insert into user(userName,userAge,userAddress) values (#{userName},#{userAge},#{userAddress})")
    public void addUser(User user);

    @Update("update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}")
    public void updateUser(User user);

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

}

UserController.java 控制器类

package com.spring.mybatis.springMybatis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.spring.mybatis.springMybatis.dao.UserMapper;
import com.spring.mybatis.springMybatis.entity.User;

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

    @Autowired
    UserMapper userMapper;

    @RequestMapping(value={"/selectUserById"}, method=RequestMethod.GET)
    public User selectUserById(String id){
        User user = userMapper.selectUserById(Integer.parseInt(id));
        return user;
    }

    @RequestMapping(value={"/selectUserByName"}, method=RequestMethod.GET)
    public List<User> selectUserByName(String userName){
        return userMapper.selectUserByName(userName);
    }

    @RequestMapping(value={"/addUser"}, method=RequestMethod.POST)
    public void addUser(User user){
        userMapper.addUser(user);
    }

    @RequestMapping(value={"/updateUser"}, method=RequestMethod.POST)
    public void updateUser(User user){
        userMapper.updateUser(user);
    }

    @RequestMapping(value={"/deleteUser"}, method=RequestMethod.POST)
    public void deleteUser(String id){
        userMapper.deleteUser(Integer.parseInt(id));
    }
}

该类是作为请求入口的,通过get/post请求来实现CRUD。

测试
本文采用google浏览器的postman插件来发送get/post请求。

在这里插入图片描述
其他对应的请求如下:

在这里插入图片描述

在这里插入图片描述
post请求后会在mysql中看到数据的改变。

原文地址:https://blog.youkuaiyun.com/baidu_16757561/article/details/75669589

其他整合案例:https://blog.youkuaiyun.com/csdn_study_lp/article/details/79261127

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值