博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot非官方教程 | 第六篇:springboot整合mybatis
阅读量:5130 次
发布时间:2019-06-13

本文共 4475 字,大约阅读时间需要 14 分钟。

转载请标明出处:

原文首发于:
本文出自

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

引入依赖

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

org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0

引入数据库连接依赖:

mysql
mysql-connector-java
runtime
com.alibaba
druid
1.0.29

引入数据源

application.properties配置文件中引入数据源:

spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver

这样,springboot就可以访问数据了。

创建数据库表

建表语句:

-- create table `account`# DROP TABLE `account` IF EXISTSCREATE 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层

@Mapperpublic 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
findAccountList();}

service层

@Servicepublic 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
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
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测试通过。

源码下载:

参考资料

SouthEast

扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 )

转载于:https://www.cnblogs.com/forezp/p/9852139.html

你可能感兴趣的文章
[草稿]挂载新硬盘
查看>>
[USACO 2017 Feb Gold] Tutorial
查看>>
关于mysql中GROUP_CONCAT函数的使用
查看>>
OD使用教程20 - 调试篇20
查看>>
Java虚拟机(JVM)默认字符集详解
查看>>
Java Servlet 过滤器与 springmvc 拦截器的区别?
查看>>
(tmp >> 8) & 0xff;
查看>>
linux命令之ifconfig详细解释
查看>>
NAT地址转换
查看>>
Nhibernate 过长的字符串报错 dehydration property
查看>>
Deque - leetcode 【双端队列】
查看>>
人物角色群体攻击判定(一)
查看>>
一步步学习微软InfoPath2010和SP2010--第九章节--使用SharePoint用户配置文件Web service(2)--在事件注册表单上创建表单加载规则...
查看>>
gulp插件gulp-ruby-sass和livereload插件
查看>>
免费的大数据学习资料,这一份就足够
查看>>
clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight
查看>>
MySQL(一)
查看>>
企业级应用与互联网应用的区别
查看>>
steelray project viewer
查看>>
itext jsp页面打印
查看>>