后端Springboot项目开发
步骤1 先创建数据库,并在下面创建一个user表,插入数据,sql如下:
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`email` varchar(255) NOT NULL COMMENT '邮箱',
`password` varchar(255) NOT NULL COMMENT '密码',
`username` varchar(255) NOT NULL COMMENT '姓名',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', '1@qq.com', '123456', '张三');
INSERT INTO `user` VALUES ('2', '2@qq.com', '234567', '李四');
INSERT INTO `user` VALUES ('3', '3@qq.com', '345678', '王五');
步骤2 创建一个springboot项目,然后在pom文件中引入相关依赖:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>zht_backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zht_backend</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!--io工具类-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--运用SpringBoot 插件 使用spring-boot-devtools模块的应用,当classpath中的文件有改变时,会自动重启!-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
步骤3 配置文件application.yml,内容如下:
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ceshi?serverTimezone=GMT%2b8
username: root
password: root
redis:
host: 127.0.0.1
port: 6379
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
mybatis:
mapper-locations: classpath:mapper/*.xml #扫描所有mybatis的xml文件
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
files:
upload:
path: D:/files/
# path: /home/files/
步骤4 开始编写核心逻辑代码,新建一个User.java实体类,代码如下:
package com.example.zht_backend.bean;
/**
* 功能描述
*
* @author: 衍生星球
* @date: 2023年06月26日 8:37
*/
public class User {
private long id;
private String email;
private String password;
private String username;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
然后再新建一个dao文件:
package com.example.zht_backend.dao;
import com.example.zht_backend.bean.User;
import org.apache.ibatis.annotations.*;
import org.springframework.data.repository.query.Param;
import java.util.List;
@Mapper
public interface UserDao {
/**
* 新增数据
*/
@Insert("insert into user(id,email,password,username) values (#{id},#{email},#{password},#{username})")
void addUser(User user);
/**
* 修改数据
*/
@Update("update user set username=#{username},password=#{password} where id=#{id}")
void updateUser(User user);
/**
* 删除数据
*/
@Delete("delete from user where id=#{id}")
void deleteUser(int id);
/**
* 根据查询数据
*
*/
@Select("select id,email,password,username from user where username=#{userName}")
User findByName(@Param("userName") String userName);
/**
* 查询所有数据
*/
@Select("select id,email,password,username FROM user")
List<User> findAll();
}
然后就是service和UserServiceImpl文件:
userservice:
package com.example.zht_backend.service;
import com.example.zht_backend.bean.User;
import java.util.List;
public interface UserService {
/**
* 新增用户
* @param user
* @return
*/
boolean addUser(User user);
/**
* 修改用户
* @param user
* @return
*/
boolean updateUser(User user);
/**
* 删除用户
* @param id
* @return
*/
boolean deleteUser(int id);
/**
* 根据名字查询用户信息
* @param userName
*/
User findUserByName(String userName);
/**
* 查询所有数据
* @return
*/
List<User> findAll();
}
UserServiceImpl文件:
package com.example.zht_backend.service.impl;
import com.example.zht_backend.bean.User;
import com.example.zht_backend.dao.UserDao;
import com.example.zht_backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 功能描述
*
* @author: 衍生星球
* @date: 2023年06月26日 8:45
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public boolean addUser(User user) {
boolean flag=false;
try {
userDao.addUser(user);
flag=true;
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
@Override
public boolean updateUser(User user) {
boolean flag=false;
try{
userDao.updateUser(user);
flag=true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
@Override
public boolean deleteUser(int id) {
boolean flag=false;
try{
userDao.deleteUser(id);
flag=true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
@Override
public User findUserByName(String userName) {
return userDao.findByName(userName);
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
}
最后就是controller文件:
package com.example.zht_backend.controller;
import com.example.zht_backend.bean.User;
import com.example.zht_backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 功能描述
*
* @author: 衍生星球
* @date: 2023年06月26日 8:52
*/
@RestController
@RequestMapping(value = "/do/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user", method = RequestMethod.POST)
public boolean addUser(@RequestBody User user) {
System.out.println("新增数据:");
return userService.addUser(user);
}
@RequestMapping(value = "/user", method = RequestMethod.PUT)
public boolean updateUser(@RequestBody User user) {
System.out.println("更新数据:");
return userService.updateUser(user);
}
@RequestMapping(value = "/user", method = RequestMethod.DELETE)
public boolean delete(@RequestParam(value = "id", required = true) int Id) {
System.out.println("删除数据:");
return userService.deleteUser(Id);
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public User findByUserName(@RequestParam(value = "userName", required = true) String userName) {
System.out.println("查询数据:");
return userService.findUserByName(userName);
}
@RequestMapping(value = "/userAll", method = RequestMethod.GET)
public List<User> findByUserAge() {
System.out.println("查询所有数据:");
return userService.findAll();
}
}
然后启动项目,进行测试:
这里后端基本就开发完成了。