springboot添加操作
阅读原文时间:2023年07月12日阅读:1

更多精彩关注微信公众号

Mybaits技术连接数据库

resources

#update tomcat port
server.port=8888

#config datasource(mysql)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=秘密

#config mybatis xml
mybatis.mapper-location=classpath:mapping/*.xml
mybatis.type-aliases-package=cn.java.entit

创建数据库

DDL信息

Create Table

CREATE TABLE `foods` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`food_name` varchar(40) DEFAULT NULL COMMENT '食品名',
`food_taste` varchar(2) DEFAULT NULL COMMENT '食品口味 1香辣 2微辣 3麻辣 4不辣',
`food_price` float(10,2) DEFAULT NULL COMMENT '食品价格',
`food_description` varchar(200) DEFAULT NULL COMMENT '食品描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

INSERT INTO foods(food_name,food_taste,food_price,food_description) VALUES('鱼香肉丝','1',18.5,'很好吃'); 

DAO层

实体类    cn.java.entity       Food.java

package cn.java.entity;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class Food {
private Long id;
private String foodName;
private String foodTaste;
private Float foodPrice;
private String foodDescription;
}

映射mapper    cn.java.mapper  FoodMaper.java

package cn.java.mapper;

import cn.java.entity.Food;
import org.apache.ibatis.annotations.Insert;

/*
增删改返回值为数字 所以设置int
食品 添加数据 DAO层完成
*/
public interface FoodMapper {
@Insert("INSERT INTO foods(food_name,food_taste,food_price,food_description) VALUES(#{foodName},#{foodTaste},#{foodPrice},#{foodDescription})")
int addFood(Food food);
}

业务层

cn.java.service     FoodService.java  接口

package cn.java.service;

import cn.java.entity.Food;

/*
添加食品
*/
public interface FoodService {
boolean saveFood(Food food);
}

实现类  cn.java.service.impl    FoodServiceImpl   实现接口 FoodService

package cn.java.service.impl;

import cn.java.entity.Food;
import cn.java.mapper.FoodMapper;
import cn.java.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FoodServiceImpl implements FoodService {
@Autowired
private FoodMapper foodMapper;
@Override
public boolean saveFood(Food food) {
return foodMapper.addFood(food) >=1 ? true : false; //业务方法 若是项目记者校验数据格式
}
}

控制层

cn.java.controller

FoodController.java

package cn.java.controller;

import cn.java.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.java.entity.Food;
/*
添加食品 项目开发 多添加注释
*/
@Controller //添加@Controller 才是一个Controller
public class FoodController {
@Autowired
private FoodService foodService;
@RequestMapping("/insertFood.do") //添加虚拟路径
@ResponseBody
public boolean insertFood(Food food) {
return foodService.saveFood(food);
}

}

pom.xml

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4\_0\_0.xsd">
4.0.0
springboot2
dingpeng_springboot2
war
0.0.1-SNAPSHOT
dingpeng_springboot2 Maven Webapp
http://maven.apache.org

<!-- 配置 spring boot 所需的依赖包 -->  
<parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>1.5.10.RELEASE</version>

</parent>

<dependencies>  
    <!-- springboot 相关启动 -->  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
    </dependency>

    <!-- spring boot热部署 为开发者提供了一个名为spring-boot-devtools的模块来使用Spring boot 应用支持热部署  
        提高开发者的开发效率无需手动重启Spring boot 应用 -->  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-devtools</artifactId>  
        <optional>true</optional>  
    </dependency>

    <!-- log4j -->  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-log4j2</artifactId>  
    </dependency>

    <!-- 使用Jasper 引擎解析JSP -->  
    <dependency>  
        <groupId>org.apache.tomcat.embed</groupId>  
        <artifactId>tomcat-embed-jasper</artifactId>  
        <scope>provided</scope>  
    </dependency>

    <!-- JSP 支持 -->  
    <!-- servlet 依赖 -->  
    <dependency>  
        <groupId>javax.servlet</groupId>  
        <artifactId>javax.servlet-api</artifactId>  
        <scope>provided</scope>  
    </dependency>  
    <dependency>  
        <groupId>javax.servlet</groupId>  
        <artifactId>jstl</artifactId>  
    </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>  
    </dependency>

    <!-- lombok -->  
    <dependency>  
        <groupId>org.projectlombok</groupId>  
        <artifactId>lombok</artifactId>  
        <version>1.16.16</version>  
        <scope>provided</scope>  
    </dependency>

    <!-- 加入p6spy的依赖 -->  
    <!-- https://mvnrepository.com/artifact/p6spy/p6spy -->  
    <!--<dependency>  
        <groupId>p6spy</groupId>  
        <artifactId>p6spy</artifactId>  
        <version>2.1.4</version>  
    </dependency>-->

<!--     druid 数据库连接池  
    <dependency>  
        <groupId>com.alibaba</groupId>  
        <artifactId>druid</artifactId>  
        <version>1.1.10</version>  
    </dependency>-->  
</dependencies>

<!-- 简化部署 这个插件,可将应用打包成一个可执行的jar包 -->  
<build>  
    <plugins>  
        <plugin>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-maven-plugin</artifactId>  
            <configuration>  
                <!-- 没有这个配置, devtools 不生效 -->  
                <fork>true</fork>  
            </configuration>  
        </plugin>  
    </plugins>  
    <finalName>dingpeng\_springboot2</finalName>  
</build>

使用的工具

Postman

结构图