JdbcTemplate的基本用法:
一个简单的示例:
pom.xml
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
JdbcTemplateDemo01:
package net.togogo.jdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
/**
* JdbcTemplate的最基本用法
*/
public class JdbcTemplateDemo01 {
public static void main(String[] args) {
//准备数据源,spring的内置数据源
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/eesy");
ds.setUsername("root");
ds.setPassword("123456");
//1.创建JdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//给jdbcTemplate设置数据源
jdbcTemplate.setDataSource(ds);
//2.执行操作
jdbcTemplate.execute("insert into account1(username,money)values('ccc',1000)");
}
}
Spring基本xml的JdbcTemplate
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/eesy"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>
JdbcTemplateDemo02:
package net.togogo.jdbcTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
/**
* JdbcTemplate的最基本用法
*/
public class JdbcTemplateDemo02 {
public static void main(String[] args) {
//1.获取容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate",JdbcTemplate.class);
//3.执行操作
jdbcTemplate.execute("insert into account1(username,money)values('ddd',1000)");
}
}
JdbcTemplate的CRUD操作
package net.togogo.jdbcTemplate;
import net.togogo.bean.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
* JdbcTemplate的CRUD操作
*/
public class JdbcTemplateDemo03 {
public static void main(String[] args) {
//1.获取容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate",JdbcTemplate.class);
//3.执行操作
//保存
// jdbcTemplate.update("insert into account1(username,money)values(?,?)","eee",1000f);
//更新
// jdbcTemplate.update("update account1 set username=?,money=? where id=?","test",1234f,5);
//删除
// jdbcTemplate.update("delete from account1 where id=?",5);
//查询所有
// List<Account> accountList = jdbcTemplate.query("select * from account1 where money > ?", new BeanPropertyRowMapper<Account>(Account.class),1000f);
// for (Account account : accountList) {
// System.out.println(account);
// }
//查询一个
// List<Account> accountList = jdbcTemplate.query("select * from account1 where id = ?", new BeanPropertyRowMapper<Account>(Account.class), 1);
// for (Account account : accountList) {
// System.out.println(account);
// }
//查询返回一行一列(使用聚合函数,但不包括group by语句)
Integer integer = jdbcTemplate.queryForObject("select count(*) from account1 where money > ?", Integer.class, 1000f);
System.out.println(integer);
}
}
实际开发中我们写在dao层是实现类中:
package net.togogo.dao.impl;
import net.togogo.bean.Account;
import net.togogo.dao.IAccountDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl implements IAccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public Account findAccountById(Integer id) {
List<Account> accountList = jdbcTemplate.query("select * from account1 where id = ?", new BeanPropertyRowMapper<Account>(Account.class), id);
return accountList.isEmpty()?null:accountList.get(0);
}
public Account findAccountByName(String name) {
List<Account> accountList = jdbcTemplate.query("select * from account1 where username = ?", new BeanPropertyRowMapper<Account>(Account.class), name);
if(accountList.isEmpty()){
return null;
}
if(accountList.size()>1){
throw new RuntimeException("结果集不唯一");
}
return accountList.get(0);
}
public void updateAccount(Account account) {
jdbcTemplate.update("update account1 set username=?,money=? where id=?",account.getUserName(),account.getMoney(),account.getId());
}
}
假如项目中有许多dao层实现类,他们都需要一个JdbcTemplate对象,那么这些dao层实现类的代码会有许多重复,而这时候这些dao层实现类可以继承JdbcDaoSupport类来减少重复代码。
不过该方法只适用于xml配置。
package net.togogo.dao.impl;
import net.togogo.bean.Account;
import net.togogo.dao.IAccountDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl2 extends JdbcDaoSupport implements IAccountDao {
public Account findAccountById(Integer id) {
List<Account> accountList = super.getJdbcTemplate().query("select * from account1 where id = ?", new BeanPropertyRowMapper<Account>(Account.class), id);
return accountList.isEmpty()?null:accountList.get(0);
}
public Account findAccountByName(String name) {
List<Account> accountList = super.getJdbcTemplate().query("select * from account1 where username = ?", new BeanPropertyRowMapper<Account>(Account.class), name);
if(accountList.isEmpty()){
return null;
}
if(accountList.size()>1){
throw new RuntimeException("结果集不唯一");
}
return accountList.get(0);
}
public void updateAccount(Account account) {
super.getJdbcTemplate().update("update account1 set username=?,money=? where id=?",account.getUserName(),account.getMoney(),account.getId());
}
}