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

轉(zhuǎn)載請(qǐng)標(biāo)明出處:
http://blog.csdn.net/forezp/article/details/70768477
本文出自方志朋的博客

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

引入依賴

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

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

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

<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>

引入數(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ù)庫表

建表語句:

-- 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<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層爬虱,構(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<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測(cè)試通過。

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

參考資料

mybatis

MyBatis整合

優(yōu)秀文章推薦:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末秉溉,一起剝皮案震驚了整個(gè)濱河市力惯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌召嘶,老刑警劉巖父晶,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異弄跌,居然都是意外死亡甲喝,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門铛只,熙熙樓的掌柜王于貴愁眉苦臉地迎上來埠胖,“玉大人,你說我怎么就攤上這事淳玩≈背罚” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵凯肋,是天一觀的道長谊惭。 經(jīng)常有香客問我,道長侮东,這世上最難降的妖魔是什么圈盔? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮悄雅,結(jié)果婚禮上驱敲,老公的妹妹穿的比我還像新娘。我一直安慰自己宽闲,他們只是感情好众眨,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著容诬,像睡著了一般娩梨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上览徒,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天狈定,我揣著相機(jī)與錄音,去河邊找鬼。 笑死纽什,一個(gè)胖子當(dāng)著我的面吹牛措嵌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播芦缰,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼企巢,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了让蕾?” 一聲冷哼從身側(cè)響起浪规,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎涕俗,沒想到半個(gè)月后罗丰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體神帅,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡再姑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了找御。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片元镀。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖霎桅,靈堂內(nèi)的尸體忽然破棺而出栖疑,到底是詐尸還是另有隱情,我是刑警寧澤滔驶,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布遇革,位于F島的核電站,受9級(jí)特大地震影響揭糕,放射性物質(zhì)發(fā)生泄漏萝快。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一著角、第九天 我趴在偏房一處隱蔽的房頂上張望揪漩。 院中可真熱鬧,春花似錦吏口、人聲如沸奄容。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昂勒。三九已至,卻和暖如春舟铜,著一層夾襖步出監(jiān)牢的瞬間戈盈,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國打工深滚, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奕谭,地道東北人涣觉。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像血柳,于是被迫代替她去往敵國和親官册。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容