一、概述
JdbcTemplate用在Dao也是有两种方法
- 第一种:使用xml配置文件
- 第二种:使用注解
案例的文件结构
需要的依赖
<dependencies>
<!-- spring的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!-- 支持jdbc的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!-- 支持事务的依赖-->
<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>
基础代码
IAccountDao
public interface IAccountDao {
/**
* 根据id查询用户
*/
Account findAccountById(Integer accountId);
/**
* 根据名称
*/
Account findAccountByName(String name);
/**
* 更新
*/
void updateAccount(Account account);
}
Account
public class Account implements Serializable {
private Integer id;
private String name;
private Float 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;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
第一种: 使用xml配置文件
配置文件bean.xml,记得将url 密码 用户名改为自己的哦
<?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.itheima.dao.impl.AccountDaoImpl">
<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/*******"></property>
<property name="username" value="****"></property>
<property name="password" value="****"></property>
</bean>
</beans>
AccountDaoImpl
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
/**
* 根据id查询用户
*/
public Account findAccountById(Integer accountId){
List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
return accounts.isEmpty()?null:accounts.get(0);
}
/**
* 根据名称
*/
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);
}
/**
* 更新
*/
public void updateAccount(Account account){
super.getJdbcTemplate().update("update account set name=?,money=? where id =?",account.getName(),account.getMoney(),account.getId());
}
}
知识点:
这里我们AccountDaoImpl中并没有以下代码
private JdbcTemplate jdbcTemplate;
@Override
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
而且在bean中我们也没有注入JdbcTemplate。这是因为我们在持久层实现类中我们继承了一个父类JdbcDaoSupport。
继承该类的原因:
当我们开发中出现多个dao的文件时,每个*****DaoImpl文件中都会有以上的代码,这就会造成代码的重复。因此spring框架为我们编写了JdbcDaoSupport来解决这个问题。减少了代码的重复出现。
第二种:使用注解
在bean.xml中引入约束,加入要扫描的包,并且把jdbcTemplate进行依赖注入,更改地址池为自己数据库的信息
代码:
<?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">
<!--配置要扫描的包-->
<context:component-scan base-package="com.itheima.dao"></context:component-scan>
<!--配置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/*****"></property>
<property name="username" value="***"></property>
<property name="password" value="****"></property>
</bean>
</beans>
AccountdaoImpl
import java.util.List;
@Repository("accountDao")
public class AccountDaoImpl2 implements IAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 根据id查询用户
*/
public Account findAccountById(Integer accountId){
List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
return accounts.isEmpty()?null:accounts.get(0);
}
/**
* 根据名称
*/
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);
}
/**
* 更新
*/
public void updateAccount(Account account){
jdbcTemplate.update("update account set name=?,money=? where id =?",account.getName(),account.getMoney(),account.getId());
}
}
两者的区别
使用注解的话可以根据自己的需要更改jdcbTemplate,
但是使用的xml因为是继承了父类所以不能更改jdbcTemplate他是只读属性的