Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一、前言
什么是MyBatis?
MyBatis是目前Java平台最为流行的ORM框架
https://baike.baidu.com/item/MyBatis/2824918本篇开发环境
1、操作系统: Windows 10 X64
2、Java SDK: jdk-8u141
3、Maven:3.5
4、IDE:IntelliJ IDEA 2017
5、Spring Boot:1.5.6
本项目构建基于:https://ken.io/note/springboot-course-basic-helloworld
二、Spring Boot整合MyBatis
- 引入核心依赖
| package | 说明 |
|---|---|
| mybatis-spring-boot-starter | MyBatis核心for Spring Boot |
| mysql-connector-java | 用于连接MySQL |
pom.xml文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
- 配置数据库连接
在配置文件:application.yml中增加以下配置:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/course?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
username: mysql
password: password
数据库自行创建MySQL下载地址:https://dev.mysql.com/downloads/
- Package创建
| Package | 说明 |
|---|---|
| io.ken.springboot.course.model | 用于存放实体 |
| io.ken.springboot.course.dao | 用于存放数据访问映射*mapper |
- user表&实体创建
1、user表创建脚本
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`hobby` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、user实体
package io.ken.springboot.course.model;
public class User {
private int id;
private String name;
private int age;
private String hobby;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
getger和setter可以选中类名之后使用快捷键Alt+Insert生成
- 创建UserMapper,用于User数据库操作映射
package io.ken.springboot.course.dao;
import io.ken.springboot.course.model.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User queryById(@Param("id") int id);
@Select("SELECT * FROM user")
List<User> queryAll();
@Insert({"INSERT INTO user(name,age,hobby) VALUES(#{name},#{age},#{hobby})"})
int add(User user);
@Delete("DELETE FROM user WHERE id = #{id}")
int delById(int id);
@Update("UPDATE user SET name=#{name},age=#{age},hobby=#{hobby} WHERE id = #{id}")
int updateById(User user);
}
- 创建UserController并提供API
package io.ken.springboot.course.controller;
import io.ken.springboot.course.dao.UserMapper;
import io.ken.springboot.course.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping("/querybyid")
@ResponseBody
User queryById(int id) {
return userMapper.queryById(id);
}
@RequestMapping("/queryall")
@ResponseBody
List<User> queryAll() {
return userMapper.queryAll();
}
@RequestMapping("/add")
@ResponseBody
String add(User user) {
return userMapper.add(user) == 1 ? "success" : "failed";
}
@RequestMapping("/updatebyid")
@ResponseBody
String updateById(User user) {
return userMapper.updateById(user) == 1 ? "success" : "failed";
}
@RequestMapping("/delbyid")
@ResponseBody
String delById(int id) {
return userMapper.delById(id) == 1 ? "success" : "failed";
}
}
- API测试
| API | 示例 |
|---|---|
| 添加用户 | /user/add?name=tom&age=1&hobby=football |
| 更新用户 | /user/updatebyid?name=ken&age=18&hobby=coding&id=1 |
| 查询指定用户 | /user/querybyid?id=1 |
| 查询所有用户 | /user/queryall |
| 删除指定用户 | /user/delbyid?id=2 |
本文代码示例:https://github.com/ken-io/springboot-course/tree/master/chapter-02-01
- 系列名称:Spring Boot入门教程
- 下一篇:Spring Boot入门教程2-2、使用Spring Boot+MyBatis访问数据库(CURD)xml配置版
- 本篇首次发布:2017-08-15
- 本篇原文链接:https://ken.io/note/springboot-course-basic-curd-annotation
Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版的更多相关文章
- Spring Boot入门教程1、使用Spring Boot构建第一个Web应用程序
一.前言 什么是Spring Boot?Spring Boot就是一个让你使用Spring构建应用时减少配置的一个框架.约定优于配置,一定程度上提高了开发效率.https://zhuanlan.zhi ...
- Spring Boot 入门教程
Spring Boot 入门教程,包含且不仅限于使用Spring Boot构建API.使用Thymeleaf模板引擎以及Freemarker模板引擎渲染视图.使用MyBatis操作数据库等等.本教程示 ...
- Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis
https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...
- Spring Boot入门教程(1)
Spring Boot入门教程(1) 本文将使用Spring Boot一步步搭建一个简单的Web项目来帮助你快速上手. 将要用到的工具 JDK 8 IntelliJ IDEA(Ultimate Edi ...
- Spring Cloud 入门教程 - 搭建配置中心服务
简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...
- Spring Cloud 入门教程(七): 熔断机制 -- 断路器
对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断路器如何使用. SpringCloud Netflix实现了断路器库 ...
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...
- Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine
1. Hystrix Dashboard (断路器:hystrix 仪表盘) Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboa ...
- Spring Cloud 入门教程(十):和RabbitMQ的整合 -- 消息总线Spring Cloud Netflix Bus
在本教程第三讲Spring Cloud 入门教程(三): 配置自动刷新中,通过POST方式向客户端发送/refresh请求, 可以让客户端获取到配置的最新变化.但试想一下, 在分布式系统中,如果存在很 ...
随机推荐
- MySQL语法大全整理的自学笔记
select * from emp; #注释 #--------------------------- #----命令行连接MySql--------- #启动mysql服务器 net start m ...
- CentOS添加磁盘分区
(这里的磁盘在vmware workstation VM中添加) 1.关闭虚拟机,在虚拟机设置中添加一个硬盘,然后开启虚拟机. 2.使用fdisk -l命令查看,这时会发现一个为被使用的设备,有2G的 ...
- 外网如何访问 Service?- 每天5分钟玩转 Docker 容器技术(139)
除了 Cluster 内部可以访问 Service,很多情况我们也希望应用的 Service 能够暴露给 Cluster 外部.Kubernetes 提供了多种类型的 Service,默认是 Clus ...
- Java反射机制应用实践
反射基础 在应用反射机制之前,首先我们先来看一下如何获取一个对象对应的反射类Class,在Java中我们有三种方法可以获取一个对象的反射类. 通过getClass方法 在Java中,每一个Object ...
- 从“跳一跳”来看微信小程序的未来
从“跳一跳”来看微信小程序的未来 相信大家这两天都被微信新推出的小程序跳一跳刷爆了朋友圈,为了方便用户在使用过程中切换小程序,微信在这次6.6.1版本中加入了下拉可快速切换小程序的功能,而“跳一跳 ...
- 第二篇:使用Spark对MovieLens的特征进行提取
前言 在对数据进行了初步探索后,想必读者对MovieLens数据集有了感性认识.而在数据挖掘/推荐引擎运行前,往往需要对数据预处理.预处理的重要性不言而喻,甚至比数据挖掘/推荐系统本身还重要. 然而完 ...
- 【CJOJ1603】【洛谷1220】关路灯
题面 Description 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老常就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉 ...
- 从FCN到DeepLab
图像语义分割,简单而言就是给定一张图片,对图片上的每一个像素点分类. 图像语义分割,从FCN把深度学习引入这个任务,一个通用的框架事:前端使用FCN全卷积网络输出粗糙的label map,后端使用CR ...
- TP5模型关联问题
在使用模型关联时:假如有表 merchant商户表 m_store 店铺表 m_store_ref 商户店铺关联表 user 普通用户表 $mer = Merchant::with([ ' ...
- c# ffmpeg视频转换【转载】
c# ffmpeg视频转换 什么是ffmpeg,它有什么作用呢,怎么可以使用它呢,带着问题去找答案吧!先参考百度百科把,我觉得它很强大无奇不有,为了方便大家我就把链接提供了! http://baik ...