1、创建spring boot项目,添加web、mybatis、mysql依赖
2、这些依赖不够,其他的手动添加(若有其他需求可追加相关依赖)
添加德鲁伊连接池、jstl、jasper、tomcat依赖
完整的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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 打包 jar war--> <packaging>war</packaging> <groupId>com.bingchuan</groupId> <artifactId>czg</artifactId> <version>0.0.1-SNAPSHOT</version> <name>czg</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-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 这里使用druid连接池,好处就是可以监控数据库访问性能,--> <!-- druid内置提供了一个功能强大的StatFilter插件,--> <!-- 能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。--> <!--实现对 Druid 连接池的自动化配置--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency> <!--jsp页面使用jstl标签 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--用于编译jsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- 添加Tomcat部署依赖包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> </resources> </build> </project>
3、配置文件application-default.yml
#http://localhost:8080/czg/ #http://localhost:8080/czg/firstserver: servlet: context-path: /czg port: 8080spring: #mvc # spring.mvc.view.prefix=/WEB-INF/jsp/ # spring.mvc.view.suffix=.jsp mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp datasource: type: com.alibaba.druid.pool.DruidDataSource # 设置类型为 DruidDataSource driverClassname: com.mysql.cj.jdbc.Driver # Loading class `com.mysql.jdbc.Driver'. # This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. # The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. name: dataSource
# 柴公司内网
url: "jdbc:mysql:// IP地址:端口/数据库库名?useSSL=false&&characterEncoding=utf8" username: 用户名password: 密码 # 德鲁伊连接池配置 druid: min-idle: 5 max-active: 100 initial-size: 5 # 获取连接时最大等待时间,单位毫秒 max-wait: 6000 filter: stat: # 开启慢查询记录 log-slow-sql: true # 慢sql得时间设置 单位:毫秒 slow-sql-millis: 5000 # sql合并配置 merge-sql: true wall: config: merge-allow: true stat-view-servlet: # 是否开启stat-view-servlet enabled: true login-username: bingchuan login-password: tianshi init-global-variants: true web-stat-filter: exclusions: '*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*,/download/*,/wj/*,/assets/*' # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,slf4j # 合并多个DruidDataSource的监控数据 use-global-data-source-stat: true# GMT(Greenwich Mean Time):格林威治标准时间# UTC:世界标准时间# CST(China Standard Time):中国标准时间# GMT + 8 = UTC + 8 = CSTmybatis: # 用于指定Mybatis的配置文件位置。如果指定了该属性, # 那么会以该配置文件的内容作为配置信息构建对应的SqlSessionFactoryBuilder, # 但是后续属性指定的内容会覆盖该配置文件里面指定的对应内容 # config-location: classpath:mybatis-config.xml # 它表示我们的Mapper文件存放的位置,当我们的Mapper文件跟对应的Mapper接口处于同一位置的时候可以不用指定该属性的值 mapper-locations: classpath:mapper/*.xml configuration: # 全局启用或禁用在此配置下在任何映射器中配置的任何缓存 cache-enabled: true # 它一般对应我们的实体类所在的包,这个时候会自动取对应包中不包括包名的简单类名作为包括包名的别名。 # 多个package之间可以用逗号或者分号等来进行分隔。(value的值一定要是包的全名) # <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model" /> type-aliases-package: com.bingchuann.springmvc.pojo# 启用从经典数据库列名A\u列到camel case经典Java属性名A列的自动映射-# map-underscore-to-camel-case: true# 允许JDBC支持生成的密钥。需要兼容的驱动程序。如果设置为true,# 则强制使用生成的键,因为有些驱动程序否认兼容性,但仍然有效# use-generated-keys: true# 设置驱动程序等待数据库响应的秒数# default-statement-timeout: 30000
开发的代码就不细写了
4、创建数据库表、根据表结构创建对应实体类
package com.bingchuan.czg.pojo;public class CzgRdsProcess { private String dbname; private long id; private String user; private String ip; private String db; private String command; private long time; private String state; private String info; public String getDbname() { return dbname; } public void setDbname(String dbname) { this.dbname = dbname; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getDb() { return db; } public void setDb(String db) { this.db = db; } public String getCommand() { return command; } public void setCommand(String command) { this.command = command; } public long getTime() { return time; } public void setTime(long time) { this.time = time; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; }}
5、开发持久层功能
package com.bingchuan.czg.dao;import com.bingchuan.czg.pojo.CzgRdsProcess;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Component;import java.util.List;/** * @author zhangbingchuan 270083413@qq.com * @date 2021/2/22 17:06 */@Mapper@Componentpublic interface ICzgRdsProcess { List<CzgRdsProcess> finAll();}
6、开发业务层功能
package com.bingchuan.czg.service;import com.bingchuan.czg.dao.ICzgRdsProcess;import com.bingchuan.czg.pojo.CzgRdsProcess;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;/** * @author zhangbingchuan 270083413@qq.com * @date 2021/2/22 17:18 */@Servicepublic class CzgRdsProcessService { private ICzgRdsProcess czgRdsProcess; @Autowired public CzgRdsProcessService(ICzgRdsProcess czgRdsProcess) { this.czgRdsProcess = czgRdsProcess; } public List<CzgRdsProcess> finAll(){ return czgRdsProcess.finAll(); }}
7、开发控制层功能
构造器注入、web控制器用@controller注解、重定向试图写文件名(“czg.jsp”写“czg”)
/** * @author zhangbingchuan 270083413@qq.com * @date 2021/2/22 17:22 */@Controller public class CzgWebController { private CzgRdsProcessService czgRdsProcessService ; @Autowired public CzgWebController(CzgRdsProcessService czgRdsProcessService) { this.czgRdsProcessService = czgRdsProcessService; } @RequestMapping("/first") public String hello(Model model){ List<CzgRdsProcess> list= czgRdsProcessService.finAll(); model.addAttribute("list",list); //视图重定向 czg.jsp System.out.println("czg..."); return "czg"; }}
8、jsp目录及页面
目录创建位置;jsp页面配置
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html><head> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>hello czg</title></head><script> // 定时器 单位毫秒 setInterval(()=>{ window.location.reload()},5000)// $("#open").click(function () {// window.location.reload();// })</script><body> 亲爱的柴: <button type="button" value="open" id="open"> <table border="1" align="center" width="50%"> <tr> <th>dbname</th> <th>id</th> <th>user</th> <th>db</th> <th>command</th> <th>time</th> <th>state</th> <th>info</th> </tr> <jsp:useBean id="list" scope="request" type="java.util.List"/> <c:forEach items="${list}" var="czgRdsProcess"> <tr> <td>${czgRdsProcess.dbname }</td> <td>${czgRdsProcess.id }</td> <td>${czgRdsProcess.user }</td> <td>${czgRdsProcess.db }</td> <td>${czgRdsProcess.command }</td> <td>${czgRdsProcess.time }</td> <td>${czgRdsProcess.state }</td> <td>${czgRdsProcess.info }</td> </tr> </c:forEach> </table></body></html>
8、运行项目
idea运行项目
9、调用: 通过url调用接口看结果;协议://ip:端口/项目名/接口名
http://locathost:8080/czg/first
10、打包部署: 打成war包部署,web项目打war包不要打jar包。
11、遇到的问题
·springboot不支持jsp需要添加依赖才行
·没有编译META-INT目录,访问不到jsp页面
12、加油,做最好的自己