实验目的:掌握基于SpringBoot的CRUD增删改查操作
实验步骤如下:
1.创建Spring Boot项目:
首先,在Eclipse中创建一个新的Maven项目。在创建项目时,选择不使用STS插件,而是使用普通的Maven项目。
2.添加依赖:
在项目的pom.xml文件中添加以下依赖:
<!-- Spring Boot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MySQL驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3.配置数据库连接:
在项目的application.properties(或application.yml)文件中配置数据库连接信息,例如:
properties
Copy code
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
4.创建实体类和Mapper接口:
创建Product实体类和对应的ProductMapper接口,用于定义数据库操作方法。
public class Product {
private Long id;
private String name;
private BigDecimal price;
// getters and setters
}
public interface ProductMapper {
void insert(Product product);
void update(Product product);
void delete(Long id);
Product getById(Long id);
List<Product> getAll();
}
5.创建Mapper XML文件:
在resources目录下创建ProductMapper.xml文件,并编写对应的SQL语句。
<mapper namespace="com.example.mapper.ProductMapper">
<insert id="insert" parameterType="com.example.entity.Product">
INSERT INTO product (name, price) VALUES (#{name}, #{price})
</insert>
<update id="update" parameterType="com.example.entity.Product">
UPDATE product SET name = #{name}, price = #{price} WHERE id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Long">
DELETE FROM product WHERE id = #{id}
</delete>
<select id="getById" parameterType="java.lang.Long" resultType="com.example.entity.Product">
SELECT * FROM product WHERE id = #{id}
</select>
<select id="getAll" resultType="com.example.entity.Product">
SELECT * FROM product
</select>
</mapper>
6.创建Service类和Controller类:
创建ProductService类和ProductController类,用于处理业务逻辑和接收请求。
@Service
public class ProductService {
private final ProductMapper productMapper;
public ProductService(ProductMapper productMapper) {
this.productMapper = productMapper;
}
public void addProduct(Product product) {
productMapper.insert(product);
}
public void updateProduct(Product product) {
productMapper.update(product);
}
public void deleteProduct(Long id) {
productMapper.delete(id);
}
public Product getProductById(Long id) {
return productMapper.getById(id);
}
public List<Product> getAllProducts() {
return productMapper.getAll();
}
}
@RestController
@RequestMapping("/products")
public class ProductController {
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@PostMapping
public void addProduct(@RequestBody Product product) {
productService.addProduct(product);
}
@PutMapping("/{id}")
public void updateProduct(@PathVariable Long id, @RequestBody Product product) {
product.setId(id);
productService.updateProduct(product);
}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable Long id) {
productService.deleteProduct(id);
}
@GetMapping("/{id}")
public Product getProductById(@PathVariable Long id) {
return productService.getProductById(id);
}
@GetMapping
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
}
7.运行项目:
通过运行Spring Boot应用程序来启动项目,并进行测试。可以使用Postman或其他工具发送HTTP请求,来测试CRUD操作的增删改查功能。
在完成上述实验的过程中,我获得了一些体会与感受:
Spring Boot的简便性:使用Spring Boot可以快速搭建一个可运行的Java项目,并且不需要繁琐的配置。它提供了很多开箱即用的功能和依赖项,使得项目的搭建和开发变得更加简单和高效。
Mybatis的易用性:Mybatis是一款轻量级的持久层框架,它将SQL语句与Java代码解耦,使得数据库操作变得更加灵活和易于维护。使用Mybatis可以通过简单的接口和XML文件来完成数据库的CRUD操作,提高了开发效率。
分层架构的优势:在实验中,我采用了分层架构的方式组织代码,将数据访问层、业务逻辑层和控制层分开,使得代码结构更加清晰和可维护。这种架构能够有效地分离关注点,并提高代码的可读性和可测试性。
接口与实现的分离:通过定义Mapper接口和对应的XML文件,我将数据库操作的接口与具体的实现分离开来。这种设计模式使得代码更加可扩展和可维护,能够方便地修改和替换数据访问层的实现。
实践对理解的重要性:通过实际编码和运行项目,我更深入地理解了Spring Boot和Mybatis的使用方式,以及CRUD操作的具体步骤。实践是加深理解和掌握技能的关键,通过不断地实践和调试,我对Spring Boot和Mybatis的使用有了更深刻的认识。
总的来说,完成这个实验使我更加熟悉了基于Spring Boot的CRUD操作,了解了如何使用Mybatis进行数据库访问,并通过实践加深了对Spring Boot和Mybatis的理解。这些经验和体会将对我的后续项目开发和数据库操作有很大的帮助。