springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记

这篇文章将介绍怎样使用springBoot的jdbcTemplate来操作MySQL数据库,主要完成对student表的增删改查操作

首先打开idea,建立springBoot项目
springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记_第1张图片
点击next,选择项目的依赖(这里我们只测试数据库连接)
springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记_第2张图片
点击finish,完成项目构建,项目结构如下:
springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记_第3张图片
删除多余的文件,删除后如下:
springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记_第4张图片
建立包dao,在下面建立Student类,Operation接口,OperationImp类(实现Operation接口),如下图:
springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记_第5张图片

Student类的代码如下:

package com.example.springbootproject.dao;

public class Student {
    private String sno;
    private String sname;
    private String sex;
    private String birthday;
    private String phone;
    private String dorm;

    public Student() {
    }

    public Student(String sno, String sname, String sex, String birthday, String phone, String dorm) {
        this.sno = sno;
        this.sname = sname;
        this.sex = sex;
        this.birthday = birthday;
        this.phone = phone;
        this.dorm = dorm;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sno='" + sno + '\'' +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday='" + birthday + '\'' +
                ", phone='" + phone + '\'' +
                ", dorm='" + dorm + '\'' +
                '}';
    }

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getDorm() {
        return dorm;
    }

    public void setDorm(String dorm) {
        this.dorm = dorm;
    }
}

Operation接口代码:

package com.example.springbootproject.dao;

import java.util.List;

public interface Operation {
//    添加学生
    public void addStudent(Student student);
//    删除学生
    public void deleteStudent(String sno);
//    修改学生
    public void updateStudent(Student student);
//    查询所有学生
    public List<Student> selectAllStudent();
//    查询单个学生
    public Student selectStudent(String sno);
}

OperationImp类代码:

package com.example.springbootproject.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class OperationImp implements Operation{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addStudent(Student student) {
        jdbcTemplate.update("insert into student values (?,?,?,?,?,?)",student.getSno()
                ,student.getSname(),student.getSex(),student.getBirthday(),student.getPhone(),
                student.getDorm());
    }

    @Override
    public void deleteStudent(String sno) {
        jdbcTemplate.update("delete from student where sno=?",sno);
    }

    @Override
    public void updateStudent(Student student) {
        jdbcTemplate.update("update student set sname=?,sex=?,birthday=?,phone=?,dorm=? where sno=?",
                student.getSname(),student.getSex(),student.getBirthday(),student.getPhone(),
                student.getDorm(),student.getSno());
    }

    @Override
    public List<Student> selectAllStudent() {
        List<Student> list=jdbcTemplate.query("select * from student",
                new Object[]{},new BeanPropertyRowMapper<Student>(Student.class));
        return list;
    }

    @Override
    public Student selectStudent(String sno) {
        Student student=jdbcTemplate.queryForObject("select * from student where sno=?",
                new Object[]{sno},new BeanPropertyRowMapper<Student>(Student.class));
        return student;
    }
}

建立MySQL数据库,数据库名:mydatabase,表名:student
代码如下:

create database mydatabase;
CREATE TABLE student(
sno VARCHAR(20) PRIMARY KEY,
sname varchar(20),
sex varchar(20),
birthday VARCHAR(20),
phone varchar(20),
dorm VARCHAR(20)
);

在resources文件夹下的application.properties文件配置数据库信息:

#配置数据库信息
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名字(这里是mydatabase)?characterEncoding=utf-8&&useSSL=false&&serverTimezone=UTC
spring.datasource.username=数据库用户名(一般是root)
spring.datasource.password=数据库密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

在springBoot的测试单元中测试增删改查代码:
springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记_第6张图片
测试代码如下:

package com.example.springbootproject;

import com.example.springbootproject.dao.Operation;
import com.example.springbootproject.dao.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringBootProjectApplicationTests {
    @Autowired
private Operation operation;
    @Test
    void contextLoads() {
        Student student1 = new Student("1001","张三","男",
                "2002-2-2","123123123","123123123");
        Student student2 = new Student("1002","李四","男",
                "2003-3-3","3245234534","345345");
        Student student3 = new Student("1001","王二","女",
                "2004-4-4","3434244","323231");
//        添加两个学生
        operation.addStudent(student1);
        operation.addStudent(student2);
//        删除学号为1002的学生
        operation.deleteStudent("1002");
//        修改1001学生的信息
        operation.updateStudent(student3);
//        查询1001学生的信息
        Student student_test=operation.selectStudent("1001");
        System.out.println(student_test);
//        查询所有学生信息
//        先添加一个学生信息,这样就有两个了
        operation.addStudent(student2);
        List<Student> list=operation.selectAllStudent();
        System.out.println(list);
    }

}

运行结果如下:
springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记_第7张图片

查看数据库结果,如下:
springBoot使用jdbcTemplate操作MySQL数据库-springBoot学习笔记_第8张图片

你可能感兴趣的:(java,spring,boot,mysql,数据库)