1:创建项目
2:创建项目文件结构、选择jdk版本
通常选择Java version 8
3:添加需要的依赖
4:查看项目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.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cjb</groupId>
<artifactId>springboot1219</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot1219</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--web相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis相关-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<!--mysql相关-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--改代码表示在编译时囊括src/main/resources文件下的
.properties
.xml
.yml文件
防止编译出错-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
5:创建数据库表
create table Goods(
GoodsName varchar(20) not null ,
goodsPrice double not null ,
goodsNum int not null ,
goodsType int not null ,
remark varchar(20)
)charset="utf8" comment="货物表"
6:pojo文件下创建Goods.java与数据库表一一对应
package com.cjb.pojo;
import lombok.Data;
@Data
public class Goods {
private String goodsName;
private double goodsPrice;
private Integer goodsNum;
private Integer goodsType;
private String remark;
}
7:编写application.yml
#指定端口
server:
port: 8888
#配置数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver # 指定数据库驱动
url: jdbc:mysql://localhost:3306/hospital?characterEncoding=utf8 # 指定url
username: root # 指定用户名
password: 123 # 指定密码
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.cjb.pojo
#输出日志文件sql语句日志文件
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 初始化dispatcherServlet,默认-1未开启,0及以上为开启
8:mapper包下编写GoodsMapper接口
package com.cjb.mapper;
import com.cjb.pojo.Goods;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
public interface GoodsMapper {
/*
* 查询所有
* */
List<Goods> findAll();
/*删除*/
int delete(String goodsName);
/*新增*/
int Insert(Goods goods);
/*修改*/
int update(Goods goods);
/*连表查询所有*/
List<Goods> findAllType();
}
9:service业务层
import java.util.List; @Service public class GoodsServiceImpl implements GoodsService { @Autowired private GoodsMapper goodsMapper; @Override public List<Goods> findAll() { return goodsMapper.findAll(); } @Override public int delete(String goodsName) { return goodsMapper.delete(goodsName); } @Override public int Insert(Goods goods) { return goodsMapper.Insert(goods); } @Override public int update(Goods goods) { return goodsMapper.update(goods); }
10:在src/main/resources/mapping文件夹下新建GoodsMapper的映射文件GoodsMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cjb.mapper.GoodsMapper"> <resultMap id="toGoods" type="Goods"> <association property="type" javaType="type"> <result property="typeName" column="typeName"/> </association> </resultMap> <select id="findAll" resultType="Goods"> select * from goods </select> <delete id="delete" parameterType="String"> delete from goods where GoodsName=#{goodsName} </delete> <insert id="Insert" parameterType="Goods"> INSERT INTO goods (`GoodsName`, `goodsPrice`, `goodsNum`, `goodsType`) VALUES (#{goodsName},#{goodsPrice},#{goodsNum},#{goodsType}); </insert> <update id="update" parameterType="Goods"> UPDATE goods <set> <if test="goodsPrice!=null"> goodsPrice=#{goodsPrice}, </if> <if test="goodsNum!=null"> goodsNum=#{goodsNum}, </if> <if test="goodsType!=null"> goodsType=#{goodsType}, </if> </set> where GoodsName=#{goodsName} </update> <select id="findAllType" resultMap="toGoods"> select * from goods g left join Type t on t.typeId=g.goodsType </select> </mapper>
11:在controller包中新建访问类GoodsController.java
@Controller public class GoodsController { @Autowired private GoodsService goodsService; @RequestMapping("/GetAll") @ResponseBody public String getAll(){ List<Goods> all = goodsService.findAll(); System.out.println(all.toString()); return all.toString(); /*return R.ok().code(200).data("all",all);*/ } @RequestMapping("/delete") @ResponseBody public Integer delete(@RequestParam(value = "goodsName")String goodsName){ int delete = goodsService.delete(goodsName); /*return R.ok().code(200).code(delete);*/ return delete; } @RequestMapping("/Insert") @ResponseBody public Integer Insert(Goods goods){ int delete = goodsService.Insert(goods); /*return R.ok().code(200).code(delete);*/ return delete; } @RequestMapping("/update") @ResponseBody public Integer update(Goods goods){ int delete = goodsService.update(goods); /*return R.ok().code(200).code(delete);*/ return delete; }}