文章目录
持久层总图概览
一、pom.xml导入坐标
<?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>spring</groupId>
<artifactId>spring10</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!--导入spring框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--导入spring内置数据源-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--和事务相关的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--导入mysql数据库连接桥-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
二、JdbcTemplate基本操作
1.使用XML配置数据源
Spring内置数据源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8"></property>
</bean>
配置数据库操作模板
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
2.JdbcTemplate基本操作之增删改查
BeanPropertyRowMapper:spring提供的RowMapper接口下的实现类,用于定义Account实体类的封装策略。该类把结果集封装到Account中,由spring把每个Account加到List集合中。
Query:该方法有众多参数及可选返回值,使用时要看我们要什么,我们有什么的原则来决定,例如:
当我们按某个标准查询一个List集合时,则需要sql语句、参数、一个用于封装的类和返回值(集合),此时,该方法满足以上所有需求,我们就采用这个方法。
public class JdbcTemplateDemo {
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
//3.执行操作
//保存
jt.update("insert into account(name,money)values(?,?)","eee",3333f);
//更新
jt.update("update account set name=?,money=? where id=?","test",4567,7);
//删除
jt.update("delete from account where id=?",8);
//查询所有
List<Account> accounts = jt.query("select * from account where money > ?",new BeanPropertyRowMapper<Account>(Account.class),1000f);
for(Account account : accounts){
System.out.println(account);
}
//查询一个(开发中常用)
List<Account> accounts = jt.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),1);
System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0));
//查询返回一行一列(使用聚合函数,但不加group by子句)
Long count = jt.queryForObject("select count(*) from account where money > ?",Long.class,1000f);
System.out.println(count);
}
}
三、在dao层使用JdbcTemplate
1.准备实体类
package com.lry.domain;
import java.io.Serializable;
public class Account implements Serializable {
private int aid;
private String name;
private Float money;
@Override
public String toString() {
return "Account{" +
"aid=" + aid +
", name='" + name + '\'' +
", money=" + money +
'}';
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
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;
}
}
2.准备dao接口和实体类
IAccountDao接口
package com.lry.dao;
import com.lry.domain.Account;
import java.util.List;
public interface IAccountDao {
//查询所有
List<Account> findAll();
//查询一个
Account findById(int id);
//保存账户
void saveAccount(Account account);
//更新账户
void updateAccount(Account account);
//删除账户
void delete(int id);
}
AccountDaoImpl实现类
JdbcSupport:我们采用Spring为我们提供的类抽取用于注入JdbcTemplate的代码,当我们需要时,继承该类就可以直接使用该类为我们创建的JdbcTemplate对象。
注意:当使用JdbcDaoSupport工具类时,只能基于xml方式,无法使用注解,因为你无法在字节码文件中增添注解!当采用注解方式时,则需要手动注入该类。
JdbcDaoSupport源码展示:
在XML配置中注入AccountDao实体类:
<bean id="accountDao" class="com.lry.dao.Impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
package com.lry.dao.Impl;
import com.lry.dao.IAccountDao;
import com.lry.domain.Account;
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 AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
//查询所有
public List<Account> findAll() {
List<Account> accounts=super.getJdbcTemplate().query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
return accounts;
}
//查询一个
public Account findById(int id) {
List<Account> accounts=getJdbcTemplate().query("select * from account where aid = ?", new BeanPropertyRowMapper<Account>(Account.class), id);
return accounts.get(0);
}
//保存账户
public void saveAccount(Account account) {
getJdbcTemplate().update("insert into account(name, money) values (?, ?)", account.getName(), account.getMoney());
}
//更新账户
public void updateAccount(Account account) {
getJdbcTemplate().update("update account set money=? where aid=?", account.getMoney(), account.getAid());
}
//删除账户
public void delete(int id) {
getJdbcTemplate().update("delete from account where aid=?", id);
}
}
四、测试
public class TemplateTest {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
IAccountDao dao=(IAccountDao) ac.getBean("accountDao");
//查询所有
List<Account> accounts=dao.findAll();
for (Account account:accounts){
System.out.println(account);
}
}
}