Spring——JdbcTemplate
簡介
它是一個Spring框架提供的一個物件。是對原始JDBC API物件的簡單封裝。
Idea快捷鍵:
雙擊shift,快速查詢類的原始碼
shift+shift:輸入RowMapper
ctrl+shift+B:選中一個類名,看它的實現
導包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.5.RELEASE</version> </dependency>
配置
<!-- properties --> <context:property-placeholder location="jdbc.properties"/> <!-- 配置資料來源物件 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置JdbcTemplate物件 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" scope="singleton"> <property name="dataSource" ref="dataSource"/> </bean>
其他類
BeanProperRowMapper:實體屬性行對映
作用:作為update的引數,用於直接封裝多個bean物件為:List<Bean>,Bean
例項化:
new BeanProperRowMapper<Bean>(Bean.class)
方法
void setDataSource(DataSource datasource)
引數:
dataSource:資料來源
返回值:void
返回值意義:無
作用:通過dataSource設定JdbcTemplate
...args :代表有多少佔位符。就多少引數
int update(String str,...args)
引數:
str:sql語句
...args:佔位符賦值
返回值:int
返回值意義:影響行數
作用:更新操作
T queryForObject(String sql , T.class )
引數:
sql:查詢語句
T.class:基本型別的封裝型別
返回值:基本型別T
返回值意義:獲取查詢結果的基本型別T
作用:獲取查詢結果的基本類T
T queryForObject(String sql , BeanProperRowMap<T>(T.class) , ..args )
引數:
sql:sql查詢語句
BeanProperRowMapper:封裝多個Bean為List<Bean>的物件,類似BeansHandler
...args:為佔位符賦值
返回值:Object
返回值意義:獲取查詢結果的封裝物件
作用:獲取查詢結果的封裝物件
List<T> query(String sql , BeanProperRowMapper<T>(T.class) ,...args )
引數:
sql:sql查詢語句
BeanProperRowMapper:封裝多個Bean為List<Bean>的物件,類似BeanHandler
...args:為佔位符賦值
返回值:List<T>
返回值意義:獲取查詢結果集的封裝物件集合
作用:獲取查詢結果集的封裝物件集合
使用
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JdbcTemplateTest { @Autowired private JdbcTemplate jdbcTemplate; @Test // 測試JDBC模板開發步驟 public void test6() throws PropertyVetoException, SQLException { //查詢操作,查詢總數 Integer integer = jdbcTemplate.queryForObject("select count(money) from account ", Integer.class); System.out.println(integer); } @Test // 測試JDBC模板開發步驟 public void test5() throws PropertyVetoException, SQLException { //查詢操作,查詢單個物件 Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), 4); System.out.println(account); } @Test // 測試JDBC模板開發步驟 public void test4() throws PropertyVetoException, SQLException { //查詢操作,查詢多個物件 List<Account> query = jdbcTemplate.query("select * from account ", new BeanPropertyRowMapper<Account>(Account.class)); ArrayList<Account> accountArray = (ArrayList<Account>) query; System.out.println(accountArray.toString()); } @Test // 測試JDBC模板開發步驟 public void test3() throws PropertyVetoException, SQLException { //更新操作 int row = jdbcTemplate.update("insert into account (name,money) values (?,?)","lisi","7000"); } @Test // 測試JDBC模板開發步驟 public void test2() throws PropertyVetoException, SQLException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); //更新操作 int row = jdbcTemplate.update("insert into account (name,money) values (?,?)","zhangsan","4000"); } @Test // 測試JDBC模板開發步驟 public void test1() throws PropertyVetoException, SQLException { // 建立資料來源物件 ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/user"); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("844597608a"); System.out.println(comboPooledDataSource.getConnection()); JdbcTemplate jdbcTemplate = new JdbcTemplate(); // 設定資料來源,獲取Connect物件 jdbcTemplate.setDataSource(comboPooledDataSource); //更新操作 int row = jdbcTemplate.update("insert into account (name,money) values (?,?)","tom","5000"); } }