这里将会介绍怎么去搭建一个简单增删改查的Springboot项目,认真看完我相信你一定能够学会,并且附有完整代码;
一、搭建数据库
首先要进行增删改查肯定是要有供操作的数据库;
这里我是用的SQLyog来搭建的,随便用什么都可以,只要能确保给项目一个配套的数据库就行;
1.建数据库
CREATE DATABASE springBoot;
2.建表
CREATE TABLE USER(
NAME VARCHAR(30),
id INT(11),
sex VARCHAR(10)
) DEFAULT CHARSET=utf8
二、新建IDEA项目
打开IDEA,创建一个新项目,准备开始动手
然后选择插件
点击创建
在pom.xml里导入Lombok依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
然后搭建四层架构
controller,mapper,pojo,service
创建mapper的映射文件
三、连接数据库
1.编写文件
在resources包下找到application.properties,将如下代码写入:
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=***.mysql.cj.jdbc.Driver
#??mybatis
mybatis.type-aliases-package=***.example.springboot.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
2.连接MySQL
选择我们之前建好的数据库
点击Apply和OK后,我们就可以看到我们连接了数据库
四、写代码
经过前面的准备工作后终于可以开始写代码了;
这里我就只介绍一个接口(查找所有用户),其他的都是差不多的流程,文章末尾附完整代码;
pojo层
实体类里的属性要与数据表一一对应;
mapper层
查询所有用户是要返回多个User,所以用List;
Service层
先用@Autowired注入userMapper;
然后声明方法;
Controller层
同样注入
然后调用Service层
写mapper层的映射文件
将查询的SQL语句写上
到此这个接口就算是写完了;
五、测试
我建议是拿Apifox来测试,自己拿网页测试也行;
先手动添加一些数据到数据库里去;
Apifox测试结果(没Apifox直接跳过)
网页测试
注:一定要先让项目保持运行状态
打开浏览器,访问接口路径:
http://localhost:8080/queryUserList
六、完整代码
UserController
package ***.example.springboot.controller;
import ***.example.springboot.pojo.User;
import ***.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping ("/queryUserList")
public List<User> queryUserList(){
return userService.queryUserList();
}
@GetMapping ("queryUserById")
public User queryUserById(int user_id){
return userService.queryUserById(user_id);
}
@GetMapping("/addUser")
public String addUser(User user){
return userService.addUser(user);
}
@GetMapping("/deleteUserById")
public String deleteUserById(int id){
return userService.deleteUserById(id);
}
}
UserMapper
package ***.example.springboot.mapper;
import ***.example.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
void addUser(User user);
void deleteUserById(int id);
}
User
package ***.example.springboot.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private int id;
private String sex;
}
UserService
package ***.example.springboot.service;
import ***.example.springboot.mapper.UserMapper;
import ***.example.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public List<User> queryUserList(){
return userMapper.queryUserList();
}
public User queryUserById(int id){
User user= userMapper.queryUserById(id);
return user;
}
public String addUser(User user){
userMapper.addUser(user);
return "新增成功";
}
public String deleteUserById(int id){
userMapper.deleteUserById(id);
return "删除成功";
}
}
UserMapper.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 namespace="***.example.springboot.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from springboot.user
</select>
<select id="queryUserById" resultType="User" parameterType="int">
select * from springboot.user where id = #{id};
</select>
<insert id="addUser">
insert into springboot.user (name, id, sex) VALUES (#{name},#{id},#{sex})
</insert>
<delete id="deleteUserById">
delete from springboot.user where id=#{id}
</delete>
</mapper>
application.properties
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=***.mysql.cj.jdbc.Driver
#??mybatis
mybatis.type-aliases-package=***.example.springboot.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
server.port=8080
pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>***.example</groupId>
<artifactId>springBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot</name>
<description>springBoot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>***.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-***piler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>***.example.springboot.Application</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>