springboot 实现增删改查
一、创建springboot工程
创建一个springboot项目,可以实现helloworld
二、编写目录结构
新建目录结构如下图
三、开始编写
编写一个简单的增删改查
1.数据库创建
在本地安装mysql,创建一个叫mysql的数据库,新建表t_student,表头如下
2.pom.xml依赖
引入mybatis 和mysql 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- pom模型版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 项目信息 -->
<groupId>com.example</groupId> <!-- 项目唯一标识 -->
<artifactId>springboot</artifactId> <!-- 项目名 -->
<version>0.0.1-SNAPSHOT</version> <!-- 版本 -->
<packaging>jar</packaging> <!-- 打包方式 (pom,war,jar) -->
<name>springboot</name> <!-- 项目的名称, Maven 产生的文档用 -->
<description>Demo project for Spring Boot</description> <!-- 项目的描述, Maven 产生的文档用 -->
<!-- 父级项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version> <!-- springboot版本 -->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 属性设置 -->
<properties>
<java.version>1.8</java.version> <!-- jdK版本 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 编译字符编码为utf-8 -->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!-- 输出字符编码为UTF-8 -->
<druid.version>1.1.14</druid.version>
</properties>
<!-- 依赖关系 -->
<dependencies>
<!-- springboot-web启动所需依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot-单元测试启动所需依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis持久层框架,操纵数据库-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql数据库驱动 去掉版本号-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--这个插件可以将应用打包成一个可执行的jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.application.yml文件配置
路径:/resources/application.yml
#启动端口配置
server:
port: 8088
# 数据源配置
spring:
application:
name: SpringbootApplication
datasource:
url: jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 #mysql数据库的地址
username: dbname #数据库用户
password: ******* #数据库密码
driverClassName: com.mysql.cj.jdbc.Driver #报红的话在pom.xml加mysql驱动的依赖
## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.example.springboot.entity # 注意:对应实体类的路径
4.编写实体类
路径:/entity/Student.java
package com.example.springboot.entity;
/*
* @Date 等于 Setter and Getter ;需要引入lombok依赖
* */
import lombok.Data;
@Data
public class Student{
/*
* id 自增
* */
private int id;
private String name;
private Integer age;
private String sex;
@ApiModelProperty(value = "查询条件")
//该字段不存在Student表中
@TableField(exist = false)
private String condition;
}
5.编写mapper接口
路径:/mapper/StudentMapper.java ;新建为接口类型,为**.xml访问数据库提供接口
package com.example.springboot.mapper;
import com.example.springboot.entity.Student;
import org.apache.ibatis.annotations.Mapper;
/*@Mapper
* 1:为了把mapper这个DAO交給Spring管理
* 2:为了不再写mapper映射文件
* 3:为了给mapper接口 自动根据一个添加@Mapper注解的接口生成一个实现类
* */
@Mapper
public interface StudentMapper {
void insertStu(@Param("student") Student student);
}
6.编写Dao的实现
路径:/resourse/mapper/StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- 这里填写对应的mapper文件所在的路径 -->
<mapper namespace="com.example.springboot.mapper.StudentMapper" >
<!-- 填写数据库里实例Person对应的表的表名 -->
<!-- 这里是作为一个变量使用 -->
<!-- id属性填写mapper文件里的函数名称 xxType是参数或是结果的类型根据情况填写 -->
<!-- 增 -->
<insert id="insertStu" parameterType="com.example.springboot.entity.Student">
INSERT INTO t_student (name,sex,age)
VALUES (#{student.name},#{student.sex}, #{student.age})
</insert>
</mapper>
7.编写Service接口
路径:/service/StudentService.java ;接口形式
package com.example.springboot.service;
import com.example.springboot.entity.Student;
public interface StudentService {
void insertStu(Student student);
}
8.编写Service接口的实现
路径:/service/impl/StudentServiceImpl.java
package com.example.springboot.service.impl;
import com.example.springboot.entity.Student;
import com.example.springboot.mapper.StudentMapper;
import com.example.springboot.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class StudentServiceImpl implements StudentService {
//Service和Dao层连接
@Resource
public StudentMapper studentMapper;
@Override
public void insertStu(Student student) {
studentMapper.insertStu(student);
}
}
9.编写Controller
路径:/Controller/StudentController.java
package com.example.springboot.controller;
import com.example.springboot.entity.Student;
import com.example.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
public StudentService studentService;
@PostMapping("/insert")
public void insertStu(@RequestBody Student student){
studentService.insertStu(student);
}
}
10.运行使用postman调用
postman调用
数据库中显示