摘要:本文主要讲解如何在下整合,并访问数据库。由于这个框架太过于流行,所以我就不讲解了。创建数据库表建表语句具体实现这篇文篇通过注解的形式实现。创建实体层层层,构建通过测试通过。源码下载参考资料整合
本文主要讲解如何在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/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 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 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测试通过。
源码下载:https://github.com/forezp/Spr...
参考资料mybatis
MyBatis整合
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/70353.html
摘要:忽略该字段的映射省略创建数据访问层接口,需要继承,第一个泛型参数是实体对象的名称,第二个是主键类型。 SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程 上一篇介绍了Spring JdbcTempl...
摘要:前言由于写的文章已经是有点多了,为了自己和大家的检索方便,于是我就做了这么一个博客导航。 前言 由于写的文章已经是有点多了,为了自己和大家的检索方便,于是我就做了这么一个博客导航。 由于更新比较频繁,因此隔一段时间才会更新目录导航哦~想要获取最新原创的技术文章欢迎关注我的公众号:Java3y Java3y文章目录导航 Java基础 泛型就这么简单 注解就这么简单 Druid数据库连接池...
摘要:这篇文篇将介绍,如何通过整合数据库链接池实时监控数据库链接信息,为优化数据库性能提供更好的指导,同样将通过配置文件形式进行配置方便简洁。 这篇文篇将介绍,如何通过SpringBoot整合Druid数据库链接池,实时监控数据库链接信息,为优化数据库性能提供更好的指导,同样将通过YML配置文件形式进行配置,方便简洁。 准备工作 环境: windows jdk 8 maven 3.0 IDE...
这篇文章主要介绍,通过Spring Boot整合Mybatis后如何实现在一个工程中实现多数据源。同时可实现读写分离。 准备工作 环境: windows jdk 8 maven 3.0 IDEA 创建数据库表 在mysql中创建student库并执行下面查询创建student表 -- ---------------------------- -- Table structure for stud...
阅读 3197·2021-11-18 10:07
阅读 4057·2021-11-17 17:00
阅读 2280·2021-11-15 18:01
阅读 1267·2021-10-11 10:58
阅读 3881·2021-09-10 10:50
阅读 4019·2021-08-13 15:05
阅读 1400·2019-08-30 15:53
阅读 2828·2019-08-29 13:01