https://blog.csdn.net/forezp/article/details/70341818
本文出自方志朋的博客
本文主要講解如何在springboot下整合mybatis共郭,并訪問數(shù)據(jù)庫(kù)哗戈。由于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ù)庫(kù)連接依賴:
<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ù)庫(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<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
參考資料
寫在最后
歡迎關(guān)注、喜歡找岖、和點(diǎn)贊后續(xù)將推出更多的spring cloud
教程陨倡,敬請(qǐng)期待。
歡迎關(guān)注我的微信公眾號(hào)獲取更多更全的學(xué)習(xí)資源许布,視頻資料兴革,技術(shù)干貨!
公眾號(hào)回復(fù)“學(xué)習(xí)”蜜唾,拉你進(jìn)程序員技術(shù)討論群帖旨,干貨資源第一時(shí)間分享。
公眾號(hào)回復(fù)“視頻”灵妨,領(lǐng)取800GJava視頻學(xué)習(xí)資源。
公眾號(hào)回復(fù)“全棧”落竹,領(lǐng)取1T前端泌霍,Java,產(chǎn)品經(jīng)理述召,微信小程序朱转,Python等資源合集大放送。
公眾號(hào)回復(fù)“慕課”积暖,領(lǐng)取1T慕課實(shí)戰(zhàn)學(xué)習(xí)資源藤为。
公眾號(hào)回復(fù)“實(shí)戰(zhàn)”,領(lǐng)取750G項(xiàng)目實(shí)戰(zhàn)學(xué)習(xí)資源夺刑。
公眾號(hào)回復(fù)“面試”缅疟,領(lǐng)取8G面試實(shí)戰(zhàn)學(xué)習(xí)資源。