二、知识点
A、JavaWeb 概述
1. 轻量级JavaWeb开发组合框架有哪些?
2. 在Maven环境中配置本地仓库,则应在maven安装路径下conf文件夹中的Settings.xml
文件中添加哪些标签来设置本地仓库和中央仓库。
3. IntelliJ IDEA 环境中创建的Maven项目的流程,特别是原型模板选择
Java 项目:org.apache.maven.archetypes:maven-archetype-quickstart
JavaWeb 项目:org.apache.maven.archetypes:maven-archetype-webapp
多模块项目父模块:org.apache.maven.archetypes:maven-archetype-site-simple
4. 以上三类原型模板的项目分别默认的打包方式(jar,war,pom)。
5. JSP页面代码是那两种代码组成
6. 常用lombok注解及其功能(Set方法,Get方法,无参构造方法和全参构造方法)
B、Spring 部分
1. Spring 基本用法需要添加的基础依赖
spring-core
spring-beans
spring-context
spring-expression
spring-aop
2. Spring 基本用法(参照课堂实验3)
(1)依赖注入:ApplicationContext.xml 实现依赖注入
设值注入(类中要有Setter方法)
构造注入(类中要有带参数的构造方法)
例如:设值依赖注入的格式:
<bean id="exampleBean" class="examples.ExampleBean">
<property name="beanTwo"ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>
例如:构造依赖注入的格式:
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>
(2)控制反转:在主类测试类中,创建控制反转容器,获取bean实例,实现bean实例
具有的功能。(重点理解标红的代码)
例如:
public static void main(String[] args) {
ApplicationContext act= new ClassPathXmlApplicationContext("applicationContext.xml");
OrderNotify orderNotify =act.getBean("orderNotify", OrderNotify.class);
orderNotify.PaySuccess();
}
3. AOP
(1)AOP基本概念(切面 连接点 增强处理 代理对象)
切面:封装横切到系统功能的类,类中可以定义切入点和通知
连接点:程序执行过程中明确的点,一般是方法的调用
切入点:需要处理的连接点,程序中一般体现为书写切入点的表达式
增强处理:由切面添加到特定的连接点的一段代码,在特定的切入点上执行的增强处理
代理对象:增强后的目标对象
(2)基于代理类的AOP实现(重点代理Bean元素的声明)
例如:(重点理解标红的代码)
<!-- 配置目标类bean-->
<bean id="myTestDao" class=" spring.demo.dao.impl.TestDaoImpl"/>
<!-- 配置切面类bean-->
<bean id="myAspect" class="spring.demo.interceptor.MyAspect" />
<!-- 使用Spring 代理工厂定义一个名为testDaoProxy的代理-->
<bean id="testDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 指定代理实现的接口-->
<property name="proxyInterfaces" value="spring.demo.dao.TestDao " />
<!-- 指定目标对象-->
<property name="target" ref=" myTestDao" />
<!-- 指定切面,织入环绕通知-->
<property name="interceptorNames" value="myAspect "/>
<!-- 指定代理方式,true指定CGLIB动态代理,false指定JDK动态代理-->
<property name="proxyTargetClass" value="true"/>
</bean>
C、SpringBoot 部分
1.
Spring boot 提供的实现对restful 接口的支持的常用注解及其功能:
@Controller、@RequestBody、@ResponseBody、@RestController、@RequestMapping
@Controller: 用于标记一个类是 Spring MVC 的控制器(Controller)。它主要用于处理 HTTP 请求,将请求映射到对应的方法上。
@RequestBody: 用于将 HTTP 请求的正文(body)内容绑定到方法的参数上。通常用于处理 POST、PUT 等请求,这些请求可能会携带数据(如 JSON 格式或 XML 格式的数据)。
示例:@Controller
public class MyController {
@RequestMapping(value = "/user", method = RequestMethod.POST)
public void addUser(@RequestBody User user) {
//将请求体中的数据转换为User对象,然后可以进行保存用户等操作
userService.addUser(user);
}
}
@ResponseBody:将方法的返回值直接写入 HTTP 响应的正文(body)中,而不是将返回值解析为视图名称。
@RestController:当前类加入到Spring上下文中,并且该类为响应体,结合了@Controller和@ResponseBody
@RequestMapping:提供给客户端响应的action路径
2. 接口数据校验中定义实体(Bean)类 ,配置校验规则使用的常用注解(重点是属性是否为空,值的大小取值范围和日期数据范围)
@NotBlank(message = “某某不能为空”)
@Min(0)
@Max(999)
@Range(max = ,min=,message=)
@Past
@Future
3. 文件上传(重点单文件上传)
前端页面表单中enctype属性值
接口控制层:实现文件上传方法的参数类型
如何获取上传文件的原始文件名
如何将上传的文件保存到服务器
举例:
(1)前端页面表单中的代码:(重点理解标红的代码)
<form action="fileUpload" method="post" enctype="multipart/form-data">
<p>选择文件:<inputtype="file"name="fileName"/></p>
<p><input type="submit" value="提交"/></p>
</form>
(2)接口控制层的方法代码:(重点理解标红的代码)
@RequestMapping("fileUpload")
@ResponseBody
public String fileUpload(@RequestParam("fileName") MultipartFile file){
String fileName = file.getOriginalFilename();
String path = "F:/test" ;
File dest = new File(path + "/" + fileName);
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
}
try {
file.transferTo(dest); //保存文件
return "true";
}
catch (IllegalStateException e) { e.printStackTrace();
catch (IOException e) { e.printStackTrace();
}
return "false";
return "false";
4. 定时任务设置使用的Scheduled注解中的cron表达式表示规则
每小时第40分钟开始,每五秒执行一次
D、SpringBoot+MyBatista 框架整合部分(参照最后课堂实验8+最后大作业)
使用框架整合技术实现完整的JavaWeb项目 ,整合项目涉及的文件编辑:
1. Entiy 中的实体类: 属性名与数据库中的数据表名和字段名对应
2. Mapper层:XXXmapper.xml 及其接口
常见的select方法有返回值List<entity>,方法中参数有(Entity entity)实体类作为查询条件
插入删除修改为没有返回值的,可以使用(Entity entity)作为参数,也可以用具体字段例如(String id)
例:
<delete id="deleteWaresById">
delete from Wares where id = #{id}
</delete>
删除操作直接传递id就可以
<insert id="insertWares" parameterType="Wares">
insert into Wares values (#{id},#{name},#{brand},#{category},#{price},#{stock},#{pic_address})
</insert>
当传递的参数不为基本参数时,要标明parameterType = “实体类名”
}
}
映射文件中常见的数据库操作创建(重点插入、查询和修改三类数据库操作功能的实现)
对应接口中方法(原型与映射文件的对应)
插入:
<insert id=”mapper接口中的对应方法” parameterType = “实体类名”>
insert into wares(对应数据库表名) value = {#{属性名},#{属性名},#{属性名},#{属性名}…}
</insert>
查询:
<select id = “mapper接口中对应的方法” parameterType = “实体类名” resultType = “实体类名”>
select * from wares(对应数据库表名)
<where>
<if test =”id != null and id != ‘’”>(这个判断是判断传递进来的属性非空)
id(这是数据库中的属性) = #{id}(这是传递进来的属性)
</if>
<if test = “name !=null and name !=’’”>
and name = #{name}
</if>
……
</where>
</select>
修改:
<update id = “mapper接口中对应的方法” parameterType = “实体类名”>
update wares(数据库表名)
<set>
<if test =”name!=null and name!=’’”>
,name = #{name}
</if>
<if test =”price != null and price != 0”>
,price = #{price}
</if>
……
</set>
where id = #{id};
</update>
3. Service 层:接口及其接口实现类
如何向映射层转发指令等
首先接口创建一堆抽象方法
实现类:
定义一个mapper类的对象mapper,用@Autowrite
实现抽象方法,一般是直接用mapper.(mapper中的对应方法)进行直接转发,有的需要进行加工,如:
@Override
public List<Wares> selectWares(String attr, String value) {
String id = null;
String name = null;
String brand= null;
String category =null;
Integer price = 0;
Integer stock = 0;
String pic_address = null;
if(attr.equals("id"))id = value;
else if(attr.equals("name"))name = value;
else if(attr.equals("brand"))brand = value;
else if(attr.equals("category"))category = value;
else if(attr.equals("price"))price = Integer.valueOf(value);
else if(attr.equals("stock"))stock = Integer.valueOf(value);
else if(attr.equals("pic_address"))pic_address = value;
Wares wares = new Wares(id,name,brand,category,price,stock,pic_address);
return waresmapper.selectWares(wares);
}
根据传递的属性名和属性值进行特定的查找
首先将所有的属性初始化为0或null
然后进行判断, if(attr.equals(“这里填实体类中的属性名”))
id(和属性名一致的属性) = value
else if(一样的操作)
一样的操作
……
Wares(具体的自己的实体类) wares = new Wares(参数,参数,参数,参数…)
return mapper.(调用mapper中对应的方法)(wares(这是传递的参数));
4. Controller 层:
(与前端页面请求的URL地址对应)
(与前端提交的参数对应)
向服务层发出指定:(调用服务层的方法)
方法的返回:
(返回给前端的页面)
(呈现给前端页面的数据)
@RequestMapping(“URL 地址”):实际页面的地址
方法的参数:(一般是(Model model),如果要传递参数,一般方法中的service对象调用方法需要用到实体类的对象,例如:(Model model,Entity(换成你自己的具体实体类) entity),然后service.方法(entity);有时需要传递attribute和value,例如(Model model, String attribute, String value),当执行查询工作时,会返回一个列表,这时,List<实体类> list = service.方法(attribute,value)。此时需要返回的页面往往有一个<tbody th:each="ware : ${list}">,需要我们用model.addAttribute(“list”,list),其中,第一个”list”需要与我们返回的页面的${list}的list名称一致,第二个list与我们这个方法中创建的list一致,以下是一个示例:
@RequestMapping("/showwares")//这里是访问的页面
public String findUser(Model model, String attribute, String value){(这里是传递的参数)
List<Wares>list=waresServiceImpl.selectWares(attribute,value); //service调用了方法,传递两个参数,返回值为list
model.addAttribute("list",list);//添加list到具体页面之中
model.addAttribute("backMessage",getAddress());
return "showwarespage";
}
5. 模板文件的编辑文件
(请求提交的URL地址@{URL})
例如:
<form th:action="@{/showwares}" method="post" >
其中,action后的@{/showwares}中,showwares为访问路径,在controller层中要和RequestMapper(“/showwares”)路径对应
(页面程序返回的模型属性${属性名}) 特别是返回批量数据的处理
返回的属性我们使用<tbody th:each=”自定义名称 : ${传递的参数}”>,例如在上面controller层中提到的<tbody th:each="ware : ${list}">,其中${list}要与我们controller层的model.addAttribute(“list”,list)中的第一个”list”名称一致,之后我们用
<tr>
<td th:text = “${自定义名称.属性}”></td>
<td th:text = “${自定义名称.属性}”></td>
<td th:text = “${自定义名称.属性}”></td>
……
</tr>
例如:
<tr>
<td th:text="${ware.id}"></td>
<td th:text="${ware.name}"></td>
<td th:text="${ware.brand}"></td>
<td th:text="${ware.category}"></td>
<td th:text="${ware.price}"></td>
<td th:text="${ware.stock}"></td>
<td><img th:src="${ware.pic_address}"/></td>
</tr>
6. application.properties 配置文件
数据源设置:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/自己的数据库名
spring.datasource.username=自己的用户名
spring.datasource.password=自己的密码
映射文件所在位置:
mybatis.mapper-locations=classpath:mapper/*.xml (替换为自己的位置)
实体类所在的包设置
mybatis.type-aliases-package=springboot.demo.entity (替换为自己的包位置)
因为是自己的作业,就放在这里了,不一定正确,可以用作参考GitCode - 全球开发者的开源社区,开源代码托管平台