1、场景:查询图书详情
    2、JdbcTemplate 实现查询返回对象
    image.png

    第一个参数:sql 语句。
    第二个参数:RowMapper 是接口,针对返回不同类型数据,使用这个接口里面实现类完成数据封装。
    第三个参数:sql 语句值。

    dao接口:

    1. package com.wzy.jdbctemplate.dao;
    2. import com.wzy.jdbctemplate.pojo.Book;
    3. import java.util.List;
    4. public interface BookDao {
    5. List<Book> selectByList();
    6. }

    dao接口实现类:

    1. package com.wzy.jdbctemplate.dao.impl;
    2. import com.wzy.jdbctemplate.dao.BookDao;
    3. import com.wzy.jdbctemplate.pojo.Book;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.jdbc.core.BeanPropertyRowMapper;
    6. import org.springframework.jdbc.core.JdbcTemplate;
    7. import org.springframework.stereotype.Repository;
    8. import java.util.List;
    9. @Repository//创建对象
    10. public class BookDaoImpl implements BookDao {
    11. @Autowired//自动装配
    12. JdbcTemplate jdbcTemplate;
    13. @Override
    14. public List<Book> selectByList() {
    15. String sql = "select * from book";
    16. List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
    17. return bookList;
    18. }
    19. }

    service层类:

    1. package com.wzy.jdbctemplate.service;
    2. import com.wzy.jdbctemplate.dao.BookDao;
    3. import com.wzy.jdbctemplate.pojo.Book;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Service;
    6. import java.util.List;
    7. @Service//创建对象
    8. public class BookService {
    9. @Autowired//自动装配
    10. private BookDao bookDao;
    11. /*
    12. * @Description: 查询返回集合
    13. * @Author: WangZiYao
    14. * @Date: 2021/8/15 16:30
    15. */
    16. public List<Book> selectBookByList(){
    17. List<Book> books = bookDao.selectByList();
    18. return books;
    19. }
    20. }


    测试类:

    1. package com.wzy.jdbctemplate.test;
    2. import com.wzy.jdbctemplate.pojo.Book;
    3. import com.wzy.jdbctemplate.service.BookService;
    4. import org.junit.Test;
    5. import org.springframework.context.ApplicationContext;
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;
    7. import java.util.List;
    8. import static org.junit.Assert.*;
    9. public class BookServiceTest {
    10. ApplicationContext applicationContext =
    11. new ClassPathXmlApplicationContext("com/wzy/jdbctemplate/spring.xml");
    12. BookService bookService = applicationContext.getBean("bookService",BookService.class);
    13. @Test
    14. public void selectBookByList(){
    15. List<Book> books = bookService.selectBookByList();
    16. books.forEach(book -> System.out.println(book));
    17. }
    18. }


    输出结果:

    Book{id=1, username=’1086’, password=’123456’, email=’16515@163.com’} Book{id=2, username=’5565456fe’, password=’123456www’, email=’tom@atguigu.com’}

    Book{id=3, username=’146846fef’, password=’789456wfff’, email=’jerry@atguigu.com’}

    Book{id=4, username=’few1656564’, password=’4645646we’, email=’black@atguigu.com’}

    Book{id=5, username=’fwefw1666’, password=’46445weww’, email=’white@atguigu.com’}

    Process finished with exit code 0