Spring相關介面和配置資料來源
Spring的相關介面(API):
- ApplicationContext:介面型別,代表應用上下文,可以通過其例項獲得spring容器中的bean物件
ApplicationContext的實現類:
1) ClassPathApplicationContext 它是從類的根路徑下載入配置檔案推薦使用
2) FileSystemXmlApplicationContext 它是從磁碟路徑上載入配置檔案,配置檔案可以在磁碟的任意位置
3) AnnotationConfigApplicationContext 當使用註解配置容器物件時,需要使用此類建立spring容器,它是用來讀取註解。
在配置好Spring的核心配置檔案(applicationContext.xml)後,我們在使用這配置檔案總是會使用以下兩句:
//連結spring容器
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//下面getBean中的id是在配置檔案中id名字,
app.getBean("id");
Spring配置資料來源
之前學習jdbc連線資料庫
1、載入資料庫驅動;
Class.forName(“com.mysql.jdbc.Driver”);
2、獲取資料庫連線;
通過Connection建立連線,Connection是一個介面類。其功能是與資料庫進行連線(會話)。
建立Connection介面類物件:
Connection conn =DriverManager.getConnection(url, user, password);
3、建立sql操作;
執行物件Statement負責執行SQL語句。由Connection物件產生。
Statement st = connection.createStatement();
Statement介面類還派生出兩個介面類PreparedStatement和CallableStatement,這兩個介面類物件為我們提供了更加強大的資料訪問功能。
PreparedStatement能夠對SQL語句進行預編譯,這樣防止了 SQL注入 提高了安全性。
PreparedStatement ps=connection.prepareStatement( "update user set id=? where username=?”); ————sql語句中庸 ? 作為萬用字元,變數值通過引數設入:ps.setObject(1, object);
而且預編譯結果能夠儲存在PreparedStatement物件中。當多次執行SQL語句時能夠提高效率。並且使用PrepareStatement物件還能有效的避免sql語句的注入,提高對資料庫安全性。
4、執行sql語句;
executeQuery(String sql),該方法用於執行實現查詢功能的sql語句。返回型別為ResultSet(結果集)。
如:ResultSet rs =st.executeQuery(sql);
executeUpdate(Stringsql),該方法用於執行實現增、刪、改功能的sql語句,返回型別為int,即受影響的行數。
如:int flag = st.executeUpdate(sql);
5、處理結果集
6、釋放資源。(後開先關的原則)
資料來源的作用:
資料來源(連線池)是提高程式效能
事先例項化資料來源,初始化部分連線部分
使用連線資源從資料來源中獲取
使用完畢後連線資源歸返給資料來源
常見的資料來源(連線池):DBCP、C3P0、BoneCP、Druid等
使用spring配置資料來源:
開發步驟:
1、 匯入連線池的座標和資料庫的驅動座標
2、 建立連線池物件
3、 設定資料來源的基本連線資料
下面以Druid資料來源為例:
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
DruidPooledConnection connection = druidDataSource.getConnection();
System.out.println(connection);
connection.close();
通過上例我們不難發現它們是直接把資料池寫道資料裡--耦合。
於是我們使用jdbc.properties檔案
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/本地資料庫的名字
jdbc.username=使用者名稱
jdbc.password=密碼
使用spring載入資料庫的配置檔案可以進一步的進行解耦,可以做到一部分檔案執行一部檔案的內容,逐步的解耦便於後續的開發。在jdbc.properties檔案的同級下建立一個Spring配置檔案applicationContext.xml
在配置檔案中我們需要使用jdbc的配置檔案,這就需要我們在spring配置檔案中是使用context標籤來載入外部的檔案
context 需要在跟標籤下新增
<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"
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">
<!-- 載入外部的properties檔案-->
<context:property-placeholder location="classpath:db.properties"/>
在spring配置檔案使用Druid資料來源
<bean id="db" class="com.alibaba.druid.pool.DruidDataSource" >
<!-- <property name="driverClass" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/****"/>-->
<!-- <property name="user" value="root"/>-->
<!-- <property name="password" value="****"/>-->
<!-- 使用外部配置檔案的屬性名進行載入資料庫-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
在bean中有一段是註釋的是未使用jdbc的配置檔案。
然後再測試類中建立一個測試類:
@Test
public void test4() throws SQLException {
// 連結spring容器
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
// 從容器中獲取所需的連結池的資源
DataSource dataSource = app.getBean("db",DataSource.class);
// 載入驅動
Connection connection = dataSource.getConnection();
System.out.println(connection);
}