Spring系列教程六: Spring jdbcTemplate在Dao中的使用

概念

Spring中的jdbcTemplate的主要做用是實現數據的交互,下面咱們就在dao層中如何使用jdbctemplate寫測試案例java

項目目錄以下mysql

基於xml實現jdbctemplate

這裏咱們使用的是JdbcDaoSupport這個類,主要是爲了減小重複每次在dao層都要用的Jdbctemplate set方法spring

導入jar包sql

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

學生實體類app

package com.cc.entity;

import java.io.Serializable;

/**
 * 學生實體類
 */
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 +
                '}';
    }
}

方法接口ide

package com.cc.dao;

import com.cc.entity.Student;

/**
 * 定義接口
 */
public interface studentDao {
    /**
     * 根據id查找學生
     * @param
     * @return
     */
     Student findStudentbyId(Integer id);
    /**
     * 根據名稱查找用戶
     */
    Student findStudentbyName(String name);

    /**
     * 更新帳戶
     */
    void updateStudent(Student student);
}

方法實現類測試

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

配置文件this

<?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>
   <!-- 配置數據源-->
    <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>

測試類url

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("陳多多");
        System.out.println(stu);

        stu.setName("陳多糖");
        student.updateStudent(stu);

    }
}

測試結果以下spa

相關文章
相關標籤/搜索