先在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountDao" class="com.xp.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<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/maven"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
JdbcTemplate在Dao中的使用
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public Account findAccountById(Integer id) {
List<Account> accounts = jdbcTemplate.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class),id);
return accounts.isEmpty()?null:accounts.get(0);
}
public void updateAccount(Account account) {
jdbcTemplate.update("update account set name =? ,money = ? where id = ?",account.getName(),account.getMoney(),account.getId());
}
test类:
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountDao accountDao = ac.getBean("accountDao",AccountDao.class);
Account a1 = accountDao.findAccountById(2);
}
JdbcTemplate在spring的ioc中使用
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); JdbcTemplate jdbcTemplate = ac.getBean("jdbcTemplate",JdbcTemplate.class); jdbcTemplate.execute("insert into account(name,money) values ('ddd',222)"); }
JdbcTemplate的CRUD操作
dbutils靠ResultSetHandler的返回值决定query方法的返回值类型
spring靠query方法的参数列表决定返回值类型
编写测试方法:
test类:
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
方式一:
//查询,自己编写结果集处理类 不常用
// List<Account> list = jt.query("select * from account where money>?", new AccountRowMapper(), 500);
//自己编写的结果集处理类↓
class AccountRowMapper implements RowMapper<Account>{
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setName(resultSet.getString("name"));
account.setMoney(resultSet.getFloat("money"));
return account;
}
}
方式二:
//用spring提供的类似与ResultSetHandler的结果集处理类,常用
List<Account> list = jt.query("select * from account where money>?", new BeanPropertyRowMapper<Account>(Account.class), 500);
for (Account a : list){
System.out.println(a);
}
//查询一个↓
List<Account> list = jt.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), 1);
System.out.println(list.isEmpty()?"空":list.get(0));
//涉及聚集函数的查询
Long i =jt.queryForObject("select count(*) from account where money>?", Long.class, 500);
去除Dao层重复配置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" 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="jdbcTemplate" ref="jdbcTemplate"></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:3306/eesy"></property> <property name="username" value="root"></property> <property name="password" value="1234"></property> </bean> </beans>
/** * 账户的持久层实现类 */ @Repository public class AccountDaoImpl2 implements IAccountDao { @Autowired private JdbcTemplate jdbcTemplate; @Override 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); }
二。继承JdbcDaoSupport类采用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" 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/eesy"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>
当继承JdbcDaoSupport 后,JdbcDaoSupport会自动创建JdbcTemplate
/** * 账户的持久层实现类 */ public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao { @Override 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); }