增删改查的操作
Account:
public class Account {
private Integer id;
private String name;
private double 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 double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + ", money=" + money + "]";
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置C3P0的连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_day03"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<!-- 配置JDBC的模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
测试:
/**
* 测试JDBC的模板类,使用IoC的方式
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1_1 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void run1() {
jdbcTemplate.update("insert into t_account values(null,?,?)", "小明",10000);
}
/**
* update(String sql,Object...params) : 可以完成增删改操作
*/
@Test
public void run2() {
jdbcTemplate.update("update t_account set name=? where id = ?", "熊大",3);
}
/**
* 删除测试
*/
@Test
public void run3() {
jdbcTemplate.update("delete from t_account where id=?", 4);
}
/**
* 测试查询: 通过主键查询一条记录
*/
@Test
public void run4() {
Account ac = jdbcTemplate.queryForObject("select * from t_account where id=?", new BeanMapper(), 1);
System.out.println(ac);
}
/**
* 查询所有的数据
*/
@Test
public void run5() {
List<Account> list = jdbcTemplate.query("select * from t_account", new BeanMapper());
System.out.println(list);
}
}
/**
* 自己手动的来封装数据(一行一行的封装数据)
* @author Administrator
*
*/
class BeanMapper implements RowMapper<Account>{
public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
Account ac = new Account();
ac.setId(rs.getInt("id"));
ac.setName(rs.getString("name"));
ac.setMoney(rs.getDouble("money"));
return ac;
}
}