zoukankan      html  css  js  c++  java
  • Spring框架针对dao层的jdbcTemplate操作crud之delete删除数据库操作 Spring相关Jar包下载

    首先,找齐Spring框架中IoC功能、aop功能、JdbcTemplate功能所需的jar包,当前13个Jar包

    1、Spring压缩包中的四个核心JAR包,实现IoC控制反转的根据xml配置文件或注解生成对象

    beans 、context、core 和expression

    下载地址:

    https://pan.baidu.com/s/1qXLHzAW

    2、以及日志jar包,以便查看相关执行细节

    commons-logging 和log4j

    下载地址:

    https://pan.baidu.com/s/1mimTW5i

    3、再增加一个

    spring-aop-5.0.1.RELEASE.jar (用于注解,在Spring-framework库中包含)

    4、再增加

    spring-aspects-5.0.1.RELEASE.jar (在Spring-framework库中包含)

    aspectjweaver-1.8.12.jar (官方下载地址 http://mvnrepository.com/artifact/org.aspectj/aspectjweaver)

    aopalliance-1.0.jar            (官方下载地址 http://mvnrepository.com/artifact/aopalliance/aopalliance/1.0)

    实现aop功能,增强相关的切入点

    5、JdbcTemplate功能所需Jar包

    spring-jdbc-4.2.4.RELEASE.jar

    spring-tx-4.2.4.RELEASE.jar

    spring 框架jar包下载地址

    链接: https://pan.baidu.com/s/1bpydNGV 密码: 2kvk

    6、连接mysql数据库jar包

    mysql-connector-java-5.1.7-bin.jar

    下载地址:链接: https://pan.baidu.com/s/1geBRqqn 密码: 8jxm


    应用Spring框架的JdbcTemplate方法删除数据库中数据

    package com.swift;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.stereotype.Component;
    
    @Component(value="jdbcTemplateDemo")
    public class JdbcTemplateDemo {
        
        public boolean delete(String username) {
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/sw_database");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        
        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
        int count=jdbcTemplate.update("delete from sw_user where username=?", username);
        if(count!=0) {
            return true;
        }
        return false;
        }
    }

    注解方法生成对象,所需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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 开启注解扫描——对象和属性 -->
        <context:component-scan base-package="com.swift"></context:component-scan>
        <!-- 开启aop注解方法 -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
        
    </beans>

    调用JdbcTemplate删除数据库信息的Servlet类代码:

    package com.swift;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    @WebServlet("/test")
    public class ServletTest extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        public ServletTest() {
            super();
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getWriter().append("Served at: ").append(request.getContextPath());
            @SuppressWarnings("resource")
            ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
            JdbcTemplateDemo jdbcTemplateDemo=(JdbcTemplateDemo) context.getBean("jdbcTemplateDemo");
            if(jdbcTemplateDemo.delete("wangwu")) {
                response.getWriter().append("delete success!");
            }else {
                response.getWriter().append("delete success!");
            }
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }
  • 相关阅读:
    vue的环境的搭建
    rem
    web前端面试题总结
    vue遇到的一些问题
    栅格布局的理解
    利用node的http模块创建一个简单的http服务器
    node 常用的一些终端的命令的快捷键
    node 环境变量设置方法
    CentOS7.5搭建Hadoop2.7.6完全分布式集群
    UUID做主键,好还是不好?这是个问题
  • 原文地址:https://www.cnblogs.com/qingyundian/p/8053736.html
Copyright © 2011-2022 走看看