作用:
用于和数据库交互的,实现对表大CRUD操作。
基于JdbcTemplate对象的创建以及其中的常用方法应用测试如下:
实体类:
public class Account implements Serializable {
private Integer id;
private String name;
private Float 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 Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
}
测试一:JdbcTemplate的基本用法
/*
* jdbctemplate的基本用法*/
public class JdbcTemplateDemo1 {
public static void main(String[] args) {
//准备数据源 spring内置数据源
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3307/spring");
ds.setUsername("root");
ds.setPassword("123");
//1.创建jdbc对象
JdbcTemplate jt = new JdbcTemplate();
//给jt设置数据源
jt.setDataSource(ds);
//2.执行操作
jt.execute("insert into Account(name,money)VALUES('aiyaya',122)");
}
}
测试二:使用配置文件
<?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
">
<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:3307/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
</beans>
public class JdbcTemplateDemo2 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
JdbcTemplate jt=(JdbcTemplate)applicationContext.getBean("jdbctemplate",JdbcTemplate.class);
//执行操作
jt.execute("delete from account where id=6");
}
}
测试三:jdbctemplate的CRUD操作
public class JdbcTemplateDemo3 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
JdbcTemplate jt=(JdbcTemplate)applicationContext.getBean("jdbctemplate",JdbcTemplate.class);
//执行操作
//保存
// jt.update("INSERT into Account(name,money)VALUES (?,?)","eee",342f);
//更新
//jt.update("UPDATE account SET name=?,money= ? WHERE id=?","茶茶",123f,3);
//删除
//查询所有 也可以用 new AccountRowMapper()
/* List<Account> accounts = jt.query("select * from account where money >?",new BeanPropertyRowMapper<Account>(Account.class),298f);
for(Account account:accounts){
System.out.println(account);
}*/
//查询一个
List<Account> accounts = jt.query("select * from account where id =?",new BeanPropertyRowMapper<Account>(Account.class),3);
System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0));
//查询返回一行一列(使用聚合函数,但是不加group by子句)
Long count =jt.queryForObject("select count(*) from account where money >?",Long.class,100f);
System.out.println(count);
}
}
//定义account的封装工具策略,
class AccountRowMapper implements RowMapper<Account>{
//含义:把结果集的数据封装到Account中,由spring把每个Account加到集合中去
@Override
public Account mapRow(ResultSet rs, int i) throws SQLException {
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getFloat("money"));
return account;
}
}
测试四:JdbcDaoSupport的使用及Dao的两种编写方法
JdbcDaoSupport用去除掉定义和那些重复的代码,当有多个Dao时不用重复的定义JdbcTemplate,
使用spring原生的JdbcDaoSupport时使用注解配置属性注入时会比较麻烦,所以如果采用注解配置,请使用2那个方式,如果使用xml请使用1的那种方式
之前:
public class AccountDaoImpl2 implements IAccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public Account findAccountById(int id) {
List<Account> accounts =jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),id);
return accounts.isEmpty()?null:accounts.get(0);
}
@Override
public Account findAccountByName(String name) {
List<Account> accounts =jdbcTemplate.query("select * from account where NAME = ?",new BeanPropertyRowMapper<Account>(Account.class),name);
if(accounts.isEmpty()){
return null;
}
if (accounts.size() >1){
throw new RuntimeException("结果不唯一");
}
return accounts.get(0);
}
@Override
public void updateAccount(Account account) {
jdbcTemplate.update("update account set name=?,money =? WHERE id=?",account.getName(),account.getMoney(),account.getId());
}
}
之后,这个是使用继承来实现的
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
@Override
public Account findAccountById(int id) {
List<Account> accounts =super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),id);
return accounts.isEmpty()?null:accounts.get(0);
}
@Override
public Account findAccountByName(String name) {
List<Account> accounts =super.getJdbcTemplate().query("select * from account where NAME = ?",new BeanPropertyRowMapper<Account>(Account.class),name);
if(accounts.isEmpty()){
return null;
}
if (accounts.size() >1){
throw new RuntimeException("结果不唯一");
}
return accounts.get(0);
}
@Override
public void updateAccount(Account account) {
super.getJdbcTemplate().update("update account set name=?,money =? WHERE id=?",account.getName(),account.getMoney(),account.getId());
}
}
手动实现JdbcDaoSupport
/*
* 此类用于抽取Dao的重复代码
public class JdbcDaoSupport {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setDataSource(DataSource dataSource) {
if(jdbcTemplate == null){
jdbcTemplate = createJdbctemplate(dataSource);
}
}
private JdbcTemplate createJdbctemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
*/
配置类的改动:
<?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
">
<!--配置账户的持久层-->
<bean id="accountDao" class="com.selan.dao.impl.AccountDaoImpl">
<!--<property name="jdbcTemplate" ref="jdbctemplate"></property>-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置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:3307/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
</beans>
spring的Jdbctemplate是根据不同的query方法来实现返回的。