企业分布式微服务云SpringCloud SpringBoot mybatis (二十一)构建restful API

引入依赖

在pom文件引入mybatis-spring-boot-starter的依赖:

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter<artifactId>
            <version>1.3.0</version>
        </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>

  

引入数据源

application.properties配置文件中引入数据源:

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

  

这样,springboot就可以访问数据了。

创建数据库表

建表语句:

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

  

dao层

@Mapper
public interface AccountMapper {

    @Insert("insert into account(name, money) values(#{name}, #{money})")
    int add(@Param("name") String name, @Param("money") double money);

    @Update("update account set name = #{name}, money = #{money} where id = #{id}")
    int update(@Param("name") String name, @Param("money") double money, @Param("id") int  id);

    @Delete("delete from account where id = #{id}")
    int delete(int id);

    @Select("select id, name as name, money as money from account where id = #{id}")
    Account findAccount(@Param("id") int id);

    @Select("select id, name as name, money as money from account")
    List<Account> findAccountList();
}

  

service层

@Service
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;

    public int add(String name, double money) {
        return accountMapper.add(name, money);
    }
    public int update(String name, double money, int id) {
        return accountMapper.update(name, money, id);
    }
    public int delete(int id) {
        return accountMapper.delete(id);
    }
    public Account findAccount(int id) {
        return accountMapper.findAccount(id);
    }
    public List<Account> findAccountList() {
        return accountMapper.findAccountList();
    }
}

  

controller层,构建restful API

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;

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

    @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";
       }

    }

}

  

通过postman测试通过。

源码来源

原文地址:https://www.cnblogs.com/Abbie/p/8440688.html

时间: 2024-08-11 20:19:27

企业分布式微服务云SpringCloud SpringBoot mybatis (二十一)构建restful API的相关文章

企业分布式微服务云SpringCloud SpringBoot mybatis (十四)服务注册(consul)

这篇文章主要介绍 spring cloud consul 组件,它是一个提供服务发现和配置的工具.consul具有分布式.高可用.高扩展性. 一.consul 简介 consul 具有以下性质: 服务发现:consul通过http 方式注册服务,并且服务与服务之间相互感应. 服务健康监测 key/value 存储 多数据中心 consul可运行在mac windows linux 等机器上. 二.consul安装 linux $ mkdir -p $GOPATH/src/github.com/h

企业分布式微服务云SpringCloud SpringBoot mybatis (六)分布式配置中心

一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中.在spring cloud config 组件中,分两个角色,一是config server,二是config client.二.构建Config Server 创建一个spring-boot项目,取名为config-se

企业分布式微服务云SpringCloud SpringBoot mybatis (三) 服务消费者(Feign)

一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解.Feign支持可插拔的编码器和解码器.Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果. 简而言之: Feign 采用的是基于接口的注解 Feign 整合了ribbon 二.准备工作 继续用上一节的工程, 启动eureka-server,端口为8761; 启动se

企业分布式微服务云SpringCloud SpringBoot mybatis (四)断路器(Hystrix)

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务通常会集群部署.由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪.服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的"雪

企业分布式微服务云SpringCloud SpringBoot mybatis (十三)Spring Boot整合MyBatis

Spring中整合MyBatis就不多说了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步骤.搜了一下Spring Boot整合MyBatis的文章,方法都比较老,比较繁琐.查了一下文档,实际已经支持较为简单的整合与使用.下面就来详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 整合MyBatis 新建Spring Boot项目,或以Chapter1为基础来操作 pom.xml中引入依赖 这里用到spring-bo

企业分布式微服务云SpringCloud SpringBoot mybatis (十四)Spring Boot中使用MyBatis注解配置详解

传参方式 下面通过几种不同传参方式来实现前文中实现的插入操作. 使用@Param 在之前的整合示例中我们已经使用了这种最简单的传参方式,如下: @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})") int insert(@Param("name") String name, @Param("age") Integer age); 这种方式很好理解,@Param中定义的name

企业分布式微服务云SpringCloud SpringBoot mybatis (十六)Spring Boot中使用LDAP来统一管理用户信息

LDAP简介 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门针对读取,浏览和搜索操作进行了特定的优化.目录一般用来包含描述性的,基于属性的信息并支持精细复杂的过滤能力.目录一般不支持通用数据库针对大量更新操作操作需要的复杂的事务管理或回卷策略.而目录服务的更新则一般都非常简单.这种目录可以存储包括个人信息.web链结.jpeg图像等各种信息.为了访问存储在目录中的

企业分布式微服务云SpringCloud SpringBoot mybatis (十九)Spring Boot 自定义配置文件

上面介绍的是我们都把配置文件写到application.yml中.有时我们不愿意把配置都写到application配置文件中,这时需要我们自定义配置文件,比如test.properties: com.forezp.name=forezp com.forezp.age=12 怎么将这个配置文件信息赋予给一个javabean呢? @Configuration @PropertySource(value = "classpath:test.properties") @Configuratio

企业分布式微服务云SpringCloud SpringBoot mybatis (十八)springboot在启动时注入了哪些bean

在程序入口加入: @SpringBootApplication public class SpringbootFirstApplication { public static void main(String[] args) { SpringApplication.run(SpringbootFirstApplication.class, args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ct