javascript
JAVAEE框架之Spring JdbcTemplate
七.JdbcTemplate
? 概念:Spring對數(shù)據(jù)庫的操作在jdbc上面做了深層次的封裝,使用spring的注入功能,可以把DataSource注冊到JdbcTemplate之中。目的是使JDBC更加易于使用。JdbcTemplate是Spring的一部分。 JdbcTemplate處理了資源的建立和釋放。
作用:
1.不需要管理連接
2.不需要設(shè)置參數(shù)
3.可以返回實體類
常用方法:
- execute方法:可以用于執(zhí)行任何SQL語句,一般用于執(zhí)行DDL語句;
- update方法及batchUpdate方法:update方法用于執(zhí)行新增、修改、刪除等語句;batchUpdate方法用于執(zhí)行批處理相關(guān)語句;
- query方法及queryForXXX方法:用于執(zhí)行查詢相關(guān)語句;
- call方法: 用于執(zhí)行存儲過程、函數(shù)相關(guān)語句。
7.1使用步驟
準(zhǔn)備工作:要有數(shù)據(jù)庫、數(shù)據(jù)表
hr庫,大家也可以自己建立一個數(shù)據(jù)庫、數(shù)據(jù)表。
7.1.1 導(dǎo)入jar依賴
<!--導(dǎo)入相應(yīng)的jar依賴--> <dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.5.RELEASE</version></dependency><!--增加spring-jdbc的依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.5.RELEASE</version></dependency><!--增加對mysql 連接的jar依賴--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version></dependency><!--增加對junit的jar依賴--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version></dependency> </dependencies>7.1.2 配置數(shù)據(jù)源
數(shù)據(jù)源是什么???DataSource
//1.spring jdbc數(shù)據(jù)源;看下這個單詞:DriverManager +DataSource DriverManagerDataSource dataSource=new DriverManagerDataSource(); //手動設(shè)置驅(qū)動 url 用戶名 密碼;如果你的是5.1; com.mysql.jdbc.Driver dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/hr"); dataSource.setUsername("root"); dataSource.setPassword("root");7.1.3 創(chuàng)建JdbcTemplate對象
//2.創(chuàng)建JdbcTemplate對象; JdbcTemplate jdbcTemplate=new JdbcTemplate(); //new JdbcTemplate(dataSource) jdbcTemplate.setDataSource(dataSource); //將上面的數(shù)據(jù)源對象,建立和JdbcTemplate對象的關(guān)聯(lián);7.1.4 執(zhí)行 增刪改操作
//3.執(zhí)行增刪改查的操作; jdbcTemplate.execute("delete from account where id=1");存在的問題是什么呢???
1.數(shù)據(jù)庫配置的代碼,放到了java里面,根據(jù)“高內(nèi)聚,低耦合”原則,應(yīng)該盡量做到分離;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--通過spring配置文件來實現(xiàn)低耦合--><!--1.配置DataSource數(shù)據(jù)源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/hr?useTimezone=true&serverTimezone=CTT&useUnicode=true&characterEncoding=utf8&useSSL=false"/><property name="username" value="root"/><property name="password" value="root"/></bean><!--配置JdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><!--這個對應(yīng)setDataSource方法,將set后的方法名首字母小寫--><property name="dataSource" ref="dataSource"/></bean> </beans>測試類:
//1.要通過Spring來訪問xml;new 完之后,Alt+Enter自動出來前面的變量名,然后名字可以自行修改ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("beans.xml");//2.通過spring 配置文件來獲取響應(yīng)的對象JdbcTemplate jdbcTemplate= (JdbcTemplate) ac.getBean("jdbcTemplate"); jdbcTemplate.update("insert account(uid,money) values (10,99999)"); System.out.println("插入數(shù)據(jù)完畢");針對Junit的知識點擴充:
額外的補充了一個知識點:
@Before:void方法之前
@After: void方法之后
@Test:用于單元測試的void方法
import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate;/*** Created by 張晨光 on 2020/6/29 15:52*/ public class TestJdbcTemplateTwo {ClassPathXmlApplicationContext ac;JdbcTemplate jdbcTemplate;/*** 這個注解是在junit單元測試,常規(guī)的void方法之前進(jìn)行;*/@Beforepublic void before(){System.out.println("開始了...");//1.要通過Spring來訪問xml;new 完之后,Alt+Enter自動出來前面的變量名,然后名字可以自行修改ac= new ClassPathXmlApplicationContext("beans.xml");//2.通過spring 配置文件來獲取響應(yīng)的對象jdbcTemplate= (JdbcTemplate) ac.getBean("jdbcTemplate");}/*** 這個注解是在junit單元測試,常規(guī)的void方法之后進(jìn)行;*/@Afterpublic void after(){System.out.println("整體結(jié)束...");}@Testpublic void test(){ //3.執(zhí)行操作--》增加操作;jdbcTemplate.update("insert account(uid,money) values (10,99999)");System.out.println("插入數(shù)據(jù)完畢");}@Testpublic void testUpdate(){//3.執(zhí)行操作--》增加操作;jdbcTemplate.update("update account set money=9988 where uid=10");System.out.println("更新數(shù)據(jù)完畢");}@Testpublic void testDelete(){//3.執(zhí)行操作--》增加操作;jdbcTemplate.update("delete from account where uid=10");System.out.println("刪除數(shù)據(jù)完畢");} }2.要擴充連接池技術(shù),下次講;
“低耦合"實現(xiàn),使用Spring框架
總結(jié):
1.已經(jīng)學(xué)習(xí)過了Spring框架,對于復(fù)雜的企業(yè)業(yè)務(wù)邏輯,進(jìn)行解耦操作,降低系統(tǒng)的復(fù)雜度;
2.Spring框架封裝了原生 JDBC,就是JdbcTemplate,可以實現(xiàn)對數(shù)據(jù)庫的增刪改查操作,注意需要依賴于DataSource數(shù)據(jù)源類;
作業(yè):
使用spring來對JdbcTemplate進(jìn)行注入,實現(xiàn)增刪改業(yè)務(wù)操作。
7.2 RowMapper
Spring提供的對數(shù)據(jù)庫查詢數(shù)據(jù)封裝的接口。
通過實現(xiàn)接口,實現(xiàn)接口mapRow方法(),通過對數(shù)據(jù)的封裝就是通過mapRow方法實現(xiàn)
@FunctionalInterface public interface RowMapper<T> {@NullableT mapRow(ResultSet var1, int var2) throws SQLException; }? BeanPropertyRowMapper這是對RowMapper的實現(xiàn)類,它可以把ResultSet和實體類的字段進(jìn)行實現(xiàn)自動映射,可以給同名字段進(jìn)行封裝。自動將一行數(shù)據(jù)映射到指定類的實例, 首先將這個類實例化,然后通過名稱匹配的方式,映射到屬性中去。
7.2.1 查詢數(shù)據(jù)
//需要提前去預(yù)習(xí)知識點:RowMapper // List<Account> accounts = jdbcTemplate.query("select * from account where money>?", new AccountRowMapper(), 2200);/*List<Account> accounts = jdbcTemplate.query("select * from account where money>?", new BeanPropertyRowMapper<Account>(Account.class), 2200);for(Account account:accounts){System.out.println(account);}*///單一的賬戶/*List<Account> accounts = jdbcTemplate.query("select * from account where money=?", new BeanPropertyRowMapper<Account>(Account.class), 8899);System.out.println(accounts.get(0));*///返回一行一列的數(shù)據(jù);Integer count=jdbcTemplate.queryForObject("select Max(money) from account where money>?", Integer.class, 2200);System.out.println(count);7.2.2 分層設(shè)計實現(xiàn)
作;
jdbcTemplate.update(“delete from account where uid=10”);
System.out.println(“刪除數(shù)據(jù)完畢”);
}
}
@FunctionalInterface
public interface RowMapper {
@Nullable
T mapRow(ResultSet var1, int var2) throws SQLException;
}
// List accounts = jdbcTemplate.query(“select * from account where money>?”, new AccountRowMapper(), 2200);
/List accounts = jdbcTemplate.query(“select * from account where money>?”, new BeanPropertyRowMapper(Account.class), 2200);
for(Account account:accounts){
System.out.println(account);
}/
//單一的賬戶
/List accounts = jdbcTemplate.query(“select * from account where money=?”, new BeanPropertyRowMapper(Account.class), 8899);
System.out.println(accounts.get(0));/
//返回一行一列的數(shù)據(jù);
Integer count=jdbcTemplate.queryForObject(“select Max(money) from account where money>?”, Integer.class, 2200);
System.out.println(count);
總結(jié)
以上是生活随笔為你收集整理的JAVAEE框架之Spring JdbcTemplate的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVAEE框架之Spring AOP
- 下一篇: JAVAEE框架之SpringMVC基础