成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

SpringBoot非官方教程 | 第六篇:SpringBoot整合mybatis

Doyle / 569人閱讀

摘要:本文主要講解如何在下整合,并訪問數(shù)據(jù)庫(kù)。由于這個(gè)框架太過于流行,所以我就不講解了。創(chuàng)建數(shù)據(jù)庫(kù)表建表語(yǔ)句具體實(shí)現(xiàn)這篇文篇通過注解的形式實(shí)現(xiàn)。創(chuàng)建實(shí)體層層層,構(gòu)建通過測(cè)試通過。源碼下載參考資料整合

本文主要講解如何在springboot下整合mybatis,并訪問數(shù)據(jù)庫(kù)。由于mybatis這個(gè)框架太過于流行,所以我就不講解了。

引入依賴

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

   
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        1.3.0
    

引入數(shù)據(jù)庫(kù)連接依賴:


    mysql
    mysql-connector-java
    runtime


    com.alibaba
    druid
    1.0.29

引入數(shù)據(jù)源

application.properties配置文件中引入數(shù)據(jù)源:

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就可以訪問數(shù)據(jù)了。

創(chuàng)建數(shù)據(jù)庫(kù)表

建表語(yǔ)句:

-- 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");
具體實(shí)現(xiàn)

這篇文篇通過注解的形式實(shí)現(xiàn)。

創(chuàng)建實(shí)體
public class Account {
private int id ;
private String name ;
private double money;

setter…
getter…
}

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 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 findAccountList() {
        return accountMapper.findAccountList();
    }
}
controller層,構(gòu)建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 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測(cè)試通過。

源碼下載:https://github.com/forezp/Spr...

參考資料

mybatis

MyBatis整合

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70353.html

相關(guān)文章

  • 一起來學(xué)SpringBoot | 六篇整合SpringDataJpa

    摘要:忽略該字段的映射省略創(chuàng)建數(shù)據(jù)訪問層接口,需要繼承,第一個(gè)泛型參數(shù)是實(shí)體對(duì)象的名稱,第二個(gè)是主鍵類型。 SpringBoot 是為了簡(jiǎn)化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動(dòng)裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕易的搭建出一個(gè) WEB 工程 上一篇介紹了Spring JdbcTempl...

    Dionysus_go 評(píng)論0 收藏0
  • Java3y文章目錄導(dǎo)航

    摘要:前言由于寫的文章已經(jīng)是有點(diǎn)多了,為了自己和大家的檢索方便,于是我就做了這么一個(gè)博客導(dǎo)航。 前言 由于寫的文章已經(jīng)是有點(diǎn)多了,為了自己和大家的檢索方便,于是我就做了這么一個(gè)博客導(dǎo)航。 由于更新比較頻繁,因此隔一段時(shí)間才會(huì)更新目錄導(dǎo)航哦~想要獲取最新原創(chuàng)的技術(shù)文章歡迎關(guān)注我的公眾號(hào):Java3y Java3y文章目錄導(dǎo)航 Java基礎(chǔ) 泛型就這么簡(jiǎn)單 注解就這么簡(jiǎn)單 Druid數(shù)據(jù)庫(kù)連接池...

    KevinYan 評(píng)論0 收藏0
  • SpringBoot進(jìn)階教程 | 第三篇:整合Druid連接池以及Druid監(jiān)控

    摘要:這篇文篇將介紹,如何通過整合數(shù)據(jù)庫(kù)鏈接池實(shí)時(shí)監(jiān)控?cái)?shù)據(jù)庫(kù)鏈接信息,為優(yōu)化數(shù)據(jù)庫(kù)性能提供更好的指導(dǎo),同樣將通過配置文件形式進(jìn)行配置方便簡(jiǎn)潔。 這篇文篇將介紹,如何通過SpringBoot整合Druid數(shù)據(jù)庫(kù)鏈接池,實(shí)時(shí)監(jiān)控?cái)?shù)據(jù)庫(kù)鏈接信息,為優(yōu)化數(shù)據(jù)庫(kù)性能提供更好的指導(dǎo),同樣將通過YML配置文件形式進(jìn)行配置,方便簡(jiǎn)潔。 準(zhǔn)備工作 環(huán)境: windows jdk 8 maven 3.0 IDE...

    Ilikewhite 評(píng)論0 收藏0
  • SpringBoot進(jìn)階教程 | 第四篇:整合Mybatis實(shí)現(xiàn)多數(shù)據(jù)源

    這篇文章主要介紹,通過Spring Boot整合Mybatis后如何實(shí)現(xiàn)在一個(gè)工程中實(shí)現(xiàn)多數(shù)據(jù)源。同時(shí)可實(shí)現(xiàn)讀寫分離。 準(zhǔn)備工作 環(huán)境: windows jdk 8 maven 3.0 IDEA 創(chuàng)建數(shù)據(jù)庫(kù)表 在mysql中創(chuàng)建student庫(kù)并執(zhí)行下面查詢創(chuàng)建student表 -- ---------------------------- -- Table structure for stud...

    AZmake 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<