Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (21) Building RESTful APIs

Source: Internet
Author: User

Introducing Dependencies

The Mybatis-spring-boot-starter dependency is introduced in the Pom file:

<dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId> mybatis-spring-boot-starter<artifactid>            <version>1.3.0</version>        </dependency>

  

To introduce database connection dependencies:
<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>

  

Introducing a data source

The data source is introduced into the application.properties configuration file:

Spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username= Rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.driver

  

This allows the springboot to access the data.

Create a database table

To build a table statement:

--Create TABLE ' account ' # DROP table ' account ' IF existscreate table ' account ' (  ' id ' int (one) ' Not NULL auto_increment ,  ' name ' varchar (NOT NULL, ' Money  ' double DEFAULT NULL,  PRIMARY KEY (' id ')) engine=innodb auto_increment =4 DEFAULT Charset=utf8;insert into ' account ' values (' 1 ', ' aaa ', ' + '); INSERT into ' account ' values (' 2 ', ' BBB ', ' 1000 ') INSERT into ' account ' VALUES (' 3 ', ' CCC ', ' 1000 ');

  

DAO layer
@Mapperpublic interface Accountmapper {    @Insert ("Insert into account (name, money) VALUES (#{name}, #{money})"    int Add (@Param ("name") String name, @Param ("money") of 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 Layer
@Servicepublic 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 layer, building restful APIs
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 Accountservic    E @RequestMapping (value = "/list", method = requestmethod.get) public list<account> getaccounts () {return a    Ccountservice.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 i D, @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 = "I        D ") 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 = "nam E ") String name, @RequestParam (value =" money ") double money) {int t= accountservice.a       DD (Name,money);       if (t==1) {return "success";       }else {return "fail"; }    }}

  

Passed through the postman test.

Source Source

Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (21) Building RESTful APIs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.