一 jabcTemplate概述
(一)jdbcTemplate作用
(二)创建jdbcTemplate对象
<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="kwantler"></property>
</bean>
</beans>
/**
* JdbcTemplate的最基本用法
*/
public class JdbcTemplateDemo01 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
JdbcTemplate jt = ac.getBean("jdbcTemplate", JdbcTemplate.class);
//jt.execute("insert into account (name, money) VALUES ('eee', 100)");
// 添加
//jt.update("insert into account (name, money) VALUES (?, ?)","fff", 2000);
// 更新
//jt.update("update account set name=?, money=? where id=?","aaa", 2000, 6);
// 删除
//jt.update("delete FROM account where id=?",6);
// 查询所有
/*List<Account> accounts = jt.query("select * from account where money>?", new BeanPropertyRowMapper<Account>(Account.class),1000f);
for (Account acc: accounts) {
System.out.println(acc);
}*/
// 查询一个
/*List<Account> accounts = jt.query("select * from account where money>?", new BeanPropertyRowMapper<Account>(Account.class),500f);
System.out.println(accounts.get(0));*/
// 查询返回一行一列
Integer count = jt.queryForObject("select count(*) from account where money>?",Integer.class,5000f);
}
}
/*
class AccountRowMapper implements RowMapper<Account>{
@Override
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;
}
}*/
- 方式2
<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.lc.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:3306/eesy"></property>
<property name="username" value="root"></property>
<property name="password" value="kwantler"></property>
</bean>
</beans>
继承JdbcDaoSupport
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public List<Account> findAll() {
try {
return super.getJdbcTemplate().query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
} catch (Exception e) {
throw new RuntimeException("列表查询");
}
}
}