JdbcTemplate概述
它是spring框架中提供的一個對象,是對原始繁瑣的JdbcAPI對象的簡單封裝。spring框架為我們提供了很多的操作模板類。例如:操作關系型數據的JdbcTemplate和MbernateTemplate,操作nosql數據庫的RedisTemplate,操作消息隊列的JmsTemplate等等。
JdbcTemplate開發步驟
① 導入spring-jdbc和spring-tx坐標
② 創建數據庫表和實體
③ 創建JdbcTemplate對象
④ 執行數據庫操作
@Test// 測試JdbcTemplate的開發步驟public void test2() throws PropertyVetoException {ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);int row = jdbcTemplate.update("insert into account values(?, ?)", "lisi11", 1000);System.out.println(row);}//-----------------------------------------------------<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 加載jdbc.properties--><context:property-placeholder location="classpath: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><!-- jdbc模板對象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean></beans>//----------------------------------------------jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=1234
?JdbcTemplate基本使用-常用操作-更新刪除操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void testUpdate(){jdbcTemplate.update("update account set money = ? where name = ?",10001, "wangwu");}@Testpublic void testDelete(){jdbcTemplate.update("delete from account where money = ?", 10001);}
}
?JdbcTemplate基本使用-常用操作-查詢操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void testUpdate(){jdbcTemplate.update("update account set money = ? where name = ?",10001, "wangwu");}@Testpublic void testDelete(){jdbcTemplate.update("delete from account where money = ?", 10001);}//查詢多個@Testpublic void estQuery(){List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));System.out.println(accountList);}//查詢一個@Testpublic void testQueryOne(){List<Account> accountList = jdbcTemplate.query("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "lisi");System.out.println(accountList);}//聚合查詢@Testpublic void testQueryCount(){Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);System.out.println(count);}
}