SpringBoot第六篇: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 {@Autowiredprivate 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 {@AutowiredAccountService 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
SpringBoot第六篇:springboot整合mybatis相关推荐
- SpringBoot 2.1.5(36)---整合Mybatis
一起来学 SpringBoot 2.x | 第七篇:整合Mybatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射,几乎避免了所有的 JDBC 代码和手动设置参 ...
- SpringBoot SpringBoot 开发实用篇 5 整合第三方技术 5.21 SpringBoot 整合 ActiveMQ
SpringBoot [黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)] SpringBoot 开发实用篇 文章目录 Spring ...
- springboot系列六、springboot配置错误页面及全局异常
springboot系列六.springboot配置错误页面及全局异常 参考文章: (1)springboot系列六.springboot配置错误页面及全局异常 (2)https://www.cnbl ...
- SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源
这篇文章主要介绍,通过Spring Boot整合Mybatis后如何实现在一个工程中实现多数据源.同时可实现读写分离. 准备工作 环境: windows jdk 8 maven 3.0 IDEA 创建 ...
- SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源 1
这篇文章主要介绍,通过Spring Boot整合Mybatis后如何实现在一个工程中实现多数据源.同时可实现读写分离. 准备工作 环境: windows jdk 8 maven 3.0 IDEA 创建 ...
- SpringBoot第八篇:整合MyBatis-Generator
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10894278.html 版权声明:本文为博主原创文章,转载请附上博文链接! 注意:本章有大量代码 ...
- SpringBoot实战(三):整合Mybatis配置多数据源
[前言] 最近接到一个新需求,经过分析后做了相应的设计:其中需要在一个项目中操做不同的数据源:于是进行了相关验证:在此记录一下验证过程. [实战多数据源] 一.Pom中引入相应的Ja ...
- SpringBoot系列六:SpringBoot整合Tomcat
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Tomcat 2.背景 SpringBoot 本身支持有两类的 WEB 容器:默认的 To ...
- 一起来学SpringBoot | 第四篇:整合Thymeleaf模板
SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...
最新文章
- NoSQL那些事--Redis
- Java中HashMap和TreeMap的区别
- 七种qsort排序方法
- win7系统老是弹窗怎么解决_教你win7电脑右下角老是弹出广告的三种解决办法
- iOS dSYM详解和分析crash,ips文件
- 调速水泵控制c语言实验程序,液压控制实验报告
- 硬盘分区的类型:mbr分区和gpt分区的区别
- android 全局剪贴板,Android剪贴板详解
- linux下samba服务器无法访问,解决windows7客户端无法访问Samba服务器的故障
- JavaScript 进阶知识 - 特效篇(一)
- 如何解决fillRect方法画矩形变形的问题?
- html,js实现对联广告
- SNN 脉冲神经网络
- 如何建立异地容灾备份体系
- 数据清洗与处理第二章
- linux和windows内存管理知乎,windows server 哪个版本好 知乎
- SQL查询cross join 的用法(笛卡尔积)
- 正则表达式: 以英文字母开头,只能包含英文字母、数字、下划线
- Hive hql 经典5道面试题
- 全网首发!马士兵内部共享—1658页《Java面试突击核心讲》