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

}

 

转载于:https://www.cnblogs.com/qingyundian/p/8053736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值