1. 程式人生 > >spring框架學習之--資料庫操作增刪改查

spring框架學習之--資料庫操作增刪改查

基於spring的NamedParameterJdbcTemplate操作資料庫

  1. 首先在 resources 資料夾下新增資料庫配置檔案jdbc.properties 配置常用的資料庫資訊
    1 consult.jdbc.driverClassName=com.mysql.jdbc.Driver
    2 consult.jdbc.url=jdbc:mysql://xxxxxxxx:3307/consult_summer?useUnicode=true&characterEncoding=utf-8&tinyInt1isBit=false
    3 consult.jdbc.username=root
    
    4 consult.jdbc.password=123456

     

  2. 將配置檔案新增到集中載入的配置檔案中,即在applicationContext.xml檔案中新增如下內容
     1 <!-- 載入資原始檔,所有的資原始檔都集中載入,不要分散到其他spring配置檔案中,否則會找不到 -->
     2     <bean id="propertyConfigurer"
     3           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     4
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 5 <property name="locations"> 6 <list> 7 <value>classpath:jdbc.properties</value> 8 </list> 9 </property> 10 </bean>

     

  3. 然後在 applicationContext.xml 檔案中增加如下內容,用來對NamedParameterJdbcTemplate的bean 的引入
     1 <bean id="consultDataSource"
     2           class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     3         <property name="driverClassName" value="${consult.jdbc.driverClassName}" />
     4         <property name="url" value="${consult.jdbc.url}" />
     5         <property name="username" value="${consult.jdbc.username}" />
     6         <property name="password" value="${consult.jdbc.password}" />
     7     </bean>
     8 
     9     <bean id="consultTemplate"
    10           class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    11         <constructor-arg ref="consultDataSource" />
    12     </bean>

     

  4. 上面配置完成,下面開始寫一個測試類Testdemo來操作資料庫(以查詢為例)
     1 import org.apache.commons.logging.Log;
     2 import org.apache.commons.logging.LogFactory;
     3 import org.junit.Test;
     4 import org.junit.runner.RunWith;
     5 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
     6 import org.springframework.test.context.ContextConfiguration;
     7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     8 
     9 import javax.annotation.Resource;
    10 import java.util.HashMap;
    11 import java.util.List;
    12 import java.util.Map;
    13 import java.util.Set;
    14 
    15 @RunWith(SpringJUnit4ClassRunner.class)
    16 @ContextConfiguration(locations="classpath:applicationContext.xml")
    17 
    18 public class Testdemo {
    19 
    20     private static final Log log = LogFactory.getLog(TestJdbc.class);
    21 
    22     // 引入註解
    23     @Resource
    24     protected NamedParameterJdbcTemplate consultTemplate;
    25     @Test
    26     public  void test() throws Exception{
    27     
    28         StringBuffer sql = new StringBuffer("SELECT * FROM " + "order_info_ext_96"
    29                 + " WHERE 1=1 ");
    30         StringBuffer sqlToLog = new StringBuffer(sql);
    31         Map<String,Object> para=new HashMap<String, Object>();
    32         para.put("order_info_id","tm2zd0ww21190111030136219");
    33         Set<String> columnNames = para.keySet();
    34         for (String columnName : columnNames) {
    35             sql.append(" AND " + columnName + "=:" + columnName);
    36             sqlToLog.append(" AND " + columnName + "='"
    37                     + para.get(columnName) + "'");
    38         }
    39         List<Map<String, Object>> select_result=consultTemplate.queryForList(sql.toString(),para);
    40         log.info(select_result);  //列印查詢結果
    41 
    42     }
    43 }

     

  5. 執行即可檢視結果,執行完成即可將方法改成引數式的,以供呼叫