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='" + name + '\'' +
        ", author='" + author + '\'' +
        '}';
  }

  public Integer getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

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

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }
}

bookDao

package com.draymonder.book.jdbc;

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

@Repository
public class BookDao {

  @Autowired
  JdbcTemplate jdbcTemplate;

  public int addBook(Book book) {
    return jdbcTemplate
        .update("insert into book(name, author) values (?, ?)", book.getName(), book.getAuthor());
  }

  public int updateBook(Book book) {
    return jdbcTemplate
        .update("update book set name=?, author=? where id=?", book.getName(), book.getAuthor(),
            book.getId());
  }

  public int deleteBookById(Integer id) {
    return jdbcTemplate.update("delete from book where id=?", id);
  }

  public Book getBookById(Integer id) {
    return jdbcTemplate.queryForObject("select * from book where id=?", new BeanPropertyRowMapper<>(Book.class), id);
  }

  public List<Book> getAllBooks() {
    return jdbcTemplate.query("select * from book", new BeanPropertyRowMapper<>(Book.class));
  }

}

bookService

package com.draymonder.book.jdbc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService {

  @Autowired
  BookDao bookDao;

  public int addBook(Book book) {
    return bookDao.addBook(book);
  }

  public int updateBook(Book book) {
    return bookDao.updateBook(book);
  }

  public int deleteBookById(Integer id) {
    return bookDao.deleteBookById(id);
  }

  public Book getBookById(Integer id) {
    return bookDao.getBookById(id);
  }

  public List<Book> getAllBooks() {
    return bookDao.getAllBooks();
  }

}

bookController

package com.draymonder.book.jdbc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BookController {
  @Autowired
  BookService bookService;

  @GetMapping("/bookOps")
  public void bookOps() {
    Book b1 = new Book();
    b1.setName("西厢记");
    b1.setAuthor("王实甫");
    int i = bookService.addBook(b1);
    System.out.println("add book >>> " + i);

    Book b2 = new Book();
    b2.setId(1);
    b2.setName("朝花夕拾");
    b2.setAuthor("鲁迅");
    int updateBook = bookService.updateBook(b2);
    System.out.println("update book >>> " + updateBook);

    Book b3 = bookService.getBookById(1);
    System.out.println("get book >>> " + b3);

    int delete = bookService.deleteBookById(1);
    System.out.println("delete book >>> " + delete);

    List<Book> allBooks = bookService.getAllBooks();
    allBooks.forEach(System.out::println);
  }
}

原文地址:https://www.cnblogs.com/Draymonder/p/11826532.html

时间: 2024-10-13 09:11:58

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进行数据库访问,代码如下 添加pom文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <properti

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