<!--springBoot整合JSP添加依赖 -->
<!--servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--jstl依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--使jsp页面生效 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
server:
port: 8090
servlet:
context-path: / #项目根目录发布
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
mvc: #引入mvn配置
view:
prefix: /WEB-INF/ # /默认代表根目录 src/main/webapp
suffix: .jsp
# Spring整合Mybatis-plus配置
mybatis-plus:
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
# 打印Mybatissql语句
logging:
level:
com.jt.mapper: debug
需求: 用户通过http://localhost:8090/findAll 获取全部userList集合,并且在userList.jsp页面中进行表格数据展现
说明:IDEA默认条件下工作目录选择不正确的,需要手动配置一下,注意工作目录编辑
第三阶段: pojo–>Mapper----->Service------>Controller------->页面及JS 自下而上的开发方式
第四阶段 pojo—>Controller—>Service----->Mapper 自上而下的开发方式
package com.jt.controller;
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public String findAll(Model model){
List<User> userList = userService.findAll();
model.addAttribute("userList", userList);
return "userList";
}
}
package com.jt.service;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.selectList(null);
}
}
说明:
1).Ajax中间有ajax引擎的参与.ajax引擎实质就是一种代理的思想
2).由于需要实现异步的操作,所以请求必然是多次请求.多次响应.
如果需要发起Ajax请求时,一般需要发起2个请求.
1个是用来跳转页面的 http://localhost:8090/toAjax
1个是用来请求数据的 http://localhost:8090/findAjax
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>您好Springboot</title>
<!-- 引入函数类库 -->
<script src="../js/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
//1.让页面全部加载完成 函数式编程
$(function(){
//1.$.get(url地址,传递的参数,回调函数,返回值类型)
//$.post() $.getJSON $.ajax
$.ajax({
url: "/findAjax", //url地址
method: "get", //请求类型
data: {
id:1,name:"tomcat"}, //请求参数
success: function(data){
for(let user of data){
//console.log(user);
let id = user.id;
let name = user.name;
let age = user.age;
let sex = user.sex;
let tr = ""+id+" "+name+" "+age+" "+sex+" "
$("#table1").append(tr);
}
},
error: function(data){
alert("请求失败");
},
cache: true, //默认条件下缓存是开启的 false
async: true //默认就是异步
})
//关于参数写法 2种
//1.JSON格式{id:1,name:"tomcat"}
//2.字符串拼接 id=1&name="tomcat"
$.get("/findAjax3333333333333",{
id:1},function(data){
//循环遍历方式3 of
for(let user of data){
//console.log(user);
let id = user.id;
let name = user.name;
let age = user.age;
let sex = user.sex;
let tr = ""+id+" "+name+" "+age+" "+sex+" "
$("#table1").append(tr);
}
//循环遍历方式2 in
/* for(let index in data){
let user = data[index];
console.log(user);
} */
//console.log(data);
//循环遍历方式1
/* for(let i=0;i
})
})
</script>
</head>
<body>
<table id="table1" border="1px" width="65%" align="center">
<tr>
<td colspan="6" align="center"><h3>学生信息</h3></td>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</table>
</body>
</html>
/**
* 跳转到ajax.jsp页面中
*/
@RequestMapping("toAjax")
public String toAjax(){
return "ajax";
}
/**
* 接收ajax请求: /findAjax
* 返回值: List
*/
@RequestMapping("/findAjax")
@ResponseBody //1.将返回值结果转化为JSON数据返回 2.代表ajax请求结束
public List<User> findAjax(){
return userService.findAll();
}
说明: 单体项目架构将所有的功能模块都写到一起,如果其中出现了问题则直接影响整个程序的允许.
1).可以按照功能模块可以将项目拆分为若干个子项目
2).如果业务功能足够复杂,则需要更加细粒度的拆分方式.
拆分的意义: 使用分布式架构设计,可以有效的降低架构中的耦合性,提高程序的开发速度及运维速度.
问题1: 分布式架构中如何保证jar包文件的统一?
问题2: 分布式架构中如何保证工具API的统一?
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jt</groupId>
<artifactId>jt</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包类型为POM 只有pom才能被其他项目继承-->
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<!--指定JDK版本-->
<java.version>1.8</java.version>
<!--跳过测试类打包-->
<skipTests>true</skipTests>
</properties>
<!--依赖的作用:依赖需要的jar包文件 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!--spring-boot-starter-xxx springboot启动项
开箱即用:
只需要引入特定的jar包简单的配置,即可以使用该功能
-->
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--支持热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--引入插件lombok 自动的set/get/构造方法插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--添加数据库驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--springBoot整合jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--SpringBoot整合MybatisPlus mybatis和plus jar包冲突的-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--springBoot整合JSP添加依赖 -->
<!--servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--jstl依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--使jsp页面生效 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<!--不要添加build标签 -->
</project>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jt</artifactId>
<groupId>com.jt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jt-manage</artifactId>
<!--动态web资源 暂时使用war包-->
<packaging>war</packaging>
<!--添加jar包文件依赖-->
<dependencies>
<dependency>
<groupId>com.jt</groupId>
<artifactId>jt-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<!--所有业务系统,必须添加build标签-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
说明:将课前资料中的jt-manage项目的src文件导入. 导入结果,如图所示