Spring中JdbcTemplate的基礎用法


Spring中JdbcTemplate的基礎用法

1、在DAO中使用JdbcTemplate

一般都是在DAO類中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可。

 

 在Spring配置文件中配置DAO一般分為4個步驟:

1、定義DataSource

2、定義JdbcTimplate

3、聲明一個抽象的<bean>,以便所有的DAO復用配置JdbcTimplate屬性的配置

4、配置具體的DAO

復制代碼
 1  <!-- 配置事務管理器 --><!-- 掃描類包,將標注Spring注解的類自動轉化Bean,同時完成Bean的注入 -->
 2     <context:component-scan base-package="com.smart.dao"/>
 3 
 4     <!-- 配置數據源 -->
 5     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
 6           destroy-method="close"
 7           p:driverClassName="${jdbc.driverClassName}"
 8           p:url="${jdbc.url}"
 9           p:username="${jdbc.name}"
10           p:password="${jdbc.password}" />
11 
12     <!-- 配置Jdbc模板  -->
13     <!--JdbcTemplate 擁有幾個可用於控制底層的JDBC API的屬性
14       queryTimeout: 設置JdbcTimplate所創建的Statement查詢數據時的最大超時時間,默認0
15       fetchSize: 設置底層的ResultSet每次從數據庫返回的行數。該屬性對程序的影響很大,如果設置過大,
16                   因為一次性載入的數據都放到內存中,所以內存消耗很大;反之相反。默認為0,Oracle驅動程序的默認值為10
17       maxRows: 設置底層的ResultSet從數據庫返回的最大行數,默認值為0。
18       ignoreWarnings:是否忽略SQL的警告信息。默認true,即所有的警告信息都被記錄到日志中;
19                      如果為false,則JdbcTemplate將拋出SQLWarningException.
20     -->
21     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
22           p:dataSource-ref="dataSource" />
復制代碼

 

按照相同的方式可以方便的創建其他的DAO類,在Spring配置文件中定義JdbcTemplate並注入每個DAO中

復制代碼
 1 package com.smart.dao;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.jdbc.core.JdbcTemplate;
 5 import org.springframework.stereotype.Repository;
 6 
 7 @Repository
 8 public class TestDao {
 9     private JdbcTemplate jdbcTemplate;
10     @Autowired
11     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
12         this.jdbcTemplate = jdbcTemplate;
13     }
14     public void initDb(){
15         String sql = "create table test_user(user_id int primary key,user_name varchar(60))";
16         jdbcTemplate.execute(sql);
17     }
18 }
復制代碼

 

 2、更改數據

復制代碼
 1 public void Insert(User user){
 2         String sql = "INSERT INTO test_user(user_id,user_name) VALUES(?,?)";
 3         /*
 4         1、int update(String sql) 為不提供占位符的SQL語句提供便利
 5         2、int update(String sql,Object[] args) 第二個參數定義了用於填充占位符的參數數組,即SQL語句中的?
 6         3、int update(String sql,PreparedStatementSetter pss):PreparedStatementSetter是一個回調接口,
 7            它定義了一個void setValues(PreparedStatement ps)接口方法。JdbcTemplate使用SQL語句創建出
 8            PreparedStatement實例后,將調用接口執行參數綁定。
 9              jdbcTemplate.update(sql,new PreparedStatementSetter(){
10                public void setValues(PreparedStatement ps) throws SQLException{
11                 ps.setInt(1,user.getUserId());
12                 ps.setString(2,user.getUserName());
13             }
14         });
15         4、如下,在2的基礎上添加了第三個參數數組,用於顯式的指定每個占位符所對應的字段數據類型
16         */
17         Object [] params = new Object[] {user.getUserId(),user.getUserName()};
18         jdbcTemplate.update(sql,params,new int[]{Types.INTEGER,Types.VARCHAR});
19     }
復制代碼

User.class

復制代碼
 1 public class User implements Serializable{
 2  
 3     private int userId;
 4     private String userName;
 5     public int getUserId() {
 6         return userId;
 7     }
 8     public void setUserId(int userId) {
 9         this.userId = userId;
10     }
11     public String getUserName() {
12         return userName;
13     }
14     public void setUserName(String userName) {
15         this.userName = userName;
16     }
17   
18 }
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM