當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring-JdbcTemplate基本使用
生活随笔
收集整理的這篇文章主要介紹了
Spring-JdbcTemplate基本使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
概述:它是spring提供的一個對象,是對原始繁瑣的Jdbc API對象的簡單封裝,spring框架為我們提供了很多的操作模板類。例如操作關系型數(shù)據(jù)庫JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫RedisTemplate,操作消息隊列的JmsTemplate等等
1.導入spring-jdbc和spring-tx坐標(因為里面封裝的是jdbc模板對象)
<dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.9.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.9.RELEASE</version></dependency>2.創(chuàng)建數(shù)據(jù)庫表和實體類(封裝查詢的數(shù)據(jù)或者
3.創(chuàng)建JdbcTemplate對象
public class JdbcTemplateTest {@Test//測試JdbcTemplate開發(fā)步驟public void test1() throws PropertyVetoException {//創(chuàng)建數(shù)據(jù)源對象ComboPooledDataSource dataSource=new ComboPooledDataSource();dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC");dataSource.setUser("root");dataSource.setPassword("hao20001010");//創(chuàng)建模板對象JdbcTemplate jdbcTemplate=new JdbcTemplate();//設置數(shù)據(jù)源,知道數(shù)據(jù)庫在哪jdbcTemplate.setDataSource(dataSource);//執(zhí)行操作jdbcTemplate.update("insert into account values(?,?)","jane",500);} }4.執(zhí)行數(shù)據(jù)庫操作
總結
以上是生活随笔為你收集整理的Spring-JdbcTemplate基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-aop注解开发(切点表达式
- 下一篇: Spring-JdbcTemplate(