day01_springboot综合案例(一)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: day01_springboot综合案例

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', 'zs@163.com');



day01_springboot综合案例(二)https://developer.aliyun.com/article/1433640

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
25天前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
22 0
|
2月前
day01_springboot综合案例(三)
day01_springboot综合案例
12 0
|
2月前
|
SQL
day01_springboot综合案例(二)
day01_springboot综合案例
47 0
|
2月前
day02_springboot综合案例(三)
day02_springboot综合案例
25 0
|
2月前
day02_springboot综合案例(二)
day02_springboot综合案例
31 0
|
2月前
day02_springboot综合案例(一)
day02_springboot综合案例
41 0
|
3月前
|
Dubbo Java 应用服务中间件
Spring Boot + Dubbo + Zookpeer分布式案例
Spring Boot + Dubbo + Zookpeer分布式案例
35 0
|
3月前
|
前端开发 Java 数据处理
基于SpringBoot的SSMP整合案例
基于SpringBoot的SSMP整合案例
27 0
|
4月前
|
Java Maven
Springboot-starter的自动配置原理-及案例实现7
Springboot-starter的自动配置原理-及案例实现7
|
4月前
|
Java Spring 容器
Springboot-starter的自动配置原理-及案例实现5
Springboot-starter的自动配置原理-及案例实现5