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);
}
}