通过框架学习(一)3.4节,JDBC完整模板完成增删改查操作详细步骤【==总结】
===========Spring JDBC模板的使用
1.引入必要的Jar包
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0DNXlFdoNDWy50VZBjTzwEMW1mY1RzRapnTtxkb5ckYplTeMZTTINGMShUYfRHelRHLwEzX39GZhh2css2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xyayFWbyVGdhd3LcV2Zh1Wa9M3clN2byBXLzN3btg3Pn5GcuMTNyUDNwcTM3ITMxgTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2.抽取连接配置文件jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_1
jdbc.username=root
jdbc.password=123456
3.配置xml配置文件applicationContext.xml
<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">
<!-- 1.引入配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.引入相应的值 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/> //===========jdbc.properties里的键
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 3.配置Spring的JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"></property>
</bean>
</beans>
4.代码完成数据库的增删改查
/**
* @author Leonard
* JDBC完成增删改查
*/
@RunWith(SpringJUnit4ClassRunner.class)//[email protected]用于测试
@ContextConfiguration("classpath:applicationContext.xml")//=======加载xml配置文件
public class spring_2 {
@Resource(name="jdbcTemplate")//===========引入jdbc模板
private JdbcTemplate jdbctemplate;
/*==========增删改查操作=================*/
@Test
public void insert(){
jdbctemplate.update("insert into account values(null,?,?)", "张三",230000d);
}
@Test
public void delete(){
jdbctemplate.update("delete from account where id = ?", 1);
}
@Test
public void update(){
jdbctemplate.update("update account set name = ? , money=? where id = ?", "李四",240000d,7);
}
/*================查询操作================*/
//1.查询一个数据
@Test
public void select(){
String name = jdbctemplate.queryForObject("select name from account where id=?", String.class, 5);
System.out.println(name);
}
//2.统计个数
@Test
public void count(){
Long count = jdbctemplate.queryForObject("select count(*) from account", Long.class);
System.out.println("个数======="+count);
}
//3.将查询的数据封装到一个对象Account==创建domain层,set,get,重写toString
@Test
public void obj(){
Account account = jdbctemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 5);
System.out.println("单条数据========"+account);
}
//4.查询多条数据封装到对象
@Test
public void objs(){
List<Account> list = jdbctemplate.query("select * from account", new MyRowMapper());
System.out.println("集合列表======="+list);
}
/*=========数据封装=========================*/
class MyRowMapper implements RowMapper<Account>{
@Override
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.getDouble("money"));
return account;
}
}
}
得到的查询结果
ccc
单条数据========Account [id=5, name=ccc, money=333333.0]
集合列表=======[Account [id=2, name=bbb, money=222222.0], Account [id=3, name=ccc, money=333333.0], Account [id=4, name=ccc, money=333333.0], Account [id=5, name=ccc, money=333333.0], Account [id=7, name=李四, money=240000.0], Account [id=8, name=hhh, money=20000.0], Account [id=9, name=hhh, money=20000.0], Account [id=10, name=eee, money=555555.0], Account [id=11, name=eee, money=555555.0], Account [id=12, name=eee, money=555555.0], Account [id=13, name=张三, money=230000.0], Account [id=14, name=张三, money=230000.0], Account [id=15, name=张三, money=230000.0], Account [id=16, name=张三, money=230000.0], Account [id=17, name=张三, money=230000.0], Account [id=18, name=张三, money=230000.0], Account [id=19, name=张三, money=230000.0], Account [id=20, name=张三, money=230000.0]]
个数=======18