The Spring framework delete s database operations for the jdbcTemplate operation crud of the dao layer and downloads Spring-related Jar packages

Keywords: Java Spring JDBC xml MySQL

First, find the jar packages needed for IoC, aop and JdbcTemplate functions in the Spring framework. There are currently 13 Jar packages

1. Four core JAR packages in Spring Compression Pack to realize IoC control inversion and generate objects according to xml configuration files or annotations

beans, context, core and expression

Download address:

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

2. and log jar packages to see the execution details

commons-logging and log4j

Download address:

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

3. Add another one

spring-aop-5.0.1.RELEASE.jar (for annotations, included in the Spring-framework library)

4. Increase again

spring-aspects-5.0.1.RELEASE.jar (included in the Spring-framework library)

aspectjweaver-1.8.12.jar (official download address: http://mvnrepository.com/artifact/org.aspectj/aspectjweaver)

aopalliance-1.0.jar (official download address http://mvnrepository.com/artifact/aopalliance/aopalliance/1.0)

Implementing aop functions and enhancing relevant entry points

5. Jar packages required for JdbcTemplate functionality

spring-jdbc-4.2.4.RELEASE.jar

spring-tx-4.2.4.RELEASE.jar

spring framework jar package download address

Link: https://pan.baidu.com/s/1bpydNGV password: 2kvk

6. Connect mysql database jar package

mysql-connector-java-5.1.7-bin.jar

Download address: link: https://pan.baidu.com/s/1geBRqn password: 8jxm

 

Applying JdbcTemplate Method of Spring Framework to Delete Data in Database

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

Annotation method generates object, which requires xml configuration file code:

<?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">
    <!-- Open Annotation Scan - Objects and Properties -->
    <context:component-scan base-package="com.swift"></context:component-scan>
    <!-- open aop Annotation method -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
</beans>

Call JdbcTemplate to delete the Servlet class code for database information:

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

}

Posted by genecide on Thu, 07 Feb 2019 08:30:17 -0800