JdbcTemplate(操作数据库-查询返回值)
生活随笔
收集整理的這篇文章主要介紹了
JdbcTemplate(操作数据库-查询返回值)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
?
JdbcTemplate(操作數(shù)據(jù)庫(kù)-查詢返回值)
1.創(chuàng)建數(shù)據(jù)庫(kù)
2.創(chuàng)建實(shí)體類
3.創(chuàng)建dao層
4.創(chuàng)建service層
5.創(chuàng)建測(cè)試類:
6.xml配置
7.測(cè)試結(jié)果:
8.結(jié)構(gòu)示意:
?
JdbcTemplate(操作數(shù)據(jù)庫(kù)-查詢返回值)
?
?
1.創(chuàng)建數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)中有3條記錄,數(shù)據(jù)庫(kù)名是user_db,數(shù)據(jù)庫(kù)表是t_book
?
2.創(chuàng)建實(shí)體類
實(shí)體類屬性對(duì)應(yīng)數(shù)據(jù)庫(kù)每一條記錄
package org.example.spring.entity;public class Book {private String userId;private String username;public String getUserId() {return userId;}public void setUserId(String 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 {//查詢的方法int selectCount(); }實(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.JdbcTemplate; import org.springframework.stereotype.Repository;@Repository public class BookDaoImpl implements BookDao{//注入jdbcTemplate對(duì)象@Autowiredprivate JdbcTemplate jdbcTemplate;// 查詢@Overridepublic int selectCount() {String sql="select count(*) from t_book"; // 此方法有兩個(gè)參數(shù)//一個(gè)參數(shù)是sql,一個(gè)參數(shù)是根據(jù)返回值類型而定,如果是String類型就返回String類型,是Int類型就返回Int類型Integer count = jdbcTemplate.queryForObject(sql, Integer.class);return count;} }?
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;//查詢表中的記錄數(shù)public int findCount(){return bookDao.selectCount();}}?
5.創(chuàng)建測(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);// 查詢返回某個(gè)值int count = bookService.findCount(); // 做輸出System.out.println(count);}}?
?
6.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>?
7.測(cè)試結(jié)果:
?
8.結(jié)構(gòu)示意:
?
?
?
總結(jié)
以上是生活随笔為你收集整理的JdbcTemplate(操作数据库-查询返回值)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 达梦数据库删除用户_达梦数据库的操作手册
- 下一篇: JAVA进阶教学之(源码及API文档概述