JdbcTemplate(操作資料庫-查詢返回值)
阿新 • • 發佈:2021-01-02
目錄
JdbcTemplate(操作資料庫-查詢返回值)
1.建立資料庫
資料庫中有3條記錄,資料庫名是user_db,資料庫表是t_book
2.建立實體類
實體類屬性對應資料庫每一條記錄
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.建立dao層
抽象類:
package org.example.spring.dao;
import org.example.spring.entity.Book;
public interface BookDao {
//查詢的方法
int selectCount();
}
實現類:
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物件 @Autowired private JdbcTemplate jdbcTemplate; // 查詢 @Override public int selectCount() { String sql="select count(*) from t_book"; // 此方法有兩個引數 //一個引數是sql,一個引數是根據返回值型別而定,如果是String型別就返回String型別,是Int型別就返回Int型別 Integer count = jdbcTemplate.queryForObject(sql, Integer.class); return count; } }
4.建立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 @Autowired private BookDao bookDao; //查詢表中的記錄數 public int findCount(){ return bookDao.selectCount(); } }
5.建立測試類:
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
{
@Test
public void test01(){
ApplicationContext context=
new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
// 查詢返回某個值
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>
<!--資料庫連線池-->
<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>
<!-- 建立jdbcTemplate物件-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--需要注入資料來源資訊-->
<property name="dataSource" ref="dataSource">
</property>
</bean>
</beans>
7.測試結果:
8.結構示意: