爲了使 JDBC 更加易於使用,Spring 在 JDBCAPI 上定義了一個抽象層, 以此創建一個JDBC存取框架,Spring Boot Spring Data-JPA。html
做爲 SpringJDBC 框架的核心, JDBC 模板的設計目的是爲不一樣類型的JDBC操做提供模板方法. 每一個模板方法都能控制整個過程,並容許覆蓋過程當中的特定任務。spring
經過這種方式,能夠在儘量保留靈活性的狀況下,將數據庫存取的工做量降到最低。sql
JdbcTemplate主要提供如下五類方法:數據庫
一、execute方法:能夠用於執行任何SQL語句,通常用於執行DDL語句;app
Execute、executeQuery、executeUpdate框架
二、update方法及batchUpdate方法:update方法用於執行新增、修改、刪除等語句;batchUpdate方法用於執行批處理相關語句 SQL SERVCER(GO SQL語句 GO) ;ide
三、query方法及queryForXXX方法:用於執行查詢相關語句;函數
四、call方法:用於執行存儲過程、函數相關語句。測試
public class Account { private Integer accountid; private String accountname; private Double balance; public Integer getAccountid() { return accountid; } public void setAccountid(Integer accountid) { this.accountid = accountid; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } }
//查詢全部全部帳戶 public List<Account> getAllAccounts();
.3 建立Dao層實現類並繼承JdbcDaoSupport接口this
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public List<Account> getAllAccounts() { JdbcTemplate jdbcTemplate = getJdbcTemplate(); String sql = "select * from accounts"; //RowMapper:接口 封裝了記錄的行映射關係 List<Account> lists = jdbcTemplate.query(sql, new RowMapper<Account>() { @Override public Account mapRow(ResultSet resultSet, int i) throws SQLException { //建立Account對象 Account account = new Account(); //從ResultSet中解數據保到Accounts對象中 account.setAccountid(resultSet.getInt("accountid")); account.setAccountname(resultSet.getString("accountname")); account.setBalance(resultSet.getDouble("balance")); return account; } }); return account; }
//查詢全部全部帳戶 public List<Account> getAllAccounts();
AccountDao accountDao; @Override public List<Account> getAllAccounts() { List<Account> allAccounts = accountDao.getAllAccounts(); return allAccounts; }
<!--識別到配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置數據源--> <!--spring內置的數據源:提供鏈接的,不負責管理,使用鏈接池--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--構建jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="accountDao" class="cn.spring.accounttest.dao.impl.AccountDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!--Service--> <bean id="accountService" class="cn.spring.accounttest.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean>
@Test public void getAllAccount(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //從spring容器中獲取Service對象 AccountService accountService = (AccountService)context.getBean("accountService"); List<Account> allAccounts = accountService.getAllAccounts(); for (Account account:allAccounts) { System.out.println("帳戶名:"+account.getAccountname()+",餘額爲:"+account.getBalance()); } }
@Repository public class AccountDaoImpl implements AccountDao { @Autowired private JdbcTemplate jdbcTemplate; Account account = new Account(); @Override public List<Account> getAllAccounts() { String sql = "select * from accounts"; //自動映射 RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class); List<Account> query = jdbcTemplate.query(sql, rowMapper); for (Account account : query) { System.out.println(account); } return query; } }
<!--掃描註解:包掃描器--> <context:component-scan base-package="cn.spring"/> <!--識別到配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置數據源--> <!--spring內置的數據源:提供鏈接的,不負責管理,使用鏈接池--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--構建jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean>
使用註解方式實現,配置式同添加操做
//刪除帳戶 public int delAccount(int id); //添加用戶 public int addAccount(Account account); //修改帳戶 public int updaAccount(Account account);
@Override public int delAccount(int id) { String sql="delete from accounts where accountid=2"; int count = jdbcTemplate.update(sql); return count; } @Override public int addAccount(Account account) { String sql="insert into Accounts(accountname,balance) values(?,?)"; int count = jdbcTemplate.update(sql,account.getAccountname(),account.getBalance()); return count; } @Override public int updaAccount(Account account) { String sql="update accounts set accountname=?,balance=? where accountid=?"; int count = jdbcTemplate.update(sql, account.getAccountname(),account.getBalance(),account.getAccountid() ); return count; }
@Test public void delAccount(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService =(AccountService) context.getBean("accountServiceImpl"); int i = accountService.delAccount(2); if (i>0){ System.out.println("刪除成功"); } } @Test public void addAccount(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl"); Account account=new Account(); account.setAccountname("劉磊"); account.setBalance(Double.valueOf(784)); int count = accountServiceImpl.addAccount(account); if (count>0){ System.out.println("添加成功"); } } @Test public void updaAcccount(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl"); Account account=new Account(); account.setAccountid(10); account.setAccountname("劉磊"); account.setBalance(Double.valueOf(784)); int count = accountServiceImpl.updaAccount(account); if (count>0){ System.out.println("修改爲功"); } }
原文出處:https://www.cnblogs.com/szhhhh/p/11781308.html