• SpringMVC(三、JDBCTemplate和拦截器)


    JDBCTemplate

    1 JdbcTemplate概述

    ​ 什么是 JdbcTemplate(Template)译为模板?

    ​ Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作。它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作 模板类。例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操 作消息队列的JmsTemplate等等。

    2 JdbcTemplate开发步骤

    ① 导入spring-jdbc和spring-tx坐标

    
    
    	org.springframework
    	spring-jdbc
    	5.0.5.RELEASE
    
    
    
    	org.springframework
    	spring-tx
    	5.0.5.RELEASE
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ② 创建数据库表和实体

    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;
        }
    
        @Override
        public String toString() {
            return "Account{" +
                    "name='" + name + '\'' +
                    ", money=" + money +
                    '}';
        }
    }
    
    • 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

    ③ 创建JdbcTemplate对象

    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import java.beans.PropertyVetoException;
    
    public class JdbcTemplateTest {
    
        @Test
        //测试Spring产生jdbcTemplate对象
        public void test2() throws PropertyVetoException {
            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
            JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
            int row = jdbcTemplate.update("insert into account values(?,?)", "lisi", 5000);
            System.out.println(row);
        }
    
        @Test
        //测试JdbcTemplate开发步骤
        public void test1() throws PropertyVetoException {
            //创建数据源对象
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            dataSource.setUser("root");
            dataSource.setPassword("root");
    
            JdbcTemplate jdbcTemplate = new JdbcTemplate();
            //设置数据源对象  知道数据库在哪
            jdbcTemplate.setDataSource(dataSource);
            //执行操作
            int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
            System.out.println(row);
    
        }
    
    }
    
    • 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

    ④ 执行数据库操作

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import java.util.List;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class JdbcTemplateCRUDTest {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Test
        public void testQueryCount(){
            Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
            System.out.println(count);
        }
    
        @Test
        public void testQueryOne(){
            Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper(Account.class), "tom");
            System.out.println(account);
        }
    
        @Test
        public void testQueryAll(){
            List accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper(Account.class));
            System.out.println(accountList);
        }
    
        @Test
        public void testUpdate(){
            jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
        }
    
        @Test
        public void testDelete(){
            jdbcTemplate.update("delete from account where name=?","tom");
        }
    
    }
    
    • 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

    Spring产生JdbcTemplate对象

    ​ 我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将 数据源DataSource注入到JdbcTemplate模版对象中,配置如下:

     	
        
    
        
        
            
            
            
            
        
    
        
        
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/test
    jdbc.username=root
    jdbc.password=root
    
    • 1
    • 2
    • 3
    • 4

    拦截器

    1 拦截器的作用

    ​ Spring MVC 的拦截器(interceptor)类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理。 将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(Interceptor Chain)。在访问被拦截的方 法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。拦截器也是AOP思想的具体实现。

    2 拦截器和过滤器区别

    区别过滤器(Filter)拦截器(Interceptor)
    使用范围是 servlet 规范中的一部分,任何 Java Web 工程都可以使用是 SpringMVC 框架自己的,只有使用了 SpringMVC 框架的工程才能用
    拦截范围在 url-pattern 中配置了/*之后, 可以对所有要访问的资源拦截在中配置了/**之 后,也可以多所有资源进行拦截,但是可以通 过标签 排除不需要拦截的资源

    3 拦截器是快速入门

    自定义拦截器很简单,只有如下三步:

    ① 创建拦截器类实现HandlerInterceptor接口

    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class MyInterceptor1 implements HandlerInterceptor {
        //在目标方法执行之前 执行
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
            System.out.println("preHandle.....");
            String param = request.getParameter("param");
            if("yes".equals(param)){
                return true;
            }else{
                request.getRequestDispatcher("/error.jsp").forward(request,response);
                return false;//返回true代表放行  返回false代表不放行
            }
        }
    
        //在目标方法执行之后 视图对象返回之前执行
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
            modelAndView.addObject("name","itheima");
            System.out.println("postHandle...");
        }
    
        //在流程都执行完毕后 执行
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
            System.out.println("afterCompletion....");
        }
    }
    
    • 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

    ② 配置拦截器

      
        
            
                
                
                
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ③ 测试拦截器的拦截效果

    测试再访问时实现被拦截效果

    4 多拦截器操作

    同上,在编写一个MyHandlerInterceptor2操作,测试执行顺序

    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class MyInterceptor2 implements HandlerInterceptor {
        //在目标方法执行之前 执行
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
            System.out.println("preHandle22222.....");
            return true;
        }
    
        //在目标方法执行之后 视图对象返回之前执行
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
            System.out.println("postHandle2222...");
        }
    
        //在流程都执行完毕后 执行
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
            System.out.println("afterCompletion2222....");
        }
    }
    
    • 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
    
        
            
                
                
                
            
            
                
                
                
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5 拦截器方法说明

    方法名说明
    preHandle()方法将在请求处理之前进行调用,该方法的返回值是布尔值Boolean类型的, 当它返回为false 时,表示请求结束,后续的Interceptor 和Controller 都不会 再执行;当返回值为true 时就会继续调用下一个Interceptor 的preHandle方 法
    postHandle()该方法是在当前请求进行处理之后被调用,前提是preHandle 方法的返回值为 true 时才能被调用,且它会在DispatcherServlet 进行视图返回渲染之前被调 用,所以我们可以在这个方法中对Controller 处理之后的ModelAndView 对象 进行操作
    afterCompletion()该方法将在整个请求结束之后,也就是在DispatcherServlet 渲染了对应的视图 之后执行,前提是preHandle 方法的返回值为true 时才能被调用
  • 相关阅读:
    【C++学习笔记】C++20的jthread
    centos7配置
    数据挖掘学习——支持向量机(SVM)
    电子电器架构 --- 智能座舱技术分类
    用C++11 make_shared替代shared_ptr
    新一代分布式实时流处理引擎Flink入门实战之先导理论篇-上
    函数防抖(javaScript)
    使用Python进行可视化
    RTMP协议详解及Wiresahrk抓包分析
    MySQL 查看 event 执行记录
  • 原文地址:https://blog.csdn.net/qq_53463544/article/details/126120716