Spring JDBC初嘗試

Spring JDBC簡化了JDBC的大部分工作,只需要專注於sql語句與執行,但它不是ORM,級聯屬性不能像hibernate那樣直接處理。

Spring JDBC的關鍵類是JdbcTemplate。


1、applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


<!-- 指定數據源的配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!--
利用 BasicDataSource配置數據源
org.apache.commons.dbcp2.BasicDataSource 需要commons-dbcp2-2.1.1.jar,依賴commons-pool2-2.4.2.jar
-->

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
</bean>


<!-- 利用spring的DriverManagerDataSource配置數據源 -->
<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
</bean>

<!-- 配置spring的jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

PS:BasicDataSource數據配置依賴的jar包,可以在我的雲盤下載http://blog.csdn.net/gw85047034/article/details/52768693


2、TestJDBC

import java.sql.SQLException;
import java.util.List;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;


public class TestJDBC {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);

@Test
public void testQueryForObject() throws SQLException {
// 使用RowMapper將數據庫與實體類對應起來,常用的實現類爲BeanPropertyRowMapper
// sql語句裏面的字段名稱要與實體類的成員屬性一致 ,順序是無關的
// 如果表結構的字段與實體類屬性名一直,可以直接寫成select *
// 不支持級聯屬性

String sql = "select id,name,age,grade from student where id= ? ";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
Student student = jdbcTemplate.queryForObject(sql, rowMapper,9);
System.out.println(student);
}

@Test
public void testQueryForList() {
// 也是使用RowMapper將數據庫與實體類對應起來
// 使用jdbcTemplate.query查詢,而不是用jdbcTemplate.queryForList

String sql = "select id,name,age,grade from student";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
List<Student> students = jdbcTemplate.query(sql, rowMapper);
System.out.println(students);
}
}

發佈了46 篇原創文章 · 獲贊 10 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章