一个简朴的增删改查Spring boot项目教程(完整过程,附代码)(从搭建数据
这里将会介绍怎么去搭建一个简朴增删改查的Springboot项目,认真看完我相信你肯定能够学会,并且附有完整代码;一、搭建数据库
起主要进行增删改查肯定是要有供操作的数据库;
这里我是用的SQLyog来搭建的,任意用什么都可以,只要能确保给项目一个配套的数据库就行;
1.建数据库
https://i-blog.csdnimg.cn/blog_migrate/6883aa39a0689bab10dcdea349ca4ca8.png
CREATE DATABASE springBoot;
2.建表
https://i-blog.csdnimg.cn/blog_migrate/62d9307b85f6de8dc6d55f70796729bb.png
CREATE TABLE USER(
NAME VARCHAR(30),
id INT(11),
sex VARCHAR(10)
) DEFAULT CHARSET=utf8
二、新建IDEA项目
打开IDEA,创建一个新项目,准备开始动手
https://i-blog.csdnimg.cn/blog_migrate/3c5933a275239e957eb263f8b67a9a18.png
然后选择插件
https://i-blog.csdnimg.cn/blog_migrate/bc1e21632ad9f249998684d308bade6e.png
https://i-blog.csdnimg.cn/blog_migrate/a52ac4f3c0f1caeb4b583f461e145f4d.png
点击创建
在pom.xml里导入Lombok依靠
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
然后搭建四层架构
controller,mapper,pojo,service
https://i-blog.csdnimg.cn/blog_migrate/a0abc92d69150350bd41a7ebfc77b457.png
创建mapper的映射文件
https://i-blog.csdnimg.cn/blog_migrate/d9046386863dee27becd255380d2ca9e.png
三、连接数据库
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=com.mysql.cj.jdbc.Driver
#??mybatis
mybatis.type-aliases-package=com.example.springboot.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
https://i-blog.csdnimg.cn/blog_migrate/d5cc5a1e8d27aacb971856cc4dea0032.png
2.连接MySQL
https://i-blog.csdnimg.cn/blog_migrate/cad2aed5c86cc51b222c024d178f346a.png
https://i-blog.csdnimg.cn/blog_migrate/1cb0e73e5ced77a766be4c2a35b5128a.png
选择我们之前建好的数据库
https://i-blog.csdnimg.cn/blog_migrate/ef761be4ac7d4d8bbb512215fd7e99a1.png
点击Apply和OK后,我们就可以看到我们连接了数据库
https://i-blog.csdnimg.cn/blog_migrate/e6eecd503b890047f9ee01efb2f4ce04.png
四、写代码
经过前面的准备工作后终于可以开始写代码了;
这里我就只介绍一个接口(查找全部用户),其他的都是差不多的流程,文章末端附完整代码;
pojo层
https://i-blog.csdnimg.cn/blog_migrate/0cf01f197683ad5b29ed18e6de29c951.png
实体类里的属性要与数据表一一对应;
mapper层
https://i-blog.csdnimg.cn/blog_migrate/72de18fe35d7a475f4961485bf5ae99d.png
查询全部用户是要返回多个User,所以用List;
Service层
https://i-blog.csdnimg.cn/blog_migrate/3087e314043946c7c7ff928b7d0c982d.png
先用@Autowired注入userMapper;
然后声明方法;
Controller层
https://i-blog.csdnimg.cn/blog_migrate/a9f9032cf4c8b4da07090d0e37c1af91.png
同样注入
然后调用Service层
写mapper层的映射文件
将查询的SQL语句写上
https://i-blog.csdnimg.cn/blog_migrate/2401b0db4bf85b9b6575839bab1ded28.png
到此这个接口就算是写完了;
五、测试
我建议是拿Apifox来测试,本身拿网页测试也行;
先手动添加一些数据到数据库里去;
https://i-blog.csdnimg.cn/blog_migrate/36fb3ff958797b038d9444ee75f55fc7.png
Apifox测试结果(没Apifox直接跳过)
https://i-blog.csdnimg.cn/blog_migrate/e8a8771d1ba4929e719c3708b13c7d4f.png
网页测试
注:肯定要先让项目保持运行状态
打开欣赏器,访问接口路径:
http://localhost:8080/queryUserList
https://i-blog.csdnimg.cn/blog_migrate/890f41a023805992f25f98aa6336ee26.png
六、完整代码
UserController
package com.example.springboot.controller;
import com.example.springboot.pojo.User;
import com.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 com.example.springboot.mapper;
import com.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 com.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 com.example.springboot.service;
import com.example.springboot.mapper.UserMapper;
import com.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="com.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=com.mysql.cj.jdbc.Driver
#??mybatis
mybatis.type-aliases-package=com.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>com.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>com.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-compiler-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>com.example.springboot.Application</mainClass> <skip>true</skip> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]