springboot项目
课程目标
1. 【掌握】SSM整合
2. 【掌握】使用SSM完成查询
3. 【理解】AdminLTE
4. 【理解】理解SSM综合案例表的结构
springboot环境搭建
搭建工程
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>cn.yanqi</groupId>
<artifactId>springboot-crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-crud</name>
<description>springboot-crud</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.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>cn.yanqi.SpringbootCrudApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
数据库
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`nickname` varchar(25) DEFAULT NULL,
`createdtime` datetime DEFAULT NULL,
`updatedtime` datetime DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
INSERT INTO `test`.`user`(`id`, `username`, `password`, `nickname`, `createdtime`, `updatedtime`) VALUES (6, 'jack', '123', '杰克', '2022-10-15 18:15:05', '2022-10-15 18:15:09');
INSERT INTO `test`.`user`(`id`, `username`, `password`, `nickname`, `createdtime`, `updatedtime`) VALUES (7, 'rose', '123', '肉丝', '2022-10-15 18:15:28', '2022-10-15 18:15:31');
application.yml
# 端口
server:
port: 8080
servlet:
context-path: /
# mybatis
mybatis:
type-aliases-package: cn.yanqi.pojo
mapper-locations: classpath:mappers/*.xml
configuration:
map-underscore-to-camel-case: true #驼峰
# 数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///ssm_crud?useSSL=true
username: root
password: root
thymeleaf:
cache: false #避免改了模板还要重启服务器
mode: HTML5
encoding: UTF-8
servlet:
content-type: text/html
prefix: classpath:/templates/
suffix: .html
# 分页插件
pagehelper:
helper-dialect: mysql
代码实现
pojo实体类
@Data
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
}
controller层
@Controller
public class UserController {
@Autowired
private UserService userService;
/**
* 后台请求,转发到 templates 模版下 取值显示
* @param model
* @return
*/
@RequestMapping("queryAllUser")
public String queryAllUser(Model model){
List<User> userList=userService.queryAllUser();
model.addAttribute("userList",userList);
return "user";
}
}
service层
public interface UserService {
List<User> queryAllUser();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> queryAllUser() {
return this.userMapper.queryAllUser();
}
}
mapper层
@Repository
public interface UserMapper {
List<User> queryAllUser();
}
mapper.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="cn.yanqi.mapper.UserMapper">
<select id="queryAllUser" resultType="User">
select * from user
</select>
</mapper>
启动类
@SpringBootApplication
@MapperScan("cn.yanqi.mapper")//扫描mapper接口
public class SpringbootthymeleafApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootthymeleafApplication.class, args);
}
}
页面
<!DOCTYPE html>
<!--引入thymeleaf-->
<html xmlns:th = "http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1px" cellpadding="1px" cellspacing="0px">
<tr>
<th>序号</th>
<th>姓名</th>
<th>密码</th>
<th>昵称</th>
</tr>
<tr th:each="user:${userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.nickname}"></td>
</tr>
</table>
</body>
</html>
测试
页面导入
导入相关文件
@Controller
@RequestMapping("to")
public class pageController {
@GetMapping("index")
public String toPageant(){
return "index";
}
}
页面效果如下:
springboot综合案例
数据库搭建
产品表(product)
product信息描述
创建product表
CREATE TABLE `product` (
`id` VARCHAR(32) NOT NULL,
`productNum` VARCHAR(50) NOT NULL,
`productName` VARCHAR(50) DEFAULT NULL,
`cityName` VARCHAR(50) DEFAULT NULL,
`departureTime` DATETIME DEFAULT NULL,
`productPrice` INT(11) DEFAULT NULL,
`productDesc` VARCHAR(500) DEFAULT NULL,
`productStatus` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `product`(`id`,`productNum`,`productName`,`cityName`,`DepartureTime`,`productPrice`,`productDesc`,`productStatus`) VALUES
('12B7ABF2A4C544568B0A7C69F36BF8B7','yunhe-003','上海五日游','上海','2018-06-13 23:21:21',1800,'魔都我来了',0),
('676C5BD1D35E429A8C2E114939C5685A','yunhe-002','北京三日游','北京','2020-01-16 19:21:25',1200,'不错的旅行',1),
('9F71F01CB448476DAFB309AA6DF9497F','yunhe-001','北京三日游','北京','2019-06-04 09:21:21',1200,'不错的旅行',1);
会员表(member)
订单与会员之间是多对一关系,我们在订单表中创建一个外键来进行关联。
member表信息描述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eFSmz5Wf-1690118686232)(assets/image-20201014063707863.png)]
member表sql
CREATE TABLE member(
id VARCHAR(32) PRIMARY KEY,
NAME VARCHAR(20),
nickname VARCHAR(20),
phoneNum VARCHAR(20),
email VARCHAR(20)
);
INSERT INTO MEMBER (id, NAME, nickname, phonenum, email)
VALUES ('E61D65F673D54F68B0861025C69773DB', '张三', '小三', '18888888888', '[email protected]');
订单表 (orders)
orders表信息描述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ofbal5CE-1690118686234)(assets/image-20201014063953436.png)]
productId描述了订单与产品之间的关系。
memberid描述了订单与会员之间的关系。
orders表sql
CREATE TABLE orders(
id VARCHAR(32) PRIMARY KEY,
orderNum VARCHAR(20) NOT NULL UNIQUE,
orderTime TIMESTAMP,
peopleCount INT,
orderDesc VARCHAR(500),
payType INT,
orderStatus INT,
productId VARCHAR(32),
memberId VARCHAR(32),
FOREIGN KEY(productId) REFERENCES product(id),
FOREIGN KEY(memberId) REFERENCES member(id)
);
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('0E7231DC797C486290E8713CA3C6ECCC', '12345', NULL, 2, '没什么', 0, 1, '676C5BD1D35E429A8C2E114939C5685A', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('5DC6A48DD4E94592AE904930EA866AFA', '54321', NULL, 2, '没什么', 0, 1, '676C5BD1D35E429A8C2E114939C5685A', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('2FF351C4AC744E2092DCF08CFD314420', '67890', NULL, 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('A0657832D93E4B10AE88A2D4B70B1A28', '98765', NULL, 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('E4DD4C45EED84870ABA83574A801083E', '11111', NULL, 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('96CC8BD43C734CC2ACBFF09501B4DD5D', '22222', NULL, 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('55F9AF582D5A4DB28FB4EC3199385762', '33333', NULL, 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('CA005CF1BE3C4EF68F88ABC7DF30E976', '44444', NULL, 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('3081770BC3984EF092D9E99760FDABDE', '55555', NULL, 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB');
旅客表( traveller)
traveller表信息描述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ikpdABTf-1690118686236)(assets/image-20201014064251188.png)]
traveller表sql
CREATE TABLE traveller(
id VARCHAR(32) PRIMARY KEY,
NAME VARCHAR(20),
sex VARCHAR(20),
phoneNum VARCHAR(20),
credentialsType INT,
credentialsNum VARCHAR(50),
travellerType INT
);
INSERT INTO TRAVELLER (id, NAME, sex, phonenum, credentialstype, credentialsnum, travellertype)
VALUES ('3FE27DF2A4E44A6DBC5D0FE4651D3D3E', '张龙', '男', '13333333333', 0, '123456789009876543', 0);
INSERT INTO TRAVELLER (id, NAME, sex, phonenum, credentialstype, credentialsnum, travellertype)
VALUES ('EE7A71FB6945483FBF91543DBE851960', '张小龙', '男', '15555555555', 0, '987654321123456789', 1);
中间表 (order_traveller)
旅客与订单之间是多对多关系,所以我们需要一张中间表(order_traveller)来描述。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qeywgnpD-1690118686237)(assets/image-20201014064431518.png)]
order_traveller表sql
CREATE TABLE order_traveller(
orderId VARCHAR(32),
travellerId VARCHAR(32),
PRIMARY KEY (orderId,travellerId),
FOREIGN KEY (orderId) REFERENCES orders(id),
FOREIGN KEY (travellerId) REFERENCES traveller(id)
);
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('0E7231DC797C486290E8713CA3C6ECCC', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E');
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('2FF351C4AC744E2092DCF08CFD314420', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E');
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('3081770BC3984EF092D9E99760FDABDE', 'EE7A71FB6945483FBF91543DBE851960');
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('55F9AF582D5A4DB28FB4EC3199385762', 'EE7A71FB6945483FBF91543DBE851960');
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('5DC6A48DD4E94592AE904930EA866AFA', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E');
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('96CC8BD43C734CC2ACBFF09501B4DD5D', 'EE7A71FB6945483FBF91543DBE851960');
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('A0657832D93E4B10AE88A2D4B70B1A28', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E');
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('CA005CF1BE3C4EF68F88ABC7DF30E976', 'EE7A71FB6945483FBF91543DBE851960');
INSERT INTO ORDER_TRAVELLER (orderid, travellerid)
VALUES ('E4DD4C45EED84870ABA83574A801083E', 'EE7A71FB6945483FBF91543DBE851960');
实体类
Product类
package cn.yanqi.ssm.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@Data
public class Product {
private String id;
private String productNum;
private String productName;
private String cityName;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private Date departureTime;
private String departureTimeStr;
private Integer productPrice;
private String productDesc;
private Integer productStatus;
private String productStatusStr;
public String getDepartureTimeStr() {
// 对日期格式化
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (null != departureTime) {
departureTimeStr = dateFormat.format(departureTime);
}
return departureTimeStr;
}
public String getProductStatusStr() {
if (productStatus == null) {
return "";
}
if (productStatus == 0){
productStatusStr = "关闭";
} else if(productStatus == 1){
productStatusStr = "开启";
}
return productStatusStr;
}
}
Member类
package cn.yanqi.ssm.pojo;
import lombok.Data;
@Data
public class Member {
private String id;
private String name;
private String nickname;
private String phoneNum;
private String email;
}
Orders类
package cn.yanqi.ssm.pojo;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Data
public class Orders {
private String id;
private String orderNum;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
private Date orderTime;
private String orderTimeStr;
private int orderStatus;
private String orderStatusStr;
private int peopleCount;
private Product product;
private List<Traveller> travellers;
private Member member;
private Integer payType;
private String payTypeStr;
private String orderDesc;
//用于添加订单时,添加产品,会员
private Integer productId;
private Integer memberId;
public String getOrderTimeStr() {
// 对日期格式化
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
orderTimeStr = dateFormat.format(orderTime);
return orderTimeStr;
}
public String getPayTypeStr() {
if (payType == 0){
payTypeStr = "支付宝";
} else if(payType == 1){
payTypeStr = "微信";
}else if(payType == 2){
payTypeStr = "其他";
}
return payTypeStr;
}
public String getOrderStatusStr() {
if (orderStatus == 0){
orderStatusStr = "未支付";
} else if(orderStatus == 1){
orderStatusStr = "已支付";
}
return orderStatusStr;
}
}
Traveller类
package cn.yanqi.ssm.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
public class Traveller {
private String id;
private String name;
private String sex;
private String phoneNum;
private Integer credentialsType;
private String credentialsTypeStr;
private String credentialsNum;
private Integer travellerType;
private String travellerTypeStr;
public String getCredentialsTypeStr() {
// 证件类型 0:身份证 1:护照 2:军官证
if (credentialsType == 0) {
credentialsTypeStr = "身份证";
} else if (credentialsType == 1) {
credentialsTypeStr = "护照";
} else if (credentialsType == 2) {
credentialsTypeStr = "军官证";
}
return credentialsTypeStr;
}
public String getTravellerTypeStr() {
// 证件类型 0:成人 1:儿童
if (travellerType == 0) {
travellerTypeStr = "成人";
} else if (travellerType == 1) {
travellerTypeStr = "儿童";
}
return travellerTypeStr;
}
}
产品操作
查询所有产品信息
Product实体类
package cn.yanqi.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Product {
private String id;
private String productNum;
private String productName;
private String cityName;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private Date departureTime;
private String departureTimeStr;
private Integer productPrice;
private String productDesc;
private Integer productStatus;
private String productStatusStr;
public void setDepartureTimeStr(String departureTimeStr) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = null;
try {
date = dateFormat.parse(departureTimeStr);
} catch (ParseException e) {
e.printStackTrace();
}
this.departureTime = date;
}
public String getDepartureTimeStr() {
// 对日期格式化
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (null != departureTime) {
departureTimeStr = dateFormat.format(departureTime);
}
return departureTimeStr;
}
public void setProductStatusStr(String productStatusStr) {
if (productStatusStr.equals("关闭")){
this.productStatus = 0;
} else if(productStatusStr.equals("开启")){
this.productStatus = 1;
}
}
public String getProductStatusStr() {
if (productStatus == null) {
return "";
}
if (productStatus == 0){
productStatusStr = "关闭";
} else if(productStatus == 1){
productStatusStr = "开启";
}
return productStatusStr;
}
}
编写ProductController
/**
* @Author: yanqi
* @Date: 12:00
* @Desc:
*/
@Controller
@RequestMapping("product")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("findAll")
public String findAll(@RequestParam(value = "page", defaultValue = "1") Integer age ,
@RequestParam(value = "size",defaultValue = "5") Integer size,
Model model){
PageHelper.startPage(age,size);
List<Product> list = this.productService.findAll();
PageInfo pageInfo = new PageInfo(list);
model.addAttribute("pageInfo",pageInfo);
return "product-list";
}
}
编写ProductService
public interface ProductService {
/**
* 查询所有产品
* @return
*/
List<Product> findAll(Integer page, Integer size);
}
@Service("productService")
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper productMapper;
/**
* 查询所有产品
* @return
*/
@Override
public List<Product> findAll(Integer page, Integer size) {
PageHelper.startPage(page,size);
return this.productMapper.findAll();
}
编写ProductMapper
public interface ProductMapper {
/**
* 查询所有产品
* @return
*/
List<Product> findAll();
}
编写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="cn.yanqi.ssm.mapper.ProductMapper">
<!--查询所有产品-->
<select id="findAll" resultType="Product">
select * from product order by id desc
</select>
</mapper>
注意事项
在产品添加时,出发时间
是一个日期格式,我们需要对日期进行格式化。日期格式化有两种方式:
- 使用
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
; - 使用
Converter
接口来实现日期格式化。
在这里我们使用@DateTimeFormat来进行日期格式化。我们需要在Product类中的departureTime加上该注解
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date departureTime;
添加产品
编写ProductController
/**
* 新增产品
* @param product
* @return
*/
@PostMapping("save")
public String save(Product product){
this.productService.save(product);
return "redirect:findAll";
}
编写ProductService
/**
* 添加产品
* @param product
*/
void save(Product product);
/**
* 添加产品
* @param product
*/
@Override
public void save(Product product) {
this.productMapper.save(product);
}
编写ProductMapper
/**
* 添加产品
* @param product
*/
void save(Product product);
编写ProductMapper.xml
<!--添加产品-->
<insert id="save">
insert into product(productNum,
productName,
cityName,
departureTime,
productPrice,
productDesc,
productStatus)
values(#{productNum},
#{productName},
#{cityName},
#{departureTime},
#{productPrice},
#{productDesc},
#{productStatus})
</insert>
产品修改
编写ProductController
/**
* 修改产品
* @return
*/
@PostMapping("edit")
public String edit(
@RequestParam("id") String id,
@RequestParam("productNum") String productNum,
@RequestParam("productName") String productName,
@RequestParam("departureTime") String departureTime,
@RequestParam("cityName") String cityName,
@RequestParam("productPrice") Integer productPrice,
@RequestParam("productStatus") String productStatus,
@RequestParam("productDesc") String productDesc){
Product product = new Product();
product.setId(id);
product.setProductNum(productNum);
product.setProductName(productName);
product.setDepartureTimeStr(departureTime);
product.setCityName(cityName);
product.setProductPrice(productPrice);
product.setProductStatusStr(productStatus);
product.setProductDesc(productDesc);
this.productService.edit(product);
return "redirect:findAll";
}
/**
* 修改之前进行查询
* @param id
* @param model
* @return
*/
@GetMapping("editById")
public String editById(@RequestParam("id") Integer id , Model model){
Product product = this.productService.editById(id);
model.addAttribute("product",product);
return "product-edit";
}
编写ProductService
/**
* 修改产品之前进行查询用于回显
* @param pid
* @return
*/
Product findById(Integer pid);
/**
* 修改产品
* @param product
* @return
*/
void edit(Product product);
/**
* 修改产品之前进行查询用于回显
* @param pid
* @return
*/
@Override
public Product findById(Integer pid) {
return this.productMapper.findById(pid);
}
/**
* 修改产品
* @param product
* @return
*/
@Override
public void edit(Product product) {
this.productMapper.edit(product);
}
编写ProductMapper
/**
* 修改产品之前进行查询用于回显
* @param pid
* @return
*/
Product findById(Integer pid);
/**
* 修改产品
* @param product
* @return
*/
void edit(Product product);
编写ProductMapper.xml
<!--修改产品之前进行查询-->
<select id="findById" resultType="Product">
select * from product where id=#{pid}
</select>
<!--修改产品-->
<update id="edit">
update product set
productNum=#{productNum},
productName=#{productName},
cityName=#{cityName},
departureTime=#{departureTime},
productPrice=#{productPrice},
productDesc=#{productDesc},
productStatus=#{productStatus}
where id=#{id}
</update>
产品详情
编写ProductController
/**
* 产品详情
* @param id
* @param model
* @return
*/
@GetMapping("findById")
public String findById(@RequestParam("id") Integer id, Model model){
Product product = this.productService.editById(id);
model.addAttribute("product",product);
return "product-show";
}
编写ProductService
/**
* 修改产品之前进行查询用于回显,之前修改写过无需再写
* @param pid
* @return
*/
Product findById(Integer pid);
/**
* 修改产品之前进行查询用于回显
* @param pid
* @return
*/
@Override
public Product findById(Integer pid) {
return this.productMapper.findById(pid);
}
编写ProductMapper
/**
* 修改产品之前进行查询用于回显
* @param pid
* @return
*/
Product findById(Integer pid);
编写ProductMapper.xml
<!--修改产品之前进行查询-->
<select id="findById" resultType="Product">
select * from product where id=#{pid}
</select>
产品删除
编写ProductController
/**
* 指定删除
* @param id
* @return
* TODO 待优化:订单中有产品的相关信息,产品删除了,订单中依然有产品的信息
*/
@GetMapping("deleteById")
public String deleteById(@RequestParam("id") Integer id){
this.productService.deleteById(id);
return "redirect:findAll";
}
编写ProductService
/**
* 删除产品
* @param pid
* @return
*/
void deleteById(Integer pid);
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper productMapper;
@Autowired
private OrderMapper orderMapper;
/**
* 删除产品
* @param pid
* @return
*/
@Override
public void deleteById(Integer pid) {
//删除订单中商品外键
this.orderMapper.updateById(pid);
//删除商品本身
this.productMapper.deleteById(pid);
}
}
编写ProductMapper
/**
* 删除订单中的商品
*/
void updateById(Integer pid);
/**
* 删除产品
* @param pid
* @return
*/
void deleteById(Integer pid);
编写ProductMapper.xml
<!--删除产品-->
<delete id="deleteById">
delete from product where id=#{pid}
</delete>
<?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="cn.yanqi.ssm.mapper.OrderMapper">
<!--删除订单中商品-->
<update id="updateById">
update orders set productId = null where productId = #{id}
</update>
</mapper>
批量删除
/**
* 批量删除
* @param ids
* @return
*/
@PostMapping("selectDelete")
public String selectDelete(@RequestParam("ids") int[] ids){
for (int id : ids) {
this.productService.deleteById(id);
}
return "redirect:findAll";
}
tMapper;
@Autowired
private OrderMapper orderMapper;
/**
* 删除产品
* @param pid
* @return
*/
@Override
public void deleteById(Integer pid) {
//删除订单中商品外键
this.orderMapper.updateById(pid);
//删除商品本身
this.productMapper.deleteById(pid);
}
}
### 编写ProductMapper
```java
/**
* 删除订单中的商品
*/
void updateById(Integer pid);
/**
* 删除产品
* @param pid
* @return
*/
void deleteById(Integer pid);
编写ProductMapper.xml
<!--删除产品-->
<delete id="deleteById">
delete from product where id=#{pid}
</delete>
<?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="cn.yanqi.ssm.mapper.OrderMapper">
<!--删除订单中商品-->
<update id="updateById">
update orders set productId = null where productId = #{id}
</update>
</mapper>
批量删除
/**
* 批量删除
* @param ids
* @return
*/
@PostMapping("selectDelete")
public String selectDelete(@RequestParam("ids") int[] ids){
for (int id : ids) {
this.productService.deleteById(id);
}
return "redirect:findAll";
}