1.JdbcTemplate的开发步骤
<1>导入spring-jdbc和spring-tx坐标
<2>创建数据库表和实体
<3>创建JdbcTemplate对象
<4>执行数据库
2.JdbcTemplate快速入门
<1>导入坐标
<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>
<2>创建实体类和表
package com.lin.domain;
public class Account {
private String name;
private double money;
public Account() {
}
public Account(String name, double money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public double getMoney() {
return money;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
<3>创建jdbcTemplate对象
<4>执行数据库操作
@Test
//测试JdbcTemplate开发步骤
public void test1() throws PropertyVetoException {
// 创建数据源对象
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("123456");
JdbcTemplate jdbcTemplate=new JdbcTemplate();
//设置数据源对象,知道数据库在哪
jdbcTemplate.setDataSource(dataSource);
//执行操作
int row = jdbcTemplate.update("insert into account value (?,?)", "wu", 12);
System.out.println(row);
}
3.spring 产生jdbcTemplate对象
可以将数据源DateSource的创建权交给Spring,在Spring容器内部将数据源注入到模板中
spring容器加载外部properties文件时必须添加context命名空间
<!--加载properties文件 添加context命名空间-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源对象-->
<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>
4.crud操作
package com.lin.test;
import com.lin.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 CrudTest {
@Autowired
// 测试谁注入谁
private JdbcTemplate jdbcTemplate;
@Test
public void testUpdate(){
int row = jdbcTemplate.update("update account set money=? where name=?", 100000, "ws");
System.out.println(row);
}
@Test
public void testDelete(){
int row = jdbcTemplate.update("delete from account where name=?", "ws");
System.out.println(row);
}
//查询所有
@Test
public void testQueryAll(){
List<Account> list = jdbcTemplate.query("select *from account", new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(list);
}
@Test
//单个查询时返回一个对象
public void testQueryOne(){
Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "wu");
System.out.println(account);
}
//聚合查询
@Test
public void testQueryCount(){
Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
}
}
5.小节
① 导入spring-jdbc和spring-tx坐标
② 创建数据库表和实体
③ 创建JdbcTemplate对象
JdbcTemplate jdbcTemplate =
new
JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
④ 执行数据库操作
更新操作:
jdbcTemplate.update (sql,params)
查询操作:
jdbcTemplate.query (sql,Mapper,params)
jdbcTemplate.queryForObject(sql,Mapper,params)