Java Spring JdbcTemplate基本使用詳解
JdbcTemplate概述
它是spring框架中提供的一個對象,是對原始繁瑣的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快速入門
①導(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對象
④執(zhí)行數(shù)據(jù)庫操作
//測試JdbcTemplate開發(fā)步驟
public void test1() throws PropertyVetoException {
//創(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");
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//設(shè)置數(shù)據(jù)源對象 知道數(shù)據(jù)庫在哪
jdbcTemplate.setDataSource(dataSource);
//執(zhí)行操作
int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
System.out.println(row);
}
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模版對象中,配置如下:
<!--數(shù)據(jù)源對象-->
<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模板對象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
從容器中獲得JdbcTemplate進(jìn)行添加操作
//測試Spring產(chǎn)生jdbcTemplate對象
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot開發(fā)案例 分布式集群共享Session詳解
這篇文章主要介紹了SpringBoot開發(fā)案例 分布式集群共享Session詳解,在分布式系統(tǒng)中,為了提升系統(tǒng)性能,通常會對單體項(xiàng)目進(jìn)行拆分,分解成多個基于功能的微服務(wù),可能還會對單個微服務(wù)進(jìn)行水平擴(kuò)展,保證服務(wù)高可用,需要的朋友可以參考下2019-07-07
解決SpringMVC使用@RequestBody注解報(bào)400錯誤的問題
這篇文章主要介紹了解決SpringMVC使用@RequestBody注解報(bào)400錯誤的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

