Spring series six: the use of spring JDBC template in Dao

concept

The main function of JDBC template in Spring is to realize data interaction. Next, we will use JDBC template to write test cases in dao layer

The project contents are as follows

Implementation of JDBC template based on xml

Here we use the class of JdbcDaoSupport, mainly to reduce the repetition of the Jdbctemplate set method used every time in the dao layer

Import jar package

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
 </dependency>

Student entity

package com.cc.entity;

import java.io.Serializable;

/**
 * Student entity
 */
public class Student implements Serializable {

    private int id;
    private String stuno;
    private String name;
    private int classid;

    public int getId() {

        return id;
    }

    public void setId(int id) { this.id = id; }

    public String getStuno() {
        return stuno;
    }

    public void setStuno(String stuno) {
        this.stuno = stuno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getClassid() {
        return classid;
    }

    public void setClassid(int classid) {
        this.classid = classid;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", stuno='" + stuno + '\'' +
                ", name='" + name + '\'' +
                ", classid=" + classid +
                '}';
    }
}

Method interface

package com.cc.dao;

import com.cc.entity.Student;

/**
 * Defining interfaces
 */
public interface studentDao {
    /**
     * Find students by id
     * @param
     * @return
     */
     Student findStudentbyId(Integer id);
    /**
     * Find users by name
     */
    Student findStudentbyName(String name);

    /**
     * Update account
     */
    void updateStudent(Student student);
}

Method implementation class

package com.cc.dao;

import com.cc.entity.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class StudentDaoImpl extends JdbcDaoSupport implements studentDao {

    @Override
    public Student findStudentbyId(Integer id) {
        List<Student> students =getJdbcTemplate().query("select * from student where id=?",new BeanPropertyRowMapper<Student>(Student.class),id);
        return students.isEmpty()?null:students.get(0);
    }

    @Override
    public Student findStudentbyName(String name) {
        List<Student> students =getJdbcTemplate().query("select * from student where name=?",new BeanPropertyRowMapper<Student>(Student.class),name);
        if(students.isEmpty()){
            return null;
        }
        if(students.size()>1){
            throw  new RuntimeException("Return result set is not unique");
        }
        return students.get(0);
    }

    @Override
    public void updateStudent(Student student) {
        getJdbcTemplate().update("update student set stuno=?,name=?,classid=? where id=?",
                student.getStuno(),student.getName(),student.getClassid(),student.getId());
    }
}

configuration file

<?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"
       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">
    <bean id="studentDao" class="com.cc.dao.StudentDaoImpl">
       <property name="dataSource" ref="dataSource"></property>
    </bean>
   <!-- Configure data sources-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/fresh?characterEncoding=UTF-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

Test class

import com.cc.dao.StudentDaoImpl;
import com.cc.entity.Student;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Testjdbctemplate {
    @Autowired
    private StudentDaoImpl student;
    @Test
    public void testfindbyId(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        StudentDaoImpl student=ac.getBean("studentDao", StudentDaoImpl.class);
        Student stu =student.findStudentbyId(1);
        System.out.println(stu);
    }
    @Test
    public void testfindbyname(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        StudentDaoImpl student=ac.getBean("studentDao", StudentDaoImpl.class);
        Student stu=student.findStudentbyName("Chen dodo");
        System.out.println(stu);

        stu.setName("Chen polysaccharide");
        student.updateStudent(stu);

    }
}

The test results are as follows

Tags: Programming JDBC Spring xml MySQL

Posted on Wed, 06 Nov 2019 10:00:06 -0500 by Dimitri89