转载请标明出处:

原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-mybatis/

本文出自方志朋的博客

本文主要讲解如何在springboot下整合mybatis,并访问数据库。由于mybatis这个框架太过于流行,所以我就不讲解了。

引入依赖

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

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

引入数据库连接依赖:

<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配置文件中引入数据源:

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就可以访问数据了。

创建数据库表

建表语句:

-- 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');

具体实现

这篇文篇通过注解的形式实现。

创建实体:

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层,构建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测试通过。

源码下载:https://github.com/forezp/SpringBootLearning

参考资料

mybatis

MyBatis整合




扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客

SpringBoot非官方教程 | 第六篇:springboot整合mybatis的更多相关文章

  1. SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源

    这篇文章主要介绍,通过Spring Boot整合Mybatis后如何实现在一个工程中实现多数据源.同时可实现读写分离. 准备工作 环境: windows jdk 8 maven 3.0 IDEA 创建 ...

  2. (转)SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

    springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事事务,引入它们依赖的时候,事物就 ...

  3. SpringBoot非官方教程 | 第二十三篇: 异步方法

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot-ansy/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  4. (转)SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql

    本文介绍springboot通过jdbc访问关系型MySQL,通过spring的JdbcTemplate去访问. 准备工作 jdk 1.8 maven 3.0 idea mysql 初始化mysql: ...

  5. SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

    转载请标明出处: http://blog.csdn.net/forezp/article/details/70833629 本文出自方志朋的博客 springboot开启事务很简单,只需要一个注解@T ...

  6. SpringBoot非官方教程 | 第八篇:springboot整合mongodb

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/ 本文出自方志朋的博客 这篇文 ...

  7. SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot5-beatlsql/ 本文出自方志朋的博客 Be ...

  8. SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot4-jpaJ/ 本文出自方志朋的博客 JPA全称J ...

  9. SpringBoot非官方教程 | 第二十篇: 处理表单提交

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-form/ 本文出自方志朋的博客 这篇文件主要介 ...

随机推荐

  1. 彻底消除wine中文乱码,QQ,kugoo等等....

    原文链接:http://forum.ubuntu.org.cn/viewtopic.php?t=290155 lendylongli wine下中文的配置方案步骤:1. 初始设置运行 winecfg, ...

  2. CMDB认识和需求分析

    一.认识ITIL   ITIL即IT基础架构库(Information Technology Infrastructure Library,信息技术基础架构库)由英国政府部门CCTA(Central ...

  3. abc098D Xor Sum 2(two point)

    题意 题目链接 给出一个序列,求出有多少区间满足\(A[l] \oplus A[l+1] \oplus \dots \oplus A[r] = A[l] + A[l + 1] +\dots+ A[r] ...

  4. 图片小精灵 & 解决同时给一个元素设置背景问题 &jq登录注册切换

    图片小精灵,当有整张图片时可以通过图片小精灵设置图标. 例如 <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  5. asp.net后台获取html控件的值

    1.asp.net后台获取前台type=text控件的值 前台:<input name="txtName" class="username" type=& ...

  6. 利用actionscript访问wfs服务

    以后整理……</> private function search_clickHandler():void{ op="search"; var urlLoader:UR ...

  7. wxpython CustomTreeCtrl

    转自 http://xoomer.virgilio.it/infinity77/Phoenix/lib.agw.customtreectrl.CustomTreeCtrl.html这个网址中有许多控件 ...

  8. eclipse 误删文件的恢复,代码的恢复

    误删除文件的恢复 在用eclipse进行代码编写操作时,有时会误删除文件或者文件包.通过eclipse的恢复文件功能可以恢复误删除的文件. 具体步骤为: 1.选择误删除文件在eclipse所在包(文件 ...

  9. Django问卷调查项目思路流程

    Django问卷调查项目思路流程: 1 后端思路 : 需求分析 ---- 找出各实体对应关系 ---- 设计model架构 ---- 统一资源封装 --- 提供资源API入口 ---- 设计项目实体功 ...

  10. C#并行编程 z

    目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C#并行编程-线程同步原语 C#并行编程-PLINQ:声明式数据并行 背景 基于任务的程序 ...