spring之关于Template的使用

使用步骤

  1. 引入相关jar包
  2. 创建数据源DataSource数据库连接池
  3. 创建JdbcTemplate对象,将DataSource注入
  4. 将service中注入dao,在dao中注入JdbcTemplate

引入相关jar包

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <!-- spring aop支持 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.2.2.RELEASE</version>
    </dependency>
    <!--AspectJ支持-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.geronimo.bundles</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8_2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.27</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.4</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>4.3.2

创建数据源DataSource数据库连接池

 <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql://localhost:3307/demo9"/>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

创建JdbcTemplate对象,将DataSource注入

 <!--创建JdbcTemplate对象,将dataSource注入到里面-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

将service中注入dao,在dao中注入JdbcTemplate

创建dao接口数据库访问层

public interface BookDao {
//增加图书
public int addBook(Book book);
//修改图书
public int updateBookById(Book book);
//删除图书
public int deleteBookById(Integer id);
//查询图书所有数量
public int countBooks();
//查询图书通过id
public Book queryBookById(Integer id);
//查询图书列表
public List queryBookList();
}

创建实现类

@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
String sql=null;

@Override
public int addBook(Book book) {
   sql="insert into book values(?,?,?,?)";
   Object[] args={book.getBookId(),book.getBookAuthor(),book.getBookName(),book.getBookPrice()};
    int update = jdbcTemplate.update(sql, args);
    return update;
}

@Override
public int updateBookById(Book book) {
    sql="update book set book_name=? ,book_author=?,book_price=? where book_id=?";
    Object[] args={book.getBookName(),book.getBookAuthor(),book.getBookPrice(),book.getBookId()};
    int update = jdbcTemplate.update(sql, args);
    return update;
}

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

@Override
public int countBooks() {
    sql="select count(*) from book";
    Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
    return integer;
}

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

@Override
public List<Book> queryBookList() {
    sql="select * from book";
    List<Book> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
    return query;
}

创建实体类Book

package com.wt.pojo;

public class Book {
    private Integer bookId;
    private String bookName;
    private double bookPrice;
    private String bookAuthor;

    public Book() {
    }

    public Book(Integer bookId, String bookName, double bookPrice, String bookAuthor) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookPrice = bookPrice;
        this.bookAuthor = bookAuthor;
    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public double getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(double bookPrice) {
        this.bookPrice = bookPrice;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", bookPrice=" + bookPrice +
                ", bookAuthor='" + bookAuthor + '\'' +
                '}';
    }
}

创建service

package com.wt.service;

import com.wt.pojo.Book;

import java.util.List;

public interface BookService {
    //增加图书
    public int addBook(Book book);
    //修改图书
    public int updateBookById(Book book);
    //删除图书
    public int deleteBookById(Integer id);
    //查询图书所有数量
    public int countBooks();
    //查询图书通过id
    public Book queryBookById(Integer id);
    //查询图书列表
    public List<Book> queryBookList();
}

创建service实现类

package com.wt.service.impl;

import com.wt.dao.BookDao;
import com.wt.pojo.Book;
import com.wt.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookDao bookDao;

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

    @Override
    public int updateBookById(Book book) {
        return bookDao.updateBookById(book);
    }

    @Override
    public int deleteBookById(Integer id) {

        return bookDao.deleteBookById(id);
    }

    @Override
    public int countBooks() {
        return bookDao.countBooks();
    }

    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBookList() {
        return bookDao.queryBookList();
    }
}

创建测试类

在这里插入代码片package com.wt.test;

import com.wt.pojo.Book;
import com.wt.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Mytest {
    @Test
    public void test1(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
        BookService bookService = applicationContext.getBean("bookServiceImpl", BookService.class);
        //测试新增
        Book book=new Book();
        book.setBookAuthor("尚硅谷");
        book.setBookName("数据结构与算法");
        book.setBookPrice(98);
        bookService.addBook(book);
        //测试查询数据条数
        int count = bookService.countBooks();
        System.out.println("数据总数:"+count);
        //测试查询某条数据通过id
        Book book1 = bookService.queryBookById(2);
        System.out.println("查询结果是:"+book1);
        //测试查询所有数据
        List<Book> list = bookService.queryBookList();
        for(Book book2:list){
            System.out.println(book2);
        }
        //修改某条数据
        Book book3=new Book();
        book3.setBookId(2);
        book3.setBookName("操作系统实战");
        book3.setBookPrice(99);
        book3.setBookAuthor("千峰教育集团");
        int i = bookService.updateBookById(book3);
        System.out.println("修改成功与否");

        //删除某条数据
        int i1 = bookService.deleteBookById(2);
        System.out.println("删除成功与否"+i1);
    }
}

上一篇:背包问题动态规划求解


下一篇:最新Springboot详解和书管项目训练