java实现商品信息的录入,查找,修改,删除的接口(用Postman测试,连接数据库看表)
程序员文章站
2022-07-13 13:37:37
...
作为一个前端,学习Java接口实现原理是有必要的,这篇文章就会教一个前端如何用IDEA实现商品信息的录入,查询,修改,删除,查看数据库。
java实现商品信息的录入,查找,修改,删除的接口
一、四步构建项目
-
第一步:打开idea,点击文件,接着点项目
-
第二步:
-
第三步:
-
第四步:
二、构建目录文件
目录如下:
三、重点编写代码:
- 在pom.xml文件中添加数据库的相关依赖。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
- 第一个依赖是 mysql 数据库
- 第二个依赖是 tk.mybatis,mybatis 包
- 第三个依赖是 org.mybatis.spring.boot,主要用来集成 mybatis 和 spring 框架
2.注意配置中对数据库的连接在(src/main/resources/application.properties文件)
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/exchange?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.example.product.entity
spring.datasource.url:数据库服务器地址。
useUnicode:使用的编码格式。
spring.datasource.username:数据库登陆用户名。
spring.datasource.password:数据库登陆密码。
spring.datasource.driver-class-name:数据库连接驱动类。
mybatis.mapper-locations:mybatis 映射扫描路径。
mybatis.type-aliases-package:实体扫描路径。
3.在ProductMapper.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.product.mapper.ProductMapper">
<resultMap id="BaseResultMap" type="com.example.product.entity.Product">
<result column="product_id" jdbcType="VARCHAR" property="productId" />
<result column="product_name" jdbcType="VARCHAR" property="productName" />
<result column="product_price" jdbcType="DOUBLE" property="productPrice" />
<result column="product_type" jdbcType="INTEGER" property="productType" />
<result column="product_img" jdbcType="VARCHAR" property="productImg" />
<result column="product_des" jdbcType="VARCHAR" property="productDes" />
</resultMap>
<insert id="addProduct" parameterType="com.example.product.entity.Product">
INSERT INTO `product` (`product_name`,`product_price`,`product_type`,`product_img`,`product_des`) VALUES(#{productName},#{productPrice},#{productType},#{productImg},#{productDes})
</insert>
<select id="getProductList" resultMap="BaseResultMap">
SELECT * FROM `product`
</select >
<select id="getProductByKey" resultMap="BaseResultMap">
SELECT * FROM `product` where product_name like concat('%',#{productName},'%') or product_des like concat('%',#{productName},'%')
</select >
<select id="getProductByCondition" resultMap="BaseResultMap">
SELECT * FROM `product`
<where>
<if test="productName != null and productName != ''">
and product_name like concat('%',#{productName},'%')
</if>
<if test="productType != null and productType != -1">
and product_type = #{productType}
</if>
</where>
</select >
<update id="updateProduct" parameterType="com.example.product.entity.Product">
update product
<trim prefix="SET" suffixOverrides=",">
<if test="null != pro.productName and '' != pro.productName">
product_name=#{pro.productName},
</if>
<if test="null != pro.productType and -1!= pro.productType">
product_type=#{pro.productType},
</if>
<if test="null != pro.productPrice and -1!= pro.productPrice">
product_price=#{pro.productPrice},
</if>
<if test="null != pro.productImg and '' != pro.productImg">
product_img=#{pro.productImg},
</if>
<if test="null != pro.productDes and '' != pro.productDes">
product_des=#{pro.productDes},
</if>
</trim>
where product_id=#{pro.productId}
</update>
<delete id="deleteProduct">
DELETE FROM product WHERE product_id = #{productId}
</delete>
</mapper>
4.需要在ProductMapper还需要添加@Mapper注解
@Repository
@Mapper
public interface ProductMapper {
int addProduct(Product product);
List<Product> getProductList();
}
5.需要在ProductApplication中加入
@ComponentScan(basePackages = {"com.example.product"})
@SpringBootApplication
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
}
四、实际上,本项目是商品的新增。查询所有,按照条件查询,修改商品,删除商品,对本地数据库名为 exchange的库中的,product表的不断插入和查询.注意用postman测试的几个问题(其中有个415的问题)
1.新增商品的测试
注意的postman中Header中的设置,否则报错415
2.查所有的的测试
3.条件查询的测试
4.修改的测试
5.删除的测试
6.数据库的展示