JdbcTemplate方法介紹與增刪改查操作實現
JdbcTemplate介紹
為了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定義了一個抽象層, 以此建立一個JDBC存取框架,Spring Boot Spring Data-JPA。
作為 SpringJDBC 框架的核心, JDBC 模板的設計目的是為不同類型的JDBC操作提供模板方法. 每個模板方法都能控制整個過程,并允許覆蓋過程中的特定任務。
通過這種方式,可以在盡可能保留靈活性的情況下,將數據庫存取的工作量降到最低。
JdbcTemplate方法介紹
JdbcTemplate主要提供以下五類方法:
1、execute方法:可以用于執(zhí)行任何SQL語句,一般用于執(zhí)行DDL語句;
Execute、executeQuery、executeUpdate
2、update方法及batchUpdate方法:update方法用于執(zhí)行新增、修改、刪除等語句;batchUpdate方法用于執(zhí)行批處理相關語句 SQL SERVCER(GO SQL語句 GO) ;
3、query方法及queryForXXX方法:用于執(zhí)行查詢相關語句;
4、call方法:用于執(zhí)行存儲過程、函數相關語句。
JdbcTemplate實現增刪改查
JdbcTemplate添加數據
1. 使用配置實現
1.1 創(chuàng)建實體類
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;
}
}
實體類
1.2 創(chuàng)建Dao層
//查詢所有所有賬戶 public List<Account> getAllAccounts();
.3 創(chuàng)建Dao層實現類并繼承JdbcDaoSupport接口
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 {
//創(chuàng)建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;
}
實現查詢方法
1.4創(chuàng)建Service層
//查詢所有所有賬戶 public List<Account> getAllAccounts();
1.5創(chuàng)建Service層實現類
AccountDao accountDao;
@Override
public List<Account> getAllAccounts() {
List<Account> allAccounts = accountDao.getAllAccounts();
return allAccounts;
}
1.6 編寫applicationContext.xml文件
<!--識別到配置文件-->
<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>
applicationContext.xml
1.7編寫測試類
@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());
}
}
查詢測試類
2. 使用注解方式實現
2.1 創(chuàng)建實體類
實體類
2.2 創(chuàng)建Dao層
查詢方法
2.3 創(chuàng)建Dao層實現類
@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;
}
}
Dao實現類
2.4創(chuàng)建Service層
查詢方法
2.5創(chuàng)建Service層實現類
實現查詢方法
2.6編寫applicationContext.xml文件
<!--掃描注解:包掃描器-->
<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>
applicationContext.xml
2.7編寫測試類
查詢測試類
JdbcTemplate實現增刪改操作
使用注解方式實現,配置式同添加操作
1.創(chuàng)建Dao層
//刪除賬戶 public int delAccount(int id); //添加用戶 public int addAccount(Account account); //修改賬戶 public int updaAccount(Account account); 增刪改方法
2.創(chuàng)建Dao曾實現類
@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;
}
增刪改方法實現類
3. 創(chuàng)建Service層
4. 創(chuàng)建Service層實現類
5. 編寫applicationContext.xml文件
6. 編寫測試類
@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("修改成功");
}
}
增刪改測試類
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關文章
springboot整合webservice使用簡單案例總結
WebService是一個SOA(面向服務的編程)的架構,它是不依賴于語言,平臺等,可以實現不同的語言間的相互調用,下面這篇文章主要給大家介紹了關于springboot整合webservice使用的相關資料,需要的朋友可以參考下2024-07-07
java線程池對象ThreadPoolExecutor的深入講解
在我們的開發(fā)中“池”的概念并不罕見,有數據庫連接池、線程池、對象池、常量池等等。下面這篇文章主要給大家介紹了關于java線程池對象ThreadPoolExecutor的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧2018-09-09
maven報錯:Failed to execute goal on p
這篇文章主要介紹了maven報錯:Failed to execute goal on project問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
springboot多數據源配合docker部署mysql主從實現讀寫分離效果
這篇文章主要介紹了springboot多數據源配合docker部署mysql主從實現讀寫分離,通過使用docker獲取mysql鏡像,具體內容詳情跟隨小編一起看看吧2021-09-09

