学习记录七

43 篇文章 0 订阅
23 篇文章 0 订阅

一、力扣打卡

正则表达式匹配

拿到这个题目的时候,就可以知道“ . ”表示匹配任意一个字符,“ * ”表示匹配任意多个前面那个字符。

首先判断字符规律中是不是只含有“ .* ”,是的话就一定会匹配成功

然后判断是不是含有" . "或者“ * ”,如果不含有就先匹配字符串和字符规律的长度,长度不相等那么一定匹配失败。 

最后判断其他情况

=========================================================================

二、Spring的事务控制

编程式事务控制指的是通过写代码的方式对事务进行控制

1.编程式事务控制相关对象

①PlatformTransactionManager(平台事务管理器)

所以在使用声明式事务控制的时候就需要告诉spring我使用的是哪一种实现技术!

②TransactionDefinition(封装事务的信息)

(1)事务的隔离级别

        

(2)事务的传播行为(在解决业务方法调用的时候的统一性的问题) 

【REQUIRED:A业务方法调用B业务方法的时候,如果A业务方法没有事务,B为他新建一个事务;如果A业务方法有事务,B就加入进去!】 

③TransactionStatus(事务的状态对象)

2.基于XML的声明式事务控制

①什么是声明式事务控制?

采用声明的方式来处理事务。这里所说的声明就是指在配置文件中声明,用在Spring配置文件中声明式的处理事务来代替代码式的处理事务。

②声明式事务控制的作用

a. 事务管理不侵入开发的组件。

b.在不需要事务管理的时候,只要在设定文件上修改一下,就可以移去事务管理服务,无需改变代码重新编译,这样维护起来很方便

【Spring声明事务控制底层就是AOP】

【典型案例:账户管理】

声明式事务控制明确事项:

··谁是切点?

··谁是通知?

··配置切面?

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="accountDao" class="dao.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <!--目标i对象 内部的方法就是我们的切点-->
    <bean id="accountService" class="service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!--配置平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--通知 事务的增强-->
    <!--transaction-manager指的就是平台事务管理器-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务AOP的织入-->
    <aop:config>
        <!--aop:advisor是专门为事务的增强提供的-->
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* service.impl.*.*(..))"></aop:advisor>
    </aop:config>
</beans>

Account.java 

package domain;

public class Account {

    private String name;
    private double money;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}

知识要点:

声明式事务控制的配置要点:平台事务管理器配置→事务通知的配置→事务AOP织入的配置 

3. 基于注解的声明式事务控制

4. Spirng集成web层

package com.wxy.web;

import com.wxy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件)方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。

那么就可以使用监听器的方式!

在Web项目中,可以使用ServletContextListener监听Web应用的启动, 我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicatiotiContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

设置如下:

ContextLoaderListener.java

package com.wxy.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        //将Spring的应用上下文对象存储到ServletContext域中
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("app",app);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

UserServlet.java

package com.wxy.web;

import com.wxy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //  ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = req.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <!--配置监听器-->
    <listener>
        <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.wxy.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>
    
</web-app>

优化!

上面就会限定加载文件只能是applicationContext.xml文件,那么我们可以配置的方式,让他后期修改只用在web.xml文件中进行修改就可以了

ContextLoaderListener.java

package com.wxy.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        //读取web.xml中的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");

        ApplicationContext app=new ClassPathXmlApplicationContext("contextConfigLocation");
        //将Spring的应用上下文对象存储到ServletContext域中

        servletContext.setAttribute("app",app);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>

    <!--配置监听器-->
    <listener>
        <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.wxy.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>
    
</web-app>

在上面的基础上,我还可以修改应用上下文到最大域的名字,否则需要用户记住名字! 

新创建一个工具类,就是用于获取名字

WebApplicationContextUtils.xml
package com.wxy.listener;

import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;

public class WebApplicationContextUtils {

    public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("app");
    }

}

UserService.java

package com.wxy.web;

import com.wxy.listener.WebApplicationContextUtils;
import com.wxy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //  ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = req.getServletContext();
       //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
        System.out.println("容器加载");
    }
}

4.1 Spring提供获取应用上下文的工具

UserServlet.java

package com.wxy.web;

import com.wxy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //  ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = req.getServletContext();
       //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
//        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
        System.out.println("容器加载");
    }
}

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--配置监听器-->
    <!--<listener>
        <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
    </listener>-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.wxy.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>
    
</web-app>

pom.xml

 <!--添加这个依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.17.RELEASE</version>
        </dependency>

三、算法复习

动态规划算法:

1. 将问题划分为若干子问题

2. 不同于分治法的是各个子问题之间不是相互独立的,一个子问题会依赖于前一个子问题

3. 动态规划算法可以通过画表的方式来实现

动态规划算法常见案例----背包问题

刚刚看了某位博主的文章,对动态规划算法也有了一定的理解!

对于0-1背包问题,我们可以总结为下面的步骤

①确定数组元素的含义

②确定数组之间的关系

③找到初始条件

对于0-1背包问题,我们设数组v[i][j],表示我们所求的价值

然后确定数组元素之间的关系

因为可以装进去也可以不装,所以有两种情况

先是重量判断,可以装进去的前提下会有下面的条件

v[i][j]=max(v[i-1][j],v[i]+v[i-1][j-w[i]] )

所以最后的表达式

动态规划问题之青蛙跳台阶问题

一个🐸一次可以跳一个或者两个台阶,请问🐸跳n个台阶有多少种跳法?
还是按照上面的步骤

①确定表达式的含义

首先,我们定义一个一维数组,dp[],那么dp[i]就表示跳i个台阶有dp[i]种跳法

②确定表达式之间的关系

然后我们可以知道,到了第i个台阶的时候,存在一种关系,就是它可以是跳两个台阶到的,也可以是跳一个台阶到的

所以dp[i]=dp[i-1]+dp[i-2]

③求得初始值

在这里,因为数组从0开始,所以我们可以知道我们需要首先给出dp[0]和dp[1]的值才不会导致到达负数,但是!我们会发现我们给出这两个数据之后,对于dp[2]来说是不对的,因为dp[2]=2,但是用上面的公式求得的解却是1,所以,我们还需要给出dp[2]的值,所以我们就要限定开始为dp[1]和dp[2] !!

最后放代码

public int gugua(int n){

    if(n<=1)
        return n;

    int dp[n];

//给出初始数据
    dp[1]=1;
    dp[2]=2;
    
//使用动态规划求解
    for(int i=3;i<=n;i++){
        dp[i]=dp[i-1]+dp[i-2];
    }
//返回所得值
    return dp[n];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值