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);
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM