Java Spring JdbcTemplate基本使用詳解
JdbcTemplate概述
它是spring框架中提供的一個(gè)對(duì)象,是對(duì)原始繁瑣的Jdbc API對(duì)象的簡(jiǎn)單封裝。spring框架為我們提供了很多的操作模板類。例如:操作關(guān)系型數(shù)據(jù)的JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫(kù)的RedisTemplate,操作消息隊(duì)列的JmsTemplate等等。
JdbcTemplate開發(fā)步驟
①導(dǎo)入spring-jdbc和spring-tx坐標(biāo)
②創(chuàng)建數(shù)據(jù)庫(kù)表和實(shí)體
③創(chuàng)建JdbcTemplate對(duì)象
④執(zhí)行數(shù)據(jù)庫(kù)操作
JdbcTemplate快速入門
①導(dǎo)入spring-jdbc和spring-tx坐標(biāo)
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.5.RELEASE</version> </dependency>
②創(chuàng)建accout表和Accout實(shí)體
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ù)庫(kù)操作
//測(cè)試JdbcTemplate開發(fā)步驟 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("root"); JdbcTemplate jdbcTemplate = new JdbcTemplate(); //設(shè)置數(shù)據(jù)源對(duì)象 知道數(shù)據(jù)庫(kù)在哪 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 name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--jdbc模板對(duì)象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>
從容器中獲得JdbcTemplate進(jìn)行添加操作
//測(cè)試Spring產(chǎn)生jdbcTemplate對(duì)象 public void test2() throws PropertyVetoException { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class); jdbcTemplate.update("insert into account values(?,?)", "lisi", 5000); System.out.println(row); }
JdbcTemplate的常用操作
修改操作、刪除和查詢?nèi)坎僮?/p>
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JdbcTemplateCRUDTest { @Autowired private JdbcTemplate jdbcTemplate; @Test public void testQueryCount(){ Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class); System.out.println(count); } @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 testQueryAll(){ List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class)); System.out.println(accountList); } @Test public void testUpdate(){ jdbcTemplate.update("update account set money=? where name=?",10000,"tom"); } @Test public void testDelete(){ jdbcTemplate.update("delete from account where name=?","tom"); } }
到此這篇關(guān)于Java Spring JdbcTemplate基本使用詳解的文章就介紹到這了,更多相關(guān)Java Spring JdbcTemplate內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java單鏈表的簡(jiǎn)單操作實(shí)現(xiàn)教程
這篇文章主要給大家介紹了關(guān)于Java單鏈表的簡(jiǎn)單操作實(shí)現(xiàn)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11SpringBoot開發(fā)案例 分布式集群共享Session詳解
這篇文章主要介紹了SpringBoot開發(fā)案例 分布式集群共享Session詳解,在分布式系統(tǒng)中,為了提升系統(tǒng)性能,通常會(huì)對(duì)單體項(xiàng)目進(jìn)行拆分,分解成多個(gè)基于功能的微服務(wù),可能還會(huì)對(duì)單個(gè)微服務(wù)進(jìn)行水平擴(kuò)展,保證服務(wù)高可用,需要的朋友可以參考下2019-07-07解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問(wèn)題
這篇文章主要介紹了解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09