①首先配置java的环境,也就是下载jdk1.8
验证是否安装成功:
②下载apache maven
③用idea创建工程
配置如下:
编码配置:
删除无用图片:
改写文件后缀:
④用postman测试接口
postman下载地址:Download Postman | Get Started for Free
先尝试创建一个数据库:
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '名称',
`age` int(11) NULL DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '张三', 20);
INSERT INTO `user` VALUES (2, '李四', 21);
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`mc` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '名称',
`price` double(10, 2) NULL DEFAULT NULL COMMENT '价格',
`user_id` int(11) NULL DEFAULT NULL COMMENT '用户id',
PRIMARY KEY (`id`) USING BTREE,
INDEX `user_id`(`user_id`) USING BTREE,
CONSTRAINT `book_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 27 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '书籍表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES (7, '红楼梦', 66.01, 1);
INSERT INTO `book` VALUES (8, '西游记三', 55.00, 2);
INSERT INTO `book` VALUES (9, '三国演义', 33.00, 2);
INSERT INTO `book` VALUES (10, '三国演义', 34.00, 2);
INSERT INTO `book` VALUES (11, '红楼梦', 66.01, 1);
INSERT INTO `book` VALUES (12, '西游记三', 55.00, 2);
INSERT INTO `book` VALUES (13, '三国演义', 33.00, 2);
INSERT INTO `book` VALUES (14, '三国演义', 34.00, 2);
INSERT INTO `book` VALUES (15, '红楼梦', 66.01, 1);
INSERT INTO `book` VALUES (16, '西游记三', 55.00, 2);
INSERT INTO `book` VALUES (17, '三国演义', 33.00, 2);
INSERT INTO `book` VALUES (18, '三国演义', 34.00, 2);
INSERT INTO `book` VALUES (19, '红楼梦', 66.01, 1);
INSERT INTO `book` VALUES (20, '西游记三', 55.00, 2);
INSERT INTO `book` VALUES (21, '三国演义', 33.00, 2);
INSERT INTO `book` VALUES (22, '三国演义', 34.00, 2);
INSERT INTO `book` VALUES (23, '红楼梦', 66.01, 1);
INSERT INTO `book` VALUES (24, '西游记三', 55.00, 2);
INSERT INTO `book` VALUES (25, '三国演义', 33.00, 2);
INSERT INTO `book` VALUES (26, '三国演义', 34.00, 2);
然后在application.properties文件编写如下代码:
server.port=9090//这里是设置自己想要运行的端口号
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
//springboot是我设置在3306端口的数据库名
spring.datasource.username=root
spring.datasource.password=123456
然后创建如下图文件:
WebController:
package com.example.springboot.controller;
import com.example.springboot.entity.Book;
import com.example.springboot.mapper.BookMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class WebController {
@Resource
BookMapper bookMapper;
@RequestMapping("/") // / 是可以省略的
public String hello1233() {
return "你好啊";
}
@RequestMapping(value = "/user" , method = RequestMethod.POST)
public void insertBook(@RequestBody Book book){
bookMapper.insert(book);
}
@RequestMapping(value = "/user", method = RequestMethod.PUT)
public void updateBook(@RequestBody Book book) {
bookMapper.updateById(book);
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public List<Book> listBook() {
return bookMapper.listBook();
}
}
Book:
package com.example.springboot.entity;
public class Book {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMc() {
return mc;
}
public void setMc(String mc) {
this.mc = mc;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
private String mc;
private Double price;
private Integer user_id;
}
BookMapper:
package com.example.springboot.mapper;
import com.example.springboot.entity.Book;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
@Mapper
public interface BookMapper {
@Insert("INSERT INTO book (mc, price, user_id) VALUES(#{mc}, #{price}, #{user_id})")
void insert(Book book);
@Update("UPDATE book SET mc = #{mc} , price = #{price} , user_id=#{user_id} where id=#{id}")
void updateById(Book book);
@Select("select * from book order by id desc ")
List<Book> listBook();
}
运行项目:
增加数据:
修改数据:
获取数据: