小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring中SimpleJdbcTemplate的用法實(shí)例(三)

 凌氏 2014-06-10

一、簡(jiǎn)介

SimpleJdbcTemplate內(nèi)部包含了一個(gè)NamedParameterJdbcTemplate,所以NamedParameterJdbcTemplate
能做的事情SimpleJdbcTemplate都能干,SimpleJdbcTemplate相對(duì)于NamedParameterJdbcTemplate主要
增加了JDK5.0的泛型技術(shù)可變長(zhǎng)度參數(shù)支持。

 

增加知識(shí):(NameParameterJdbcTemplate內(nèi)部包含了一個(gè)JdbcTemplate,所以JdbcTemplate能做的事情它都能做.NameParameterJdbcTemplate相對(duì)于JdbcTemplate主要增加了參數(shù)可以命名的功能)


simpleJdbcTemplate中有些方法不全,比如拿到主鍵的方法就沒(méi)有,可以獲取到NamedParameterJdbcTemplate
調(diào)用它里面的方法 getNamedParameterJdbcOperations()返回的是NamedParameterJdbcOperations其實(shí)現(xiàn)就是NamedParameterJdbcTemplate這個(gè)類
getJdbcOperations()返回的是JdbcOperation其實(shí)現(xiàn)就是JdbcTemplate
 
SqlParameterSource的兩個(gè)主要實(shí)現(xiàn)MapSqlParameterSourceBeanPropertyParameterSource類,它的作用是將實(shí)例中的屬性對(duì)應(yīng)到sql中的命名參數(shù)上去;而ParameterizedBeanPropertyRowMapper做查詢出來(lái)的結(jié)果映射到j(luò)ava類實(shí)例上去;

 

二、配置

 和JdbcTemplate一樣,SimpleJdbcTemplate也有3中配置方式:

  第一種方式:我們可以在自己定義的DAO 實(shí)現(xiàn)類中注入一個(gè)DataSource 引用來(lái)完 成SimpleJdbcTemplate的實(shí)例化。也就是它是從外部“注入” DataSource 到DAO 中,然后 自己實(shí)例化SimpleJdbcTemplate,然后將DataSource 設(shè)置到SimpleJdbcTemplate對(duì)象中。
第二種方式: 在 Spring 的 IoC 容器中配置一個(gè) SimpleJdbcTemplate的 bean,將 DataSource 注入進(jìn)來(lái),然后再把SimpleJdbcTemplate注入到自定義DAO 中。
第三種方式: Spring 提供了 org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport類 , 這 個(gè) 類 中 定 義 了 NamedParameterJdbcTemplate 屬性,也定義了DataSource 屬性,當(dāng)設(shè)置DataSource 屬性的時(shí)候,會(huì)創(chuàng) 建SimpleJdbcDaoSupport的實(shí)例,所以我們自己編寫(xiě)的DAO 只需要繼承SimpleJdbcDaoSupport類, 然后注入DataSource 即可。提倡采用第三種方法。雖然下面的用法中采用了前兩種方法

 

配置方法有3種:

1、

Java代碼  收藏代碼
  1. public class UserServiceImpl implements UserService {  
  2.   
  3.     private SimpleJdbcTemplate simpleJdbcTemplate;  
  4.       
  5.     public SimpleJdbcTemplate getSimpleJdbcTemplate () {  
  6.         return simpleJdbcTemplate;  
  7.     }  
  8.   
  9.                 //注入方法1     
  10.     public void setSimpleJdbcTemplate (SimpleJdbcTemplate simpleJdbcTemplate) {  
  11.         this.simpleJdbcTemplate= simpleJdbcTemplate;  
  12.     }  
  13.   
  14.                //其它方法這里省略……  
  15. }  

spring配置文件為:

Xml代碼  收藏代碼
  1. <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">  
  2.         <property name = "dataSource" ref="dataSource">  
  3. </bean>  
  4. <bean id="userService" class="com.hxzy.account.simpleJdbcTemplate.UserServiceImpl">  
  5.      <property name="simpleJdbcTemplate" ref="simpleJdbcTemplate"/>  
  6. </bean>  

 

方法2、

Java代碼  收藏代碼
  1. public class UserServiceImpl implements UserService {  
  2.   
  3.         private SimpleJdbcTemplate simpleJdbcTemplate;  
  4.           
  5.         //注入方法2  
  6.         public void setDataSource(DataSource dataSource) {  
  7.                    this.simpleJdbcTemplate= new JdbcTemplate(dataSource);  
  8.         }  
  9.        
  10.        //其它方法省略……  
  11. }  

 

spring配置文件為:

Xml代碼  收藏代碼
  1. <bean id="userService" class="com.hxzy.account.jdbcTemplate.UserServiceImpl">  
  2.        <property name="dataSource" ref="dataSource"/>  
  3. </bean>  

 

方法3:繼承SimpleJdbcDaoSupport,其內(nèi)部有個(gè)namedParamterJdbcTemplate ,需要注入DataSource 屬性來(lái)實(shí)例化。

Java代碼  收藏代碼
  1. public class UserDaoImpl extends SimpleJdbcDaoSupport implements UserDao {  
  2.   
  3.     @Override  
  4.     public void save(User user) {  
  5.         String sql = null;  
  6.         this.getSimpleJdbcDaoSupport().update(sql);  
  7.     }  
  8.         //其它方法省略……  
  9. }  

 

spring配置文件:

 

Xml代碼  收藏代碼
  1. <bean id="userDao" class="com.hxzy.account.jdbcTemplate.UserDaoImpl">  
  2.            <property name="dataSource" ref="dataSource"/>  
  3. </bean>  

 

 

三、使用

 

Java代碼  收藏代碼
  1. @Override  
  2.     public void save(User user) {  
  3.         String sql = "insert into tb_test1(id,username,password,sex) "  
  4.                 + "values (:id,:username,:password,:sex)";  
  5.         this.simpleJdbcTemplate.getNamedParameterJdbcOperations().update(sql,  
  6.                 new BeanPropertySqlParameterSource(user));  
  7.     }  
  8.       
  9.     @Override  
  10.     public void update(User user) {  
  11.         String sql = "update tb_test1 set username=:username,password=:password,sex=:sex where id=:id";  
  12.         this.simpleJdbcTemplate.update(sql,  
  13.                 new BeanPropertySqlParameterSource(user));  
  14.     }  
  15.       
  16.     public void deleteById(String id) {  
  17.         String sql = "delete from tb_test1 where id=?";  
  18.         this.simpleJdbcTemplate.update(sql, id);  
  19.     }  
  20.       
  21.     public Object queryById(String id) {  
  22.         String sql = "select * from tb_test1 where id=?";  
  23.         return simpleJdbcTemplate.queryForObject(sql,  
  24.                 ParameterizedBeanPropertyRowMapper.newInstance(User.class), id);  
  25.     }  
  26.       
  27.     public List<?> queryAll() {  
  28.         String sql = "select * from tb_test1" ;  
  29.         return this.simpleJdbcTemplate.query(sql,  
  30.                 ParameterizedBeanPropertyRowMapper.newInstance(clazz));  
  31.     }  
  32.   
  33. private List<User> queryEntityByExample(User user) {  
  34.                String sql = "select * from tb_test1 where (username=:username or :username IS NULL) and (password=:password or :password IS NULL)";  
  35.                return this.simpleJdbcTemplate.query(sql, ParameterizedBeanPropertyRowMapper.newInstance(User.class), new BeanPropertySqlParameterSource(user));  
  36.  }  

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多