Spring中的JdbcTemplate詳細(xì)解析
一、JdbcTemplate概述
它是Spring框架中提供的一個(gè)對(duì)象,是對(duì)原始繁瑣的Jdbc API對(duì)象的簡(jiǎn)單封裝。
Spring框架為我們提供了很多的操作模板類。
例如:操作關(guān)系型數(shù)據(jù)的JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)的RedisTemplate,操作消息隊(duì)列的JsmTeSmplate等等
二、JdbcTemplate開發(fā)步驟
① 導(dǎo)入spring-jdbc和spring-tx坐標(biāo)
② 創(chuàng)建數(shù)據(jù)庫表和實(shí)體
③ 創(chuàng)建JdbcTemplate對(duì)象
④執(zhí)行數(shù)據(jù)庫操作
更新操作:
jdbcTemplate.update (sql,params)
查詢操作:
jdbcTemplate.query (sql,Mapper,params)
jdbcTemplate.queryForObject (sql,Mapper,params)
三、JdbcTemplate快速入門
① 導(dǎo)入坐標(biāo)
<!--導(dǎo)入spring的jdbc坐標(biāo)--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.5.RELEASE</version> </dependency> <!--導(dǎo)入spring的tx坐標(biāo)--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.5.RELEASE</version> </dependency>
② 創(chuàng)建accout表和Accout實(shí)體
CREATE DATABASE Test CREATE TABLE ACCOUNT( NAME VARCHAR(255), money INT); SELECT * FROM ACCOUNT
public class Account { private String name; private double money; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } @Override public String toString() { return "Account{" + "name='" + name + '\'' + ", money=" + money + '}'; } }
③ 創(chuàng)建JdbcTemplate對(duì)象
④ 執(zhí)行數(shù)據(jù)庫操作
public class JdbcTemplateTest { @Test //測(cè)試JdbcTemplate快速入門 public void test1() throws PropertyVetoException { //創(chuàng)建數(shù)據(jù)源對(duì)象 ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUser("root"); dataSource.setPassword("ggm"); //創(chuàng)建模板對(duì)象 JdbcTemplate jdbcTemplate = new JdbcTemplate(); //設(shè)置數(shù)據(jù)源對(duì)象 jdbcTemplate.setDataSource(dataSource); //執(zhí)行操作 int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000); System.out.println(row); } }
四、Spring產(chǎn)生JdbcTemplate對(duì)象
我們可以將JdbcTemplate的創(chuàng)建權(quán)交給Spring,將數(shù)據(jù)源DataSource的創(chuàng)建權(quán)也交給Spring,在Spring容器內(nèi)部將 數(shù)據(jù)源DataSource注入到JdbcTemplate模版對(duì)象中,配置如下:
<!--配置數(shù)據(jù)源對(duì)象--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--jdbc模板對(duì)象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>
從容器中獲得JdbcTemplate進(jìn)行添加操作
@Test //測(cè)試Spring產(chǎn)生Jdbc模板對(duì)象 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(?,?)", "zhangsan", 6000); System.out.println(row); }
五、JdbcTemplate的常用操作
- 修改操作
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JdbcTemplateCRUDTest { @Autowired private JdbcTemplate jdbcTemplate; //修改操作 @Test public void testUpdate() { jdbcTemplate.update("update account set money=? where name=?", 10000, "tom"); } }
- 刪除和查詢?nèi)坎僮?/li>
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JdbcTemplateCRUDTest { @Autowired private JdbcTemplate jdbcTemplate; //刪除操作 @Test public void testDelete() { jdbcTemplate.update("delete from account where name=?", "tom"); } //查詢多個(gè)對(duì)象 @Test public void testQueryAll() { List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class)); System.out.println(accountList); } }
- 查詢單個(gè)數(shù)據(jù)操作操作
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JdbcTemplateCRUDTest { @Autowired private JdbcTemplate jdbcTemplate; //查詢單個(gè)對(duì)象 @Test public void testQueryOne() { Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom"); System.out.println(account); } //聚合查詢 @Test public void testQueryCount() { Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class); System.out.println(count); } }
到此這篇關(guān)于Spring中的JdbcTemplate詳細(xì)解析的文章就介紹到這了,更多相關(guān)Spring之JdbcTemplate內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深度剖析java動(dòng)態(tài)靜態(tài)代理原理源碼
這篇文章主要介紹了深度剖析java動(dòng)態(tài)靜態(tài)代理原理源碼,關(guān)于Java中的動(dòng)態(tài)代理,我們首先需要了解的是一種常用的設(shè)計(jì)模式--代理模式,而對(duì)于代理,根據(jù)創(chuàng)建代理類的時(shí)間點(diǎn),又可以分為靜態(tài)代理和動(dòng)態(tài)代理。,需要的朋友可以參考下2019-06-06Java 中 String,StringBuffer 和 StringBuilder 的區(qū)別及用法
這篇文章主要介紹了Java 中 String,StringBuffer 和 StringBuilder 的區(qū)別及用法的相關(guān)資料,需要的朋友可以參考下2017-03-03linux配置jdk環(huán)境變量簡(jiǎn)單教程
這篇文章主要為大家詳細(xì)介紹了linux配置jdk環(huán)境變量簡(jiǎn)單教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01