JdbcTemplate
实体类:
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
//省略set方法
}
不使用ioc
public class JdbcTemplateDemo {
public static void main(String[] args) {
//准备数据源:Spring内置数据源
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost/test");
ds.setUsername("root");
ds.setPassword("1022");
//创建JdbcTemplate对象
JdbcTemplate jt = new JdbcTemplate();
//给JdbcTemplate对象设置数据源
jt.setDataSource(ds);
//执行操作
jt.execute("insert into account1(name,money)values('eee',998.0)");
}
}
执行之后可以在数据库中插入一条数据。
使用ioc实现增加一条内容
public class JdbcTemplateDemo {
public static void main(String[] args) {
//获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取对象
JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
//执行操作
jt.execute("insert into account1(name,money)values('fff',1200.0)");
}
}
配置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">
<!--配置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/test"></property>
<property name="username" value="root"></property>
<property name="password" value="1022"></property>
</bean>
</beans>
实现删改查:
public class JdbcTemplateDemo2 {
public static void main(String[] args) {
//获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取对象
JdbcTemplate jt = ac.getBean("jdbcTemplate", JdbcTemplate.class);
//执行操作
// 保存
jt.update("insert into account1(name,money)values(?,?)","ggg",1100.0);
//执行保存
jt.update("update account1 set name = ?,money=? where id=?","test",4567,4);
//删除
jt.update("delete from account1 where id=?",7);
//查询所有金额大于1000的
List<Account> accounts = jt.query("select * from account1 where money > ?", new BeanPropertyRowMapper<Account>(Account.class), 1000.0);
for (Account account : accounts) {
System.out.println(account);
}
//查询一个
List<Account> account = jt.query("select * from account1 where id = ?",new BeanPropertyRowMapper<Account>(Account.class), 2);
System.out.println(account.isEmpty() ? "没有结果" : account);
}
}
增加Dao层对方法进行封装:
public class AccountDaoImpl implements IAccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public Account findAccountById(int id) {
List<Account> accounts = jdbcTemplate.query("select * from account1 where id = ?",new BeanPropertyRowMapper<Account>(Account.class),id);
if(accounts.isEmpty()) return null;
else return accounts.get(0);
}
public Account findAccountByName(String name) {
List<Account> accounts = jdbcTemplate.query("select * from account1 where name = ?",new BeanPropertyRowMapper<Account>(Account.class),name);
if(accounts.isEmpty()) return null;
if(accounts.size()>1){
throw new RuntimeException("查询到的结果不止一个");
}else return accounts.get(0);
}
public void updateAccount(Account account) {
jdbcTemplate.update("insert into account1(name,money)values(?,?)","hhh",180.0);
}
}
在bean.xml中注入参数:
<!--配置账户的持久层-->
<bean id="accountDao" class="com.lwl.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
Dao层的第二种写法:
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
private JdbcTemplate jdbcTemplate = super.getJdbcTemplate();
//后面的不变
public Account findAccountById(int id) {
List<Account> accounts = jdbcTemplate.query("select * from account1 where id = ?",new BeanPropertyRowMapper<Account>(Account.class),id);
if(accounts.isEmpty()) return null;
else return accounts.get(0);
}
public Account findAccountByName(String name) {
List<Account> accounts = jdbcTemplate.query("select * from account1 where name = ?",new BeanPropertyRowMapper<Account>(Account.class),name);
if(accounts.isEmpty()) return null;
if(accounts.size()>1){
throw new RuntimeException("查询到的结果不止一个");
}else return accounts.get(0);
}
public void updateAccount(Account account) {
jdbcTemplate.update("insert into account1(name,money)values(?,?)","hhh",180.0);
}
}