我们先来看一般的JdbcTemplate。
package com.qst.jdbctemplate; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; public class jdbcTemplateDemo { public static void main(String[] args) { DriverManagerDataSource dm = new DriverManagerDataSource(); dm.setDriverClassName("com.mysql.jdbc.Driver"); dm.setUrl("jdbc:mysql://localhost:3306/spring"); dm.setUsername("root"); dm.setPassword("root"); JdbcTemplate jt = new JdbcTemplate(); jt.setDataSource(dm); jt.execute("insert into account(name,money)value ('bbb',1000)"); } }先来执行一下插入方法。发现非常简单,但是不太符合我们spring的常规操作,那我们就把jdbctemplate相关对象注入到ioc中
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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dateSouce"></property> </bean> <bean id="dateSouce" 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="root"></property> </bean> </beans>jdbctemplateDemo.java
public class jdbcTemplateDemo { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); JdbcTemplate jt = (JdbcTemplate)ac.getBean("jdbcTemplate"); jt.execute("insert into account(name,money)value ('bbb',1000)"); } }在真正编写代码时我们都是通过Dao层与数据库进行交互,再来看一下代码
accountDao.java
package com.qst.dao.impl; import com.qst.dao.IAccountDao; import com.qst.domain.Account; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class accountDao implements IAccountDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public Account findById(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); } //姓名查找和id查找的区别 @Override public Account findByName(String name) { List<Account> accounts = jdbcTemplate.query("select * from account where id =?", new BeanPropertyRowMapper<Account>(Account.class), name); if (accounts.isEmpty()) { return null; } else if (accounts.size() > 1) { throw new RuntimeException("结果集不唯一"); } return accounts.get(0); } @Override public void updateAccount(Account account) { jdbcTemplate.update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId()); } }bean.xml
<bean id="accountDao" class="com.qst.dao.impl.accountDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean>进行实现
public class jdbcTemplateDemo { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountDao act =(IAccountDao)ac.getBean("accountDao"); accountDao a =new accountDao(); Account account = a.findById(1); System.out.println(account); } }