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