springboot之JdbcTemplate

springboot可以使用JdbcTemplate进行数据库访问,代码如下

添加pom文件

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

代码如下

CREATE TABLE  if NOT EXISTS springboot.student
(
stu_no INT ,
name VARCHAR(20),
age INT
)

package com.rookie.bigdata.domain;

/**
 * @author
 * @date 2018/10/9
 */
public class Student {
    private Long stuNo;
    private String name;
    private Integer age;

    public Long getStuNo() {
        return stuNo;
    }

    public void setStuNo(Long stuNo) {
        this.stuNo = stuNo;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (stuNo != null ? !stuNo.equals(student.stuNo) : student.stuNo != null) return false;
        if (name != null ? !name.equals(student.name) : student.name != null) return false;
        return age != null ? age.equals(student.age) : student.age == null;
    }

    @Override
    public int hashCode() {
        int result = stuNo != null ? stuNo.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (age != null ? age.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuNo=" + stuNo +
                ", name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;

/**
 * @author
 * @date 2018/10/9
 */
public interface StudentService {

    /**
     * 添加一个学生
     *
     * @param student
     */
    void create(Student student);

    /**
     * 删除一个学生
     *
     * @param stuNo
     */
    void deleteByNo(Integer stuNo);

    /**
     * 查询学生
     *
     * @param stuNo
     */
    Integer queryByStuNo(Integer stuNo);
}
package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @author
 * @date 2018/10/9
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(Student student) {

        jdbcTemplate.update("INSERT INTO student(stu_no,name,age)VALUE (?,?,?)", student.getStuNo(), student.getName(), student.getAge());

    }

    @Override
    public void deleteByNo(Integer stuNo) {
        jdbcTemplate.update("DELETE  FROM  student WHERE stu_no=?", stuNo);
    }

    @Override
    public Integer queryByStuNo(Integer stuNo) {
        return jdbcTemplate.queryForObject("select count(1) from student", Integer.class);
    }
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

测试代码如下:

package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

/**
 * @author
 * @date 2018/10/9
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentServiceImplTest {

    @Autowired
    private StudentServiceImpl studentService;

    @Test
    public void create1() throws Exception {
        Student student = new Student();
        student.setStuNo(1L);
        student.setName("张三");
        student.setAge(23);

        studentService.create(student);

    }

    @Test
    public void deleteByName1() throws Exception {
        studentService.deleteByNo(1);
    }

    @Test
    public void queryByStuNo1() throws Exception {
        System.out.println(studentService.queryByStuNo(1));

    }

}

原文地址:https://www.cnblogs.com/haizhilangzi/p/9762668.html

时间: 2024-10-12 02:04:43

springboot之JdbcTemplate的相关文章

springboot 整合jdbcTemplate

springboot 整合jdbcTemplate 〇.搭建springboot环境(包括数据库的依赖) 一.添加依赖 如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency&g

SpringBoot使用JdbcTemplate

前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTemplate. 新建项目 新建一个项目.pom文件中加入Jdbc依赖,完整pom如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/PO

Springboot 使用JdbcTemplate

Springboot 使用JdbcTemplate book package com.draymonder.book.jdbc; public class Book { private Integer id; private String name; private String author; @Override public String toString() { return "Book{" + "id=" + id + ", name='"

SpringBoot整合JdbcTemplate连接Mysql

import java.io.IOException; import javax.sql.DataSource; import org.apache.ignite.IgniteSystemProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframe

Springboot使用JdbcTemplate RowMapper查询,直接返回实体列表

ManagerRowMapper.java public class ManagerRowMapper implements RowMapper<Manager>{ @Override public Manager mapRow(ResultSet rs, int i) throws SQLException { Manager m = new Manager(); m.setId(rs.getInt("ID")); m.setName(rs.getString("

SpringBoot中JdbcTemplate

步骤如下: 依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 1.dao层 public interface IGradeDao { public int insertGrade(Grade grade); public int upda

springboot使用jdbcTemplate案例

1 创建实体类 public class Student { private Integer stuid; private String stuname; public Integer getStuid() { return stuid; } public void setStuid(Integer stuid) { this.stuid = stuid; } public String getStuname() { return stuname; } public void setStunam

SpringBoot整合jdbcTemplate

一.目录展示 二.导入依赖 三.配置文件 四.Student实体类 package com.zn.entity; public class Student { private Integer stu_id; private String stu_name; @Override public String toString() { return "Student{" + "stu_id=" + stu_id + ", stu_name='" + s

Springboot整合druid

概述 前面的Springboot整合jdbcTemplate.mybatis.jpa三篇文章中使用的数据库连接池都是Spingboot默认配置的tomcat.jdbc.pool.配置原理进入org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration类查看,根据通过spring.datasource.type配置其他连接池,当然我们也可以配置指定的数据库连接池比如Druid.配置Druid有两种方式 第一种 配置传统的Dr