Java?超詳細(xì)講解核心類Spring?JdbcTemplate
JdbcTemplate概述
它是spring框架中提供的一個(gè)對象,是對原始繁瑣的Jdbc API對象的簡單封裝。spring框架為我們提供了很多的操作 模板類。例如:操作關(guān)系型數(shù)據(jù)的JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫的RedisTemplate,操 作消息隊(duì)列的JmsTemplate等等。
JdbcTemplate開發(fā)步驟
① 導(dǎo)入spring-jdbc和spring-tx坐標(biāo)
② 創(chuàng)建數(shù)據(jù)庫表和實(shí)體
③ 創(chuàng)建JdbcTemplate對象
④ 執(zhí)行數(shù)據(jù)庫操作
JdbcTemplate快速入門
① 在pom.xml文件中導(dǎo)入spring-jdbc和spring-tx坐標(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)建數(shù)據(jù)庫表和實(shí)體
表名稱 | account |
name | varchar(20) |
money | varchar(20) |
?
public class Account { private String name; private double money; //為方便展示省略get和set方法,具體開發(fā)中不能省。 }
③ 創(chuàng)建JdbcTemplate對象,執(zhí)行數(shù)據(jù)庫操作
//1、創(chuàng)建數(shù)據(jù)源對象 ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUser("root"); dataSource.setPassword("root"); //2、創(chuàng)建JdbcTemplate對象 JdbcTemplate jdbcTemplate = new JdbcTemplate(); //3、設(shè)置數(shù)據(jù)源給JdbcTemplate jdbcTemplate.setDataSource(dataSource); //4、執(zhí)行操作 jdbcTemplate.update("insert into account values(?,?)","tom",5000);
Spring產(chǎn)生JdbcTemplate對象
我們可以將JdbcTemplate的創(chuàng)建權(quán)交給Spring,將數(shù)據(jù)源DataSource的創(chuàng)建權(quán)也交給Spring,在Spring容器內(nèi)部將數(shù)據(jù)源DataSource注入到JdbcTemplate模版對象中,applicationContext.xml配置如下:
<!--數(shù)據(jù)源DataSource--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!--JdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean>
從容器中獲得JdbcTemplate進(jìn)行添加操作。
@Test public void testSpringJdbcTemplate() throws PropertyVetoException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class); jdbcTemplate.update("insert into account values(?,?)","lucy",5000); }
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=?",1000,"tom"); } }
刪除和查詢?nèi)坎僮?/h3>
@Test
public void testDelete(){
jdbcTemplate.update("delete from account where name=?","tom");
}
@Test
public void testQueryAll(){
List<Account> accounts = jdbcTemplate.query("select * from account", new
BeanPropertyRowMapper<Account>(Account.class));
for (Account account : accounts) {
System.out.println(account.getName());
}
}
@Test public void testDelete(){ jdbcTemplate.update("delete from account where name=?","tom"); } @Test public void testQueryAll(){ List<Account> accounts = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class)); for (Account account : accounts) { System.out.println(account.getName()); } }
查詢單個(gè)數(shù)據(jù)操作
@Test //測試查詢單個(gè)對象操作 public void testQueryOne(){ Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom"); System.out.println(account.getName()); } @Test //測試查詢單個(gè)簡單數(shù)據(jù)操作(聚合查詢) public void testQueryCount(){ Long aLong = jdbcTemplate.queryForObject("select count(*) from account", Long.class); System.out.println(aLong); }
本章小結(jié)
Spring JdbcTemplate開發(fā)步驟:
① 導(dǎo)入spring-jdbc和spring-tx坐標(biāo)
② 創(chuàng)建數(shù)據(jù)庫表和實(shí)體
③ 創(chuàng)建JdbcTemplate對象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
④ 執(zhí)行數(shù)據(jù)庫操作
更新操作:
dbcTemplate.update(sql,params)
查詢操作:
jdbcTemplate.query(sql,Mapper,params)
jdbcTemplate.queryForObject(sql,Mapper,params)
到此這篇關(guān)于Java 超詳細(xì)講解核心類Spring JdbcTemplate的文章就介紹到這了,更多相關(guān)Java Spring JdbcTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python smtplib模塊自動(dòng)收發(fā)郵件功能(二)
這篇文章主要為大家詳細(xì)介紹了python smtplib模塊自動(dòng)收發(fā)郵件功能的第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Python&Matlab實(shí)現(xiàn)櫻花的繪制
正值櫻花飄落的季節(jié),本文將利用Python和Matlab分別繪制一顆櫻花樹,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起動(dòng)手嘗試一下2022-04-04基于Python實(shí)現(xiàn)西西成語接龍小助手
成語接龍是中華民族傳統(tǒng)的文字游戲。本文將用Python制作一個(gè)簡單的成語接龍小程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-08-08python實(shí)現(xiàn)簡易自習(xí)室座位預(yù)約系統(tǒng)
本文將結(jié)合實(shí)例代碼,介紹python實(shí)現(xiàn)簡易自習(xí)室座位預(yù)約系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06pygame實(shí)現(xiàn)俄羅斯方塊游戲(基礎(chǔ)篇1)
這篇文章主要為大家介紹了pygame實(shí)現(xiàn)俄羅斯方塊游戲基礎(chǔ)的第1篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法
這篇文章主要介紹了python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法,實(shí)例分析了Python使用MySQLdb模塊操作mysql數(shù)據(jù)庫的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08python?配置uwsgi?啟動(dòng)Django框架的詳細(xì)教程
這篇文章主要介紹了python?配置uwsgi?啟動(dòng)Django框架,本文給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12