摘要:本文主要講解如何在下整合,并訪問數(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ù)連接依賴:
引入數(shù)據(jù)源mysql mysql-connector-java runtime com.alibaba druid 1.0.29
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") Listservice層findAccountList(); }
@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 Listcontroller層,構(gòu)建restful APIfindAccountList() { return accountMapper.findAccountList(); } }
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 ListgetAccounts() { 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
摘要:忽略該字段的映射省略創(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...
摘要:前言由于寫的文章已經(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ù)連接池...
摘要:這篇文篇將介紹,如何通過整合數(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...
這篇文章主要介紹,通過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...
閱讀 3049·2021-11-18 10:07
閱讀 3801·2021-11-17 17:00
閱讀 2128·2021-11-15 18:01
閱讀 959·2021-10-11 10:58
閱讀 3423·2021-09-10 10:50
閱讀 3555·2021-08-13 15:05
閱讀 1249·2019-08-30 15:53
閱讀 2673·2019-08-29 13:01