您好,登錄后才能下訂單哦!
這篇文章主要講解了“springboot整合mybatis并訪問數(shù)據(jù)庫的過程”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“springboot整合mybatis并訪問數(shù)據(jù)庫的過程”吧!
在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>
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ù)了。
建表語句:
-- 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');
這篇文篇通過注解的形式實現(xiàn)。
public class Account { private int id ; private String name ; private double money; setter... getter... }
@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 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(); } }
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測試通過。
感謝各位的閱讀,以上就是“springboot整合mybatis并訪問數(shù)據(jù)庫的過程”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對springboot整合mybatis并訪問數(shù)據(jù)庫的過程這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。