Spring Data 02 : Spring JdbcTemplate 访问 MySQL

目录

Spring Data 01 :JDBC 访问 MySQL

Spring Data 02 :Spring JdbcTemplate 访问 MySQL

Spring Data 03 :JPA入门

Spring Data 04:Spring Data JPA入门

Spring Data 05 : Spring Data JPA + SpringBoot2 集成多数据源


一、依赖

    
      mysql
      mysql-connector-java
      8.0.15
    

    
      org.springframework
      spring-jdbc
      5.1.5.RELEASE
    

    
      org.springframework
      spring-context
      5.1.5.RELEASE
    
    
    
      junit
      junit
      4.11
      test
    

二、创建bean.xml




    
        
        
        
        
    

    
        
    

    // 创建完StudentDAOSpringJDBCTemplateImpl 后将其添加进来
    
        
    

三、对bean.xml进行单元测试

package com.springboot.utils;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

public class DataSourceTest {

    private ApplicationContext ctx = null;

    @Before
    public void setup(){
        ctx = new ClassPathXmlApplicationContext("beans.xml");
    }

    @After
    public void tearDown(){
        ctx = null;
    }

    @Test
    public void testDataSource(){
        DataSource dataSource = (DataSource)ctx.getBean("dataSource");
        Assert.assertNotNull(dataSource);
    }

    @Test
    public void testJdbcTemplate(){
        JdbcTemplate jdbcTemplate= (JdbcTemplate)ctx.getBean("jdbcTemplate");
        Assert.assertNotNull(jdbcTemplate);
    }

}

四、创建 Student DAO接口的实现类 StudentDAOSpringJDBCTemplateImpl

package com.springboot.dao;
import com.springboot.dao.StudentDAO;
import com.springboot.domain.Student;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class StudentDAOSpringJDBCTemplateImpl implements StudentDAO {

    // 注入JdbcTemplate 生成getter and setter
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    // 继承接口的查询
    @Override
    public List query() {
        final ArrayList students = new ArrayList<>();
        String sql = "select id, name, age from student";

        jdbcTemplate.query(sql, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");

                Student student = new Student();
                student.setId(id);
                student.setName(name);
                student.setAge(age);

                students.add(student);
            }
        });
        return students;
    }

    // 继承接口的 添加
    @Override
    public void save(Student student) {
        String sql = "insert into student(name ,age) values (?,?)";
        jdbcTemplate.update(sql,new Object[]{student.getName(),student.getAge()});

    }
}

五、对查询、新增 进行测试

package com.springboot.dao;

import com.springboot.domain.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class StudentDAOSpringJdbcTemplateImplTest {


    private ApplicationContext ctx = null;
    protected StudentDAO studentDAO =null;

    @Before
    public void setup(){
        ctx = new ClassPathXmlApplicationContext("beans.xml");
        studentDAO = (StudentDAO)ctx.getBean("studentDAO");
    }

    @After
    public void tearDown(){
        ctx = null;
    }


    // 查询所有记录
    @Test
    public void testQuery(){
        List students = studentDAO.query();
        for (Student student:students){
            System.out.println("id"+student.getId()
                    +"name"+student.getName()
                    +"age"+student.getAge()
            );
        }

    }

    // 新增一个记录
    @Test
    public void testSave(){
        Student student = new Student();
        student.setName("小辣椒");
        student.setAge(25);
        studentDAO.save(student);
    }
}

你可能感兴趣的:(Spring Data 02 : Spring JdbcTemplate 访问 MySQL)