1. JdbcTemplate是什么?
JdbcTemplate是spring框架中提供的一个模板对象,是对原始繁琐的Jdbc API对象的简单封装.
核心对象:
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSource dataSource);
核心方法:
int update(); 执行增、删、改语句
List<T> query(); 查询多个
T queryForObject(); 查询一个
new BeanPropertyRowMapper<>(); 实现ORM映射封装
2. Spring整合JdbcTemplate
1) 创建java项目,导入坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lagou</groupId>
<artifactId>spring_jdbctemplate</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
</dependencies>
</project>
2)编写实体类
package com.lagou.domain;
public class Account {
private Integer id;
private String name;
private Double money;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}
3)编写AccountDao接口和实现类
package com.lagou.dao;
import com.lagou.domain.Account;
import java.util.List;
public interface AccountDao {
/*
查询所有账户
*/
public List<Account> findAll();
/*
根据ID查询账户
*/
public Account findById(Integer id);
/*
添加账户
*/
public void save(Account account);
/*
更新账户信息
*/
public void update(Account account);
/*
根据ID删除账户
*/
public void delete(Integer id);
}
package com.lagou.dao.impl;
import com.lagou.dao.AccountDao;
import com.lagou.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/*
查询所有账户
*/
public List<Account> findAll() {
// 需要用到jdbcTemplate
String sql = "select * from account";
List<Account> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
return list;
}
/*
根据ID查询账户
*/
public Account findById(Integer id) {
String sql = "select * from account where id = ?";
Account account = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), id);
return account;
}
/*
添加账户
*/
public void save(Account account) {
String sql = "insert into account values(null,?,?)";
jdbcTemplate.update(sql,account.getName(),account.getMoney());
}
/*
更新账户
*/
public void update(Account account) {
String sql = "update account set money = ? where name = ?";
jdbcTemplate.update(sql,account.getMoney(),account.getName());
}
/*
根据ID删除账户
*/
public void delete(Integer id) {
String sql = "delete from account where id = ?";
jdbcTemplate.update(sql,id);
}
}
(4)编写AccountService接口和实现类
package com.lagou.servlet.impl;
import com.lagou.dao.AccountDao;
import com.lagou.domain.Account;
import com.lagou.servlet.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountSerivceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public List<Account> findAll() {
List<Account> all = accountDao.findAll();
return all;
}
public Account findById(Integer id) {
Account account = accountDao.findById(id);
return account;
}
public void save(Account account) {
accountDao.save(account);
}
public void update(Account account) {
accountDao.update(account);
}
public void delete(Integer id) {
accountDao.delete(id);
}
}
(5)编写spring核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--IOC注解扫描-->
<context:component-scan base-package="com.lagou"/>
<!--引入properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--dataSource-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
</beans>
(6)测试类的编写
package com.lagou.test;
import com.lagou.domain.Account;
import com.lagou.servlet.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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 AccountServiceImplTest {
@Autowired
private AccountService accountService;
// 测试保存
@Test
public void testSave(){
Account account = new Account();
account.setName("lucy");
account.setMoney(1000d);
accountService.save(account);
}
// 测试查询所有
@Test
public void testFindAll(){
List<Account> all = accountService.findAll();
for (Account account : all) {
System.out.println(account);
}
}
//测试根据ID进行查询
@Test
public void testFindById(){
Account account = accountService.findById(1);
System.out.println(account);
}
// 测试账户修改
@Test
public void testUpdate(){
Account account = new Account();
account.setName("tom");
account.setMoney(1000d);
accountService.update(account);
}
// 测试根据ID删除账户
@Test
public void testDelete(){
accountService.delete(4);
}
}
节选自拉钩教育JAVA系列课程