12.13 @RequestMapping用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.runsstudio.springmvc.servlet;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.View;

@Controller

public class RequestMappingControllerTester {
/**
* 必须不包含username
* @return
*/
@RequestMapping(value = "/handle01" ,params = {"!username"},method = RequestMethod.GET)
public String handle01(){
System.out.println("RequestMappingControllerTester..handle01");
return "success";
}


/**
* 必须是火狐浏览器才能访问:把请求头复制过来
*/
@RequestMapping(value = "/onlyFirefox" ,headers = {"User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0"})
public String onlyFirefox(Model model){
System.out.println("firefox...");
model.addAttribute("name","firefox");
model.addAttribute("time",System.currentTimeMillis());
return "success";
}

/**
* 模糊匹配功能:ANT风格
* 模糊和精确都匹配的情况下 精确优先!
* ?匹配 单个字符 优先级 high
* *匹配 一层路径下的多个字符 优先级 mid
* ** 匹配 多层路径下的多个字符 比如/a** /aaa 这样的话访问/aa/bb/cc/aaa也可以访问的到 优先级最低
*/
@RequestMapping(value = "/AntStyleURL?")
public String AntStyleUrl01(Model model){
System.out.println("AntStyleUrl");
model.addAttribute("name","AntStyleURL");
return "success";
}
@RequestMapping(value = "/AntStyleURL*")
public String AntStyleUrl02(Model model){
System.out.println("AntStyleUrl2");
model.addAttribute("name","AntStyleURL2");
return "success";
}

/**
* 路径上可以有占位符
* 占位符只能占用一层路径
*/
@RequestMapping("/user/{id}")
public String pathVariableTest(@PathVariable("id")String id,Model model){
System.out.println("占位符的值:"+id);
model.addAttribute("name",id);
return "success";
}

}

12.12 SPRING MVC入门及一些坑

Spring 为展现层****提供的基于 MVC 设计理念的优秀的 Web 框架,是目前最主流 MVC 框架****之一

•Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架

Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请求的控制器,而无须实现任何接口。

支持 REST 风格****的 URL 请求

•采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性

IDEA建立Spring MVC Hello World 详细入门教程

https://www.cnblogs.com/wormday/p/8435617.html


Spring MVC的HELLOWORLD当中的一些坑

用IDEA创建HELLOWORLD 的时候

image-20191212174152755

要手动添加这两个包到lib下,不然怎么运行都开不起来 同时WEB-INF底下会有报错!


1
@RequestMapping

的作用:告诉 SPRINGMVC 这个方法 用什么方法来处理这个请求

这个/helloworld中的/可以省略 习惯了比较好

image-20191212192601259


如果不指定配置文件位置,默认会找一个/WEB-INF/XXX-servlet.xml

这里的xxx是前端控制器的名字

image-20191212192942937


URL-PATTERN

1
2
3
4
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

/拦截所有请求 不拦截jsp页面

/* 拦截所有请求 (相当于完全覆盖了所有的资源 所以我们写/就可以了)

写/* 的话访问jsp会出现这个效果:(控制台报错)

1
mapping found for HTTP request with URI [/index.jsp] in DispatcherServlet with name 'dispatcher'

image-20191212194607304


处理*.jsp是tomcat做的事 这是因为DefaultServlet是Tomcat中用来处理静态资源了

除了JSP 和servlet外剩下的都是静态资源

index.html:静态资源:tomcat会在服务器下找到这个资源并返回

/

重写了大web.xml中的资源

为什么jsp能访问?因为我们没有覆盖JspServlet的资源

  1. 服务器的web.xml中有一个DefaultServlet是url-pattern=/
  2. 我们配置中前端控制器url-pattern=/
  3. 项目中小web.xml都是继承于服务器中大web.xml
  4. 小web.xml 相当于子类 重写了父类的方法

一个方法只能处理一个请求


RequestMapping的属性:

  • method:限定请求方式 RequestMethod.GET/ POST/…….

  • params

    1
    2
    3
    4
    5
    @RequestMapping(value = "/handle01" ,params = {"username"},method = RequestMethod.GET)
    public String handle01(){
    System.out.println("RequestMappingControllerTester..handle01");
    return "success";
    }

    url中必须有username,比如

    http://localhost:8080/haha/handle01?username=111

    才可以

    @RequestMapping(value = “/handle01” ,params = {“!username”},method = RequestMethod.GET)

    url中必须不包含username,比如

    http://localhost:8080/haha/handle01

    才可以,不然会报错

    1
    handleNoSuchRequestHandlingMethod No matching handler method found for servlet request: path '/haha/handle01', method 'GET', parameters map['username' -> array<String>['111']]
  • headers

  • consumes

  • produces

12.11 声明事务

Spring提供JdbcTemplate能快捷操作数据库

  1. ```
    使用JDBC 回顾一下:
    1. getDataSource
    2. getConnection
    3. getStatement 或preparedStatement
    4. statement.setInt
    5. stament.executeQuery()
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15



      然后配置一下
      然后执行想执行的语句

      ```xml
      <!--1. 配置事务管理器让其进行事务控制:一定要导入面向切面编程的几个包,基本版aspect+增强版三个-->
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!-- 控制住数据源-->
      <property name="dataSource" ref="dataSource"/>
      </bean>
      <!--2. 开启基于注解的事务控制模式:依赖tx名称空间-->
      <tx:annotation-driven transaction-manager="transactionManager"/>
      <!-- 3. 给事务方法加注解 -->
1
2
@Autowired
JdbcTemplate jdbcTemplate;
  1. 引入外部配置文件

  2. 扫描包

  3. 配置数据源

  4. 配置JDBCTEMPLATE,给JDBCTEMPLATE一个id,配置dataSource

  5. 配置声明式事务(上面的三步代码)

  6. @Transactional 
    是注解在要事务的方法上,不是执行在test上!
        @Transactional
        public void checkout(String username,String isbn) throws SQLException {
            bookDao.updateStock(isbn);
            int price = bookDao.getPrice(isbn);
            bookDao.updateBalance(username,price);
            int i=10/0;
        }//正确
    
    
        @Test
        @Transactional //错误 不应该注解在这
        public void test04() throws SQLException {
            BookService bookService=ioc.getBean("bookService",BookService.class);
    //        BookService bookService=new BookService();
    
            bookService.checkout("Tom","ISBN-001");
            System.out.println("结账完成");
        }
    

12.11 基于配置的AOP步骤

(基于注解的AOP)

  1. 将目标类和切面类都加入到ioc容器中,搞定@Component
  2. 告诉Spring哪个是切面类 搞定@Aspect
  3. 在切面类中使用五个通知注解来配置切面中的通知方法何时何地运行
  4. 开启AOP功能

基于配置的也是同样的步骤 不过是在xml里配置

注解的优点:快速方便

xml配置优点:功能完善 (重要的东西用配置,不重要的可以用注解)

12/12 事务细节

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
事务细节:
isolation
propagation
如果有多个事务嵌套运行,子事务是否要和大事务共用一个事务

noRollbackFor Class[] -- 哪些异常事务可以不会滚
noRollbackForClassName
rollbackFor Class[] -- 哪些异常事务需要回滚
rollbackForClassName

异常分类:
运行时异常(非检查异常,比如除0):可以不处理:默认都回滚
编译时异常(比如file找不到):要么try-catch:默认不回滚

readOnly -boolean =true:设置为只读事务 加快查询速度,不用管事务的操作 不能用于增删改
timeout -int 三秒内执行不完就回滚,并且报TransactionTimedOutException


img

事务的传播:

​ 坐一辆车用REQUIRED

​ 坐新车用REQUIRED_NEW:当前事务总是使用一个新事务 如果已经有事务,会将之前事务挂起

任何地方崩异常,已经执行了的REQUIRED NEW 还是一定完成,都会成功

​ REQUIRED 事物的属性都是继承于大事务的

REQUIRES_NEW: 这个方法直接使用新的connection

XML方法配置事务

1
2
3
4
5
6
7
8
9
10
<aop:config>
<aop:pointcut id="txPoint" expression="execution(* com.runsstudio.service.*.*(..))"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"></aop:advisor>
</aop:config>
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="checkout" propagation="REQUIRED" timeout="-1"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>

写论文常用的词

用在开头

  • in this regard 在这一点上
  • Accordingly 替换 Therefore
  • threefold 三重的: The contribution of this paper is threefold
  • practitioners : 与researcher 连用
  • It has been well recognized that 达成共识的是——替换 Many experts believe that
  • have positive impacts on …
  • In light of this, additional efforts need to be made to…… 从这个角度来说,需要做额外的。。。工作
  • in doing so 在这种情况下

专有名词

  • inflows 输入流量: on-ramp traffic inflows
  • proportionately 成比例的

mybatis

Spring 专注于容器

Spring MVC 专注于模型

MyBatis 和数据库进行交互 是一个持久化层框架

什么是框架?是某个领域的整体解决方案

Mybatis 考虑:缓存、异常处理、字段映射

JDBC工具会产生什么问题?

1. 麻烦
 2. sql语句是硬编码在程序中的,耦合 每写一次都要重新打包项目

Hibernate - 数据库交互的框架(ORM框架) (使用各种框架)

ORM(Object Relation Mapping)对象关系映射

创建好java bean即可,数据库里没表 ORM可以创建表,JAVA BEAN 增删字段,在数据库中可以同步增删

写到后面都不需要掌握sql

为什么Hibernate 现在不用了 ? 因为他是黑箱操作 ,没法写sql