1、JdbcTemplate简介
它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。
步骤:
①导入spring-jdbc和spring-tx坐标
②创建数据库表和实体
③创建JdbcTemplate对象
④执行数据库操作
2、入门
package com.spring.controller;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import java.beans.PropertyVetoException;
public class JdbcController {
public static void main(String[] args) throws PropertyVetoException {
//创建数据源对象
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/my");
cpds.setUser("root");
cpds.setPassword("mysql");
JdbcTemplate jt = new JdbcTemplate();
//设置数据源,知道数据库在哪里
jt.setDataSource(cpds);
int n = jt.update("insert into account (`name`,`money`) values(?,?)","dd",1000);
System.out.println(n);
}
}
3、Spring产生jdbc
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/my"></property>
<property name="user" value="root"></property>
<property name="password" value="mysql"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
package com.spring.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcController2 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jt = ac.getBean(JdbcTemplate.class);
int n = jt.update("insert into account (`name`,`money`) values(?,?)","dd",1000);
System.out.println(n);
}
}
抽取jdbc
4、CRUD操作
package com.spring.test;
import com.spring.domain.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void update(){
jdbcTemplate.update("update account set name = ? where id = ?","ee",11);
}
@Test
public void delete(){
jdbcTemplate.update("delete from account where id = ?",12);
}
//查询全部数据
@Test
public void select1(){
List<Account> list = jdbcTemplate.query("select * from account",
new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(list);
}
//查询单个数据
@Test
public void select2(){
Account account = jdbcTemplate.queryForObject("select * from account where id = ?",
new BeanPropertyRowMapper<Account>(Account.class), 3);
System.out.println(account);
}
//查询一个数据
@Test
public void select3(){
Long l = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(l);
}
}