JdbcTemplate(操作数据库-查询返回对象、查询返回集合)
生活随笔
收集整理的這篇文章主要介紹了
JdbcTemplate(操作数据库-查询返回对象、查询返回集合)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
JdbcTemplate(操作數(shù)據(jù)庫(kù)-查詢返回對(duì)象、查詢返回集合)
?
1.創(chuàng)建數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)中有三條記錄,數(shù)據(jù)庫(kù)名為user_db,數(shù)據(jù)庫(kù)表為t_book
?
2.新建實(shí)體類:
Book類中的每一個(gè)屬性對(duì)應(yīng)數(shù)據(jù)庫(kù)中的一條記錄
package org.example.spring.entity;public class Book {private int userId;private String username;@Overridepublic String toString() {return "Book{" +"userId=" + userId +", username='" + username + '\'' +'}';}public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}}?
?
3.創(chuàng)建dao層
抽象類:
package org.example.spring.dao;import org.example.spring.entity.Book;public interface BookDao {//查詢對(duì)象的方法Book findBookInfo(int id);//查詢集合的方法List<Book> findAllBook(); }實(shí)現(xiàn)類:
package org.example.spring.dao;import org.example.spring.entity.Book; 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 BookDaoImpl implements BookDao{//注入jdbcTemplate對(duì)象@Autowiredprivate JdbcTemplate jdbcTemplate;//查詢對(duì)象@Overridepublic Book findBookInfo(int id) {String sql="select * from t_book where user_id=?";//queryForObject()有三個(gè)參數(shù) // 第一個(gè)參數(shù)sql語句 // 第二個(gè)參數(shù) RowMapper(這是spring中封裝好的一個(gè)接口,針對(duì)返回不同類型數(shù)據(jù),使用這個(gè)接口里面的實(shí)現(xiàn)類完成數(shù)據(jù)的【封裝】) // 第三個(gè)參數(shù) sql語句值Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);return book;}//查詢集合@Overridepublic List<Book> findAllBook() {String sql="select * from t_book";//query()有三個(gè)參數(shù) // 第一個(gè)參數(shù)sql語句 // 第二個(gè)參數(shù) RowMapper(這是spring中封裝好的一個(gè)接口,針對(duì)返回不同類型數(shù)據(jù),使用這個(gè)接口里面的實(shí)現(xiàn)類完成數(shù)據(jù)的【封裝】) // 第三個(gè)參數(shù) sql語句值List<Book> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));return query;} }?
4.創(chuàng)建service類
package org.example.spring.service;import org.example.spring.dao.BookDao; import org.example.spring.entity.Book; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Service public class BookService {//注入dao@Autowiredprivate BookDao bookDao;//查詢返回對(duì)象,根據(jù)id查詢public Book findOne(int id){return bookDao.findBookInfo(id);}}?
5.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 開啟組件掃描--><context:component-scan base-package="org.example"></context:component-scan> <!--數(shù)據(jù)庫(kù)連接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"><property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="sise"/><property name="driverClassName" value="com.mysql.jdbc.Driver"/></bean><!-- 創(chuàng)建jdbcTemplate對(duì)象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--需要注入數(shù)據(jù)源信息--><property name="dataSource" ref="dataSource"></property></bean> </beans>?
?
6. 測(cè)試類
package org.example.spring.test;import org.example.spring.entity.Book; import org.example.spring.service.BookService; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBook {@Testpublic void test01(){ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");BookService bookService = context.getBean("bookService", BookService.class);//查詢返回對(duì)象Book one = bookService.findOne(2);System.out.println(one);//查詢返回集合List<Book> all = bookService.findAll();System.out.println(all);}}?
7.測(cè)試結(jié)果:
返回對(duì)象:
返回集合:
總結(jié)
以上是生活随笔為你收集整理的JdbcTemplate(操作数据库-查询返回对象、查询返回集合)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 30岁学python全栈_知乎热帖!戳痛
- 下一篇: linux cmake编译源码,linu