1.项目搭建
<1>数据库表account对应的账户实体类
package domain; import java.io.Serializable; /** * 账户实体类 */ 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 + '}'; } }
<2>账户的持久层接口及其实现类
接口类 IAccountDao
package dao; import domain.Account; /** * 账户的持久层接口 */ public interface IAccountDao { /** * 根据Id查询账户 * @param accountId * @return */ Account findAccountById(Integer accountId); /** * 根据名称查询账户 * @param accountName * @return */ Account findAccountByName(String accountName); /** * 更新账户 * @param account */ void updateAccount(Account account); }
AccountDaoImpl.java
package dao.impl; import dao.IAccountDao; import domain.Account; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.util.List; /** * 账户的持久层实现类 */ public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao { 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 accountName) { List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName); 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()); } }
2.JdbcTemplate的使用
<1>JdbcTemplate最基本用法
package jdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; /** * JdbcTemplate最基本用法 */ public class JdbcTemplateTest01 { public static void main(String[] args) { //1.准备数据源,Spring的内置数据源 DriverManagerDataSource ds=new DriverManagerDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3306/spring"); ds.setUsername("root"); ds.setPassword("plj824"); //2.创建jdbcTemplate对象 JdbcTemplate jdbcTemplate=new JdbcTemplate(); //3.给jdbcTemplate对象设置数据源 jdbcTemplate.setDataSource(ds); //2.执行操作 jdbcTemplate.execute("insert into account(name,money)values ('ccc',1000)"); } }
<2>JdbcTemplate 用IOC进行依赖注入
package jdbcTemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; /** * JdbcTemplate 用IOC进行依赖注入 */ public class JdbcTemplateTest02 { public static void main(String[] args) { //1.获取容器 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml"); //2.获取对象 JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); //3.执行操作 jdbcTemplate.execute("insert into account(name,money)values ('eee',1250)"); } }
<3>JdbcTemplate的CRUD操作
package jdbcTemplate; import domain.Account; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; /** * JdbcTemplate的CRUD操作 */ public class JdbcTemplateTest03 { public static void main(String[] args) { //1.获取容器 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml"); //2.获取对象 JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); //3.执行操作 //3.1保存 //jdbcTemplate.update("insert into account(name,money)values ('eee',1250)"); //3.2 更新 //jdbcTemplate.update("update account set name=?,money=? where id=?","test",4567,6); //3.3 删除 //jdbcTemplate.update("delete from account where id=? ",4); //3.4 查询所有 // List<Account> accounts = jdbcTemplate.query("select * from account where money > ?", new AccountRowMapper(), 1000f); // for (Account account : accounts) { // System.out.println(account); // } // //查询一个 // List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),1); // System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0)); //查询返回一行一列(使用聚合函数,但不加group by子句) Long count = jdbcTemplate.queryForObject("select count(*) from account where money > ?",Long.class,1000f); System.out.println(count); } } /** * 定义Account的封装策略 */ class AccountRowMapper implements RowMapper<Account> { /** * 把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中 * * @param rs * @param rowNum * @return * @throws SQLException */ public Account mapRow(ResultSet rs, int rowNum) throws SQLException { Account account = new Account(); account.setId(rs.getInt("id")); account.setName(rs.getString("name")); account.setMoney(rs.getFloat("money")); return account; } }
<4>bean.xml
<?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="dao.impl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置jdbcTemplate,注意:JdbcTemplate这个类是在jar包中,无法使用setter方法进行注入,只能用xml配置--> <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/spring"></property> <property name="username" value="root"></property> <property name="password" value="plj824"></property> </bean> </beans>