springboot第十三节 springboot整合mybatis,postman测试使用

一、数据库导入

-- create table `account`
# DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `money` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');

二:添加依赖

<dependencies>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.29</version>
		</dependency>



        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
</dependencies>

三、controller层

package com.forezp.web;

import com.forezp.entity.Account;
import com.forezp.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by fangzhipeng on 2017/4/20.
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    AccountService accountService;
/*    http://127.0.0.1:8080/account/list*/

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Account> getAccounts() {
        return accountService.findAccountList();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Account getAccountById(@PathVariable("id") int id) {
        return accountService.findAccount(id);
    }


   /* http://127.0.0.1:8080/account/2   输入name和money 然后可以更新,put方法
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        int t= accountService.update(name,money,id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(value = "id")int id) {
        int t= accountService.delete(id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {

       int t= accountService.add(name,money);
       if(t==1) {
           return "success";
       }else {
           return "fail";
       }

    }


}

 

代码示例:https://gitee.com/dgx555/springboot.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是操作步骤: 1. 创建SpringBoot项目,并添加Mybatis和MySQL依赖。 2. 创建实体类和Mapper接口,并在Mapper接口中定义增删查改的方法。 3. 在application.properties文件中配置数据库连接相关信息。 4. 编写Controller层,调用Mapper接口中的方法,并使用@RequestBody注解接收前端传来的数据。 5. 使用Postman进行测试,发送对应的请求并查看返回结果。 具体的代码实现可以参考以下示例: 实体类: ``` public class User { private int id; private String name; private int age; // 省略getter和setter方法 } ``` Mapper接口: ``` @Mapper public interface UserMapper { List<User> findAll(); User findById(int id); void insert(User user); void update(User user); void delete(int id); } ``` Controller层: ``` @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public List<User> findAll() { return userMapper.findAll(); } @GetMapping("/users/{id}") public User findById(@PathVariable int id) { return userMapper.findById(id); } @PostMapping("/users") public void insert(@RequestBody User user) { userMapper.insert(user); } @PutMapping("/users/{id}") public void update(@PathVariable int id, @RequestBody User user) { user.setId(id); userMapper.update(user); } @DeleteMapping("/users/{id}") public void delete(@PathVariable int id) { userMapper.delete(id); } } ``` 使用Postman进行测试: 1. GET请求,查询所有用户: ``` 请求方式:GET 请求URL:http://localhost:8080/users ``` 2. GET请求,根据ID查询用户: ``` 请求方式:GET 请求URL:http://localhost:8080/users/1 ``` 3. POST请求,新增用户: ``` 请求方式:POST 请求URL:http://localhost:8080/users 请求体:{ "name": "Tom", "age": 20 } ``` 4. PUT请求,修改用户: ``` 请求方式:PUT 请求URL:http://localhost:8080/users/1 请求体:{ "name": "Tom", "age": 21 } ``` 5. DELETE请求,删除用户: ``` 请求方式:DELETE 请求URL:http://localhost:8080/users/1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值